我对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);
该页面只是空的。
答案 0 :(得分:-1)
第一
session_start(); //always in first line. Before include/require functions.
秒。您可以为noncookies用户添加会话ID
if (session cookie exists == true)
header('location:echosession.php');
else
header('location:echosession.php?'.session_name().'='.session_id());
您100%确定$ myusername不为空?
通过
检查var_dump($myusername); die(); //in first script before add to session
我想知道你为什么在第二个脚本中没有输出。看起来您已关闭会话问题和错误报告。
在脚本开始时插入
<?PHP
ini_set( 'display_errors', 'On' );
error_reporting( E_ALL );
?>