我是 php 新手,在创建登录脚本时遇到问题。在代码的第一部分,它可以找到属于登录者的正确id,并在$_SESSION['id']中正确设置。然后脚本重定向回索引,但在这里我收到一条错误消息,指出会话中未设置 id。
注意:未定义的索引:第 11 行 C:\xampp\htdocs\gym\v1\content\body\panel.php 中的 id
$sql="SELECT id FROM user WHERE email='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if($count==1){
session_regenerate_id(); //Regenerate session ID to prevent session fixation attacks
$member = mysql_fetch_assoc($result);
$_SESSION['id'] = $member['id'];
session_write_close();
header("location: ../index.php?page=2");
exit();
}
else {
header("Location: ../index.php?page=1");
exit();
}
?>
session_start();
echo 'session id:' . $_SESSION['id'];
if(!isset($_SESSION['id']) || (trim($_SESSION['id']) == '')) {
echo 'error not session id set';
exit();
}
$sql="SELECT * FROM user WHERE id='" . $_SESSION['id'] . "'";
$result=mysql_query($sql);
$member = mysql_fetch_assoc($result);
$mail = $member['emails'];
echo '<h1>Welcome!'. $mail . ' </h1>';
提前致谢