我试图在输入用户名和密码后重定向到其他名为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;
}
?>

和面板代码-&gt;

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

我try 使用会话查看面板页面

推荐答案

登录表单的操作是"Panel.php",但应该是"Login.php".通过将数据发送到Panel.php,Login.php永远没有机会处理POST数据和设置会话值,而使用未设置的会话值,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'); 
}

Php相关问答推荐

PHP Perl正则表达式—URL前面没有等号和可能的单引号或双引号

获得客户S在WooCommerce为期1年的总支出

启用额外的WooCommerce产品库存位置

MULTI UPDATE Laravel为什么只能更新我的第一行

PHP空数组定义与开始或结束逗号

限制某些产品只能在WooCommerce的特定邮政编码/邮政编码范围内发货

在WP工具中页面加载时可短暂看到的JavaScript生成的字段

PHP -将字符串拆分为两个相等的部分,但第二个字符串中的单词更多

根据发货类别在WooCommerce产品页面中显示快捷代码

如何将WooCommerce产品从一个产品复制到另一个产品S

在带有livewire 3的laravel中使用规则方法时,无法进行实时验证

Regex|PHP捕获json字符串中的每个非法双引号

使用 php Carbon 获取两个日期之间的随机日期和时间

从 XAMPP 运行 shell 脚本时,意外标记(附近出现语法错误

多个产品类别 Select 自定义 WooCommerce 插件设置页面

Woocommerce API 的 PHP Curl 附加参数

PHP 合并/合并多维数组

带有 nginx 的 Symfony 不提供 .js 和 .css 文件

使用 PHP ImageMagick 库将 pdf 文件的前 n 页转换为单个 png 图像文件

如何简化 php API 中的格式化数组列表数据以提供 React Native 部分列表