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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?