如何在PHP中根据会话将用户重定向到不同的页面?

How can I redirect users to different pages based on session in PHP?

提问人:mohammad safari 提问时间:9/27/2023 最后编辑:Crocsxmohammad safari 更新时间:9/29/2023 访问量:47

问:

我在将用户名和密码重定向到其他名为面板的页面后尝试.php但是在将当前值放入表单目标页面后,再次将我重定向到登录表单。我的登录码 ->

<?php
session_start();
$login='
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>login</title>
    </head>
    <body>
        <h1> login form</h1>
        <pre>
        <form method="POST" action="panel.php" >
            <input type="text" name="username">
            <input type="password" name="password">
            <input type="submit" name="submit">
        </form>  
    </body>
</html>
';
if(isset($_POST['username']) && isset($_POST['password'])){
    $username= $_POST['username'];
    $password= $_POST['password'];
    if ($username == 'teddy' && $password == '123'){
        $_SESSION['X'] = true;
        $_SESSION['username'] = $username;
        header('location: panel.php');
        exit();
    }
    else{
        echo "invalid credentials";
    }
}
else
{
    echo $login;
}
?>

和面板代码 ->

<?php
session_start();
$panel = '
<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>panel</title>
    </head>
    <body>
        <p>welcom to the panel; dear ' . $_SESSION['username'] . ' </p>
    </body>
</html>
';
if ($_SESSION['X'] === true){
    echo $panel;
}
else{
    header('location: login.php');
}
?>                                                                                                                                                                                                                                                                                                                                             

我尝试查看带有会话的面板页面

PHP cookie 变量 会话状态

评论

2赞 brombeer 9/27/2023
欢迎,请使用换行符而不是空格来布局代码

答:

1赞 volkerschulz 9/27/2023 #1

登录表单的操作是“panel.php”,但应该是“login.php”。通过将数据发送到 panel.php,login.php 永远没有机会处理 post 数据并设置 SESSION 值,并且取消设置 SESSION 值,panel.php 会重定向回 login.php。

登录名:.php

<?php
session_start();
$login='<!DOCTYPE html>   <html lang="en">  <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0">   <title>login</title>    </head> <body>  <h1> login form</h1>   <pre>  <form method="POST" action="login.php" >   <input type="text" name="username">   <input type="password" name="password">    <input type="submit" name="submit">   </form></body> </html>';
if(isset($_POST['username']) && isset($_POST['password'])) {
    $username= $_POST['username'];
    $password= $_POST['password'];
    if ($username == 'teddy' && $password == '123'){
        $_SESSION['X'] = true; 
        $_SESSION['username'] = $username;
        header('location: panel.php');
        exit();
    } else { 
        echo "invalid credentials";  
    }
} else { 
    echo $login; 
}

面板:.php

<?php
session_start();
$panel = '<!DOCTYPE html>   <html lang="en">  <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0">   <title>panel</title>    </head> <body>   <p>welcom to the panel; dear ' . $_SESSION['username'] . ' </p></body> </html>';
if ($_SESSION['X'] === true) {
    echo $panel;
} else {
    header('location: login.php'); 
}

评论

0赞 mohammad safari 9/28/2023
感谢您的解释,我的问题已通过这个答案得到解决。