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

voase2hg  于 2023-09-29  发布在  PHP
关注(0)|答案(1)|浏览(93)

im尝试后把用户名和密码重定向到其他网页名为panel.php,但后把currect值在形式的目的地页面重定向我再次登录形式.我的登录代码->

<?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');
}
?>

我尝试查看包含会话面板页面

idfiyjo8

idfiyjo81#

登录表单的操作是“panel.php”,但应该是“login.php”。通过将数据发送到panel.php,login.php永远不会有机会处理post数据并设置SESSION值,并且使用未设置的SESSION值,panel.php重定向回login.php。
login.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; 
}

panel.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'); 
}

相关问题