因此,我目前正在为跳舞教练登台参加我们的舞蹈比赛... The Trainers创建一个帐户, 从该帐户登录 - >如果邮件和密码正确,一些会话变量将被启动,在这些变量内部是用户ID,邮件等以及用户“角色”(管理员或用户/ 1或2) 在用户已经启动这些变量之后将被转发到实际的用户区域网站。 这一方现在将检查是否设置了会话变量,并且如果用户角色足够高以访问该页面,则不会将用户转发到管理员或登录页面(admin如果角色= 2并且如果角色!= 1则登录或2) 但变量不保存在第一次登录尝试,你必须登录两次....PHP的SESSION变量,只做第二次尝试
这里是我的代码
登录页面:
session_start();
include('connect.php'); //mysql connection function
if (isset($_POST["login"])) // check if login attemp exists
{
//get mail from form
$mail = mysqli_real_escape_string($verbindung, $_POST["mail"]);
//get password from form
$pw = mysqli_real_escape_string($verbindung, $_POST["pw"]);
//check if user exists in db
$sql = "SELECT * FROM user_db WHERE mail = '$mail'";
$query = mysqli_query($verbindung, $sql);
while ($user = mysqli_fetch_object($query))
{
if (password_verify($pw, $user->password))
{
// set session variables
$_SESSION["login"] = $user->acc_type; //role
$_SESSION["id"] = $user->id; //id
$_SESSION["vorname"] = $user->vorname;
$_SESSION["mail"] = $user->mail;
$_SESSION["nachname"] = $user->nachname;
}
}
}
// check if login attempt succeded and get the user role
if ($_SESSION["login"] == 1)
{
// if role is user goto user page
echo '<meta http-equiv="refresh" content="100; URL=user" />';
}else if ($_SESSION["login"] == 2)
{
if role is admin goto admin page
echo '<meta http-equiv="refresh" content="100; URL=admin" />';
}
用户/管理员页面(两者具有相同的代码/错误):
session_start();
if ($_SESSION["login"] == '2')
{
//goto admin if user is admin
echo '<meta http-equiv="refresh" content="0; URL=../admin">';
}else if ($_SESSION["login"] != '1')
{
//goto login if user is neither role 1 or 2
//this one triggers allways if it is your first attempt to login
//it behaves like the session didnt start on the login page but i dont know why
echo '<meta http-equiv="refresh" content="0; URL=../">';
}
你的意思是登录后用户被重定向到登录了吗? 此外, 为什么你不使用标题位置? 为什么你在不检查信息安全的情况下使用查询? – Buddhi741
是的,在您点击登录后,您将被重定向到用户页面(这是应该发生的事情),但是由于会话变量未设置,您将再次被重定向到登录页面。但如果你再次进行登录,一切正常 –
,因为我从来没有听说过注入和标题位置:/我现在将谷歌那些:) –