我对 php 相当陌生,并试图重新熟悉这些信息。我目前有一个登录脚本工作正常,直到我设置变量 - 它检查用户/密码很好,但是当它重定向到 echosession.php 时 $_SESSION['username']; 变量不回显。另外 - 检查用户是否登录以及注销。
include 'db_connect.php';
include 'functions.php';
session_start();
//user tabls
$tbl_name="users";
// username and password sent from form
$myusername=$_POST['username'];
$mypassword=$_POST['password'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = md5($mypassword);
$myusername = $mysqli->real_escape_string($myusername);
$log_sql="SELECT * FROM $tbl_name WHERE email='$myusername' and password='$mypassword'";
$log_result=$mysqli->query($log_sql);
// Mysql_num_row is counting table row
$log_count=$log_result->num_rows;
// If result matched $myusername and $mypassword, table row must be 1 row
if($log_count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
// Set username session variable
$_SESSION['username'] = $myusername;
header("location:echosession.php");
}
else {
echo "Wrong Username or Password";
}
如果我尝试像这样加载 echosession.php:
session_start();
print("hi");
echo $_SESSION["username"];
var_dump($_SESSION);
页面只是空的。