dqoqnmb163241 2019-01-27 01:23
浏览 44
已采纳

在用户输入正确的登录详细信息后,为什么我的登录脚本无法正常运行?

when the user enters the username and password for the script why doesn't it forward them onto the protected php page? Can someone help me fix my script please?

I have checked the file names and they're correct so I'm not really sure what to do next, I can't see the problem. Therefore, I thought I'd write a post about it on here.

Any help would be greatly appreciated, thanks in advance!

login.php script

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $user = $_POST["username"];
    $pass = $_POST["password"];
    $validated = false;

    session_start();
    if($user!=""&&$pass!="")
    {
        if($user=="test1"&&$pass=="test1")
            $validated = true;
        if($validated)
        {
            $_SESSION['login'] = "OK";
            $_SESSION['username'] = $user;
            $_SESSION['password'] = $pass;

            header('Location: protected.php');
        }
        else
        {
            $_SESSION['login'] = "";
            echo "Invalid username or password.";
        }
    }
    else $_SESSION['login'] = "";
}
?>

    <html>
     <body>
        <h1>Login Page</h1>
        <p>Please enter your username and password:</p>
        <form action="login.php" method="post">
            <table>
            <tr>
                <td align="right">Username: </td>
                <td><input size=\"20\" type="text" size="20" maxlength="15" name="username"></td>
            </tr>

            <tr>
                <td align="right">Password: </td>
                <td><input size=\"20\" type="password" size="20" maxlength="15" name="password"></td>
            </tr>

            <tr>
                <td> </td>
                <td colspan="2" align="left"><input type="submit" value="Login"></td>
            </tr>
            </table>
        </form>
     </body>
    </html>

protected script

<?php
   session_start();
   if($_SESSION['login'] != "OK")
   {
    header('Location: login.php');
    exit();
   }
?>
<html>
   <head
   <title>Protected Web Page</title>
   </head>
   <body>
      <h1>Protected Web Page</h1>
      <<p>You have successfully logged in!</p>
<img src="pic_mountain.jpg" alt="Mountain View" style="width:304px;height:228px">
<p>
    Your username is: <?= $_SESSION['username'] ?=><br/>
    Your password is: <?= $_SESSION['password'] ?>
</p>
   </body>
</html>
  • 写回答

1条回答 默认 最新

  • dtdfl62844 2019-01-27 02:45
    关注

    You have a few errors and pointless code (explained in comments) - This will work: (remove text after //)

    protected.php

    <?php
       session_start();
       if($_SESSION['login'] != "OK"){
            header('Location: login.php');
            exit();
       }
    ?>
    <html>
       <head> // Missing closing bracket > 
       <title>Protected Web Page</title>
       </head>
       <body>
          <h1>Protected Web Page</h1>
          <p>You have successfully logged in!</p> // had extra < at begining of line 
        <img src="pic_mountain.jpg" alt="Mountain View" style="width:304px;height:228px">
        <p>
            Your username is: <?php $_SESSION['username'] ?><br/> // dont use PHP opening and closing shortcodes, incorrect close
            Your password is: <?php $_SESSION['password'] ?> // dont use PHP opening and closing shortcodes
        </p>
       </body>
    </html>
    

    login.php

    <?php
    session_start(); // start session at top
    
    
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {  
        $user = $_POST["username"];
        $pass = $_POST["password"];
    
        if($user != "" || $pass != ""){ // should use || for OR, not AND 
            if($user=="test1"&&$pass=="test1"){ // this wilkl only work with this combination
            // removed validation boolean it's not needed
                $_SESSION['login'] = "OK";
                $_SESSION['username'] = $user;
                $_SESSION['password'] = $pass;
    
                header('Location: protected.php');
    
            }else{
                $_SESSION['login'] = "";
                echo "Invalid username or password.";
            }
        }else{
          $_SESSION['login'] = "";  
          echo "Username or password cannot be blank"; // added echo for blank inputs
        } 
    }
    ?>
    
        <html>
         <body>
            <h1>Login Page</h1>
            <p>Please enter your username and password:</p>
            <form method="post">
                <table>
                <tr>
                    <td align="right">Username: </td>
                    <td><input size="20" type="text" size="20" maxlength="15" name="username"></td>
                </tr>
    
                <tr>
                    <td align="right">Password: </td>
                    <td><input size="20" type="password" size="20" maxlength="15" name="password"></td>
                </tr>
    
                <tr>
                    <td> </td>
                    <td colspan="2" align="left"><input type="submit" value="Login"></td>
                </tr>
                </table>
            </form>
         </body>
        </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型