dongqindu8110 2015-10-20 14:35
浏览 43
已采纳

会话不会在标签内传递。 PHP会话

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

Notice: Undefined index: password in ...\login.php on line 9

Wrong username or password

session start 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

<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

    <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>
  • 写回答

1条回答 默认 最新

  • douhe3313 2015-10-20 15:17
    关注

    -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. Actually your coding of login.php is wrong, thats why you are getting Undefined index error.

    -First of all session_start() must be before <!DOCTYPE html>

    -$_SESSION["$user"] is wrong, use $_SESSION['user'] instead.

    -Why are you using $_REQUEST? Your form method is POST, so use $_POST. Refer $_REQUEST vs $_GET and $_POST

    -Session must be destroyed when user logout.

    -Here is the working code...

    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

    <?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

    <?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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化