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 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000