如何在2个php文件之间连接变量

时间:2016-12-06 20:38:49

标签: php

大家好,这是我的" login.php":

<?php 
session_start();
// connect to database
if (isset($_POST['login_btn'])) {
    $username =$_POST['username'];
    $password =$_POST['password'];
    $_SESSION['username'] = $_POST['username'];
    $conn = oci_connect('insidedba', 'progetto16', 'localhost/XE');
    if (!$conn) {
    $e = oci_error();
    trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
    }

    $password = md5($password); // remember we hashed password before storing last time
    $sql = oci_parse($conn,"SELECT * FROM UTENTI WHERE USERNAME='$username' AND PASSWORD='$password'");
    $result =oci_execute($sql);
if ($result) {
    $stid = oci_parse($conn, "SELECT * FROM UTENTI WHERE username='$username' AND password='$password'");
oci_execute($stid);
oci_fetch($stid);

if (oci_num_rows($stid) == 1) {
    $_SESSION['message'] = "You are now logged in";
    $_SESSION['username'] = $username;
    header("location: panel/index.php"); //redirect to home page
} else{
    $_SESSION['message'] = "Username/password combination incorrect";
}
} else {
    $_SESSION['message'] = "Query error";
}
    }

?>

它有效,我问我是否可以在另一个php中使用$ username变量。 &#34; myorder.php&#34;:

<?php
session_start()
    $conn = oci_connect('insidedba', 'progetto16', 'localhost/XE');
        if (!$conn) {
        $e = oci_error();
        trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
        }
$columns = array(
  'Nome cinema ' => 'NOME_CINEMA', 
  'Ora' => 'ORARIO',
  'Data' => 'DATA',
  'Titolo' => 'TITOLO',
  'Numero prenotazione' =>'Codice_della_prenotazione',
  'Numero sala' =>'Numero_sala',
  'Fila' =>'fila',
  'Numero sala' =>'numero',
);
// Run the query
$sql = oci_parse($conn,"SELECT DISTINCT CIN.NOME AS NOME_CINEMA,PAL.DATA as DATA,PAL.ORA ORARIO,F.Titolo as TITOLO,
PRE.ID as Codice_della_prenotazione,S.CODS AS Numero_sala,P.FILA as fila,P.NUMERO as numero
FROM (((((UTENTI U JOIN PRENOTAZIONI PRE ON PRE.UTENTE=U.ID AND U.USERNAME='$username')
JOIN PALINSESTI PAL ON PAL.ID=PRE.PALINSESTO)
JOIN FILM F ON F.ID=PAL.FILM)
JOIN CINEMA CIN ON CIN.ID=PAL.CINEMA )
JOIN POSTI P ON P.PRENOTAZIONE=PRE.ID AND P.CINEMA=CIN.ID)
JOIN SALE S ON S.CINEMA=CIN.ID and p.SALA=S.CODS WHERE BOOL_PAGATO=1
ORDER BY PRE.ID");
oci_execute($sql);

// Output table header
echo "<table border=\"1px solid black\" width=\"95%\"><tr>";
foreach ($columns as $name => $col_name) {
  echo "<th>$name</th>";
}
echo "</tr>";

// Output rows 
while($row = oci_fetch_array($sql)) {
  echo "<tr>";
  foreach ($columns as $name => $col_name) {
    echo "<td style=\"text-align:center;\">". $row[$col_name] . "</td>";
  }
  echo "</tr>";
}
// Close table
echo "</table>"
?>

它表示未定义的变量, 我怎么解决这个问题? 我试过&#34;但它仍然无法运作。

2 个答案:

答案 0 :(得分:1)

如果它存储在会话中,你可以。

session_start();
if(!empty($_SESSION['username'])) $username = $_SESSION['username'];
else $username = "guest";

答案 1 :(得分:0)

您需要在 myorder.php 文件中使用U.USERNAME=$_SESSION['username']变量。

顺便说一句,在您显示的代码中, myorder.php 中没有$username变量,我猜您在SQL查询中已经拥有了:U.USERNAME='$prova'