我目前正在学习如何使用PHP创建登录系统。一切似乎都很简单 - 我将输入数据设置为$_SESSION
变量,然后在我打开的下一页上启动会话。但是,当我刷新页面时,会话仍在工作,但变量数据已被删除且无法识别。
这是我设置变量的php:
<?php
include ("connection.php"); //connects to the database.
if(isset($_POST['login']))
{
$user_name = $_POST['EMAIL'];
$password = $_POST['PASSWORD'];
// echo $user_name, " AND ", $password, "<br>";
$q = "SELECT * FROM ".$table." WHERE EMAIL='".$user_name."' AND PASSWORD= '".$password."' AND IS_ADMIN= 'YES';";
// echo "<br>",$q,"<br>";
$r = mysqli_query($conn, $q);
if(mysqli_num_rows($r) > 0)
{
if(session_id() == '' || !isset($_SESSION))
{
// session isn't started
session_start();
echo "<br>GREAT SUCCESS!!!<br>";
$_SESSION["SESSION_EMAIL"]= $user_name;
$_SESSION["SESSION_PASSWORD"]= $password;
echo $_SESSION["SESSION_EMAIL"], " ", $_SESSION["SESSION_PASSWORD"];
header("Refresh:0; url=\website2.php");
}
}
else echo "<br>FAIL!!!<br>";
}
?>
这是我用来检查下一页会发生什么的位:
<?php
session_start();
if (session_status() == PHP_SESSION_ACTIVE)
{
echo 'Session is active <br>';
} else echo"session is ded";
echo $_SESSION["SESSION_EMAIL"]; // it prints out an error if it screws up here
?>
<?php
session_start();
include ("connection.php"); //connects to the database.
if(isset($_POST['login']))
{
$user_name = $_POST['EMAIL'];
$password = $_POST['PASSWORD'];
// echo $user_name, " AND ", $password, "<br>";
$q = "SELECT * FROM ".$table." WHERE EMAIL='".$user_name."' AND PASSWORD= '".$password."' AND IS_ADMIN= 'YES';";
// echo "<br>",$q,"<br>";
$r = mysqli_query($conn, $q);
if(mysqli_num_rows($r) > 0)
{
if(!isset($_SESSION["SESSION_EMAIL"]))
{
// session isn't started
echo "<br>GREAT SUCCESS!!!<br>";
$_SESSION["SESSION_EMAIL"]= $user_name;
$_SESSION["SESSION_PASSWORD"]= $password;
echo $_SESSION["SESSION_EMAIL"], " ", $_SESSION["SESSION_PASSWORD"];
header("Refresh:0; url=\website2.php");
}
}
else echo "<br>FAIL!!!<br>";
}
?>
并在第二页检查这样的事情
<?php
session_start();
if (isset($_SESSION["SESSION_EMAIL"]))
{
echo 'Session is active <br>';
echo $_SESSION["SESSION_EMAIL"];
} else echo"session is ded";
// it prints out an error if it screws up here
?>
希望这个帮助
要么
您可以检查其他会话变量
我真的很讨厌这样说而且老实说我觉得我应该早点完成它但是在重新启动我的XAMPP和PHP服务器之后,一切似乎都已经修好了,会议现在正常工作。现在没有任何东西被丢弃,页面之间的所有转换都毫不费力。
尽管如此,我向所有考虑过我这个愚蠢问题的人道歉,我还要感谢他们所有的时间和精力来回答。
再一次,谢谢大家!