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

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵