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

问题描述 投票:0回答:1

我尝试将用户名和密码重定向到名为 panel.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 session session-cookies session-variables session-state
1个回答
0
投票

您的登录表单的操作是“panel.php”,但应该是“login.php”。通过将数据发送到 panel.php,login.php 永远没有机会处理发布数据并设置 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'); 
}
© www.soinside.com 2019 - 2024. All rights reserved.