简体   繁体   English

会话不会在标签内进行。 PHP会话

[英]Sessions doesn't pass on within tabs. PHP session

I'm facing an issue where my simple dummy form doesn't pass it's session variable. 我遇到的一个问题是我的简单虚拟表单无法通过它的会话变量。 There is an error code 有错误代码

Notice: Undefined index: username in ...login.php on line 8 注意:未定义的索引:第8行... login.php中的用户名

Notice: Undefined index: password in ...\\login.php on line 9 注意:未定义的索引:第9行... \\ login.php中的密码

Wrong username or password 错误的用户名或密码

session start index.php 会话开始index.php

<html>
    <head></head>
<body>
<?php session_start();?>
<form action="login.php" method="POST">
     Username:<input type="text" name="username">
     Password:<input type="password" name="password">
    <input type="submit" value="Login">
</form>
</body>

</html>

Validation of the input will be passed on to login.php 输入的验证将传递给login.php

<html>
<head></head>
<body>
<?php 
    session_start();
    $user = "NULL";
    $password = "NULL";
    $user = $_REQUEST["username"];
    $password = $_REQUEST["password"];


    if($user == "ali" && $password == "123"){
    $_SESSION["$user"] = $user;
    echo "Login Successful</br>"; 
    echo '<a href="logout.php">Logout</a>';
    }
    else{
     echo "Wrong username or password</br>";
     echo '<a href="index.php">Back</a>';
    }
    ?>

    <p>Welcome <?php echo htmlspecialchars($user) ?></p>


 </body>
 </html>

logout.php logout.php

    <html>
    <head></head>
<body>
<?php session_start();?>
<form action="login.php" method="POST">
     Username:<input type="text" name="username">
     Password:<input type="password" name="password">
    <input type="submit" value="Login">
</form>
</body>

</html>

-You are facing Undefined index error because when you directly execute login.php or reload login.php or you open it in another tab, at that time $_REQUEST is an empty array. -您遇到未定义索引错误,因为直接执行login.php或重新加载login.php或在另一个选项卡中打开它时, $_REQUEST是一个空数组。 Actually your coding of login.php is wrong, thats why you are getting Undefined index error. 实际上,您login.php的编码是错误的,这就是为什么您遇到未定义索引错误的原因。

-First of all session_start() must be before <!DOCTYPE html> -首先session_start()必须在<!DOCTYPE html>

- $_SESSION["$user"] is wrong, use $_SESSION['user'] instead. - $_SESSION["$user"]是错误的,请改用$_SESSION['user']

-Why are you using $_REQUEST ? -为什么要使用$_REQUEST Your form method is POST, so use $_POST . 您的表单方法是POST,因此请使用$_POST Refer $_REQUEST vs $_GET and $_POST 参考$ _REQUEST与$ _GET和$ _POST

-Session must be destroyed when user logout. -用户注销时必须销毁会话。

-Here is the working code... -这是工作代码...

1. index.php 1. index.php

<html>
    <head></head>
<body>
<form action="login.php" method="POST">
     Username:<input type="text" name="username">
     Password:<input type="password" name="password">
    <input type="submit" value="Login">
</form>
</body>

</html>

2. login.php 2. login.php

<?php session_start(); ?>

<!DOCTYPE html>
<html>

<head>

</head>

<body>

<?php 

if( isset($_SESSION['user']) ){
   echo 'User is already logged in.<br />';
   echo '<a href="logout.php">Logout</a>';
}
else {
    if( isset($_POST["username"]) && isset($_POST["password"]) ) {
        if($_POST["username"] == "ali" && $_POST["password"] == "123"){
            $_SESSION["user"] = $_POST["username"];
            echo "Login Successful</br>"; 
            echo '<a href="logout.php">Logout</a>';
        }
        else {
            echo "Wrong username or password</br>";
            echo '<a href="index.php">Back</a>';    
        }    
    }
}

?>
 </body>
 </html>

3. logout.php 3. logout.php

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// remove all session variables
session_unset();

// destroy the session
session_destroy();
echo "All session variables are now removed, and the session is destroyed."
?>

</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM