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 vhdl+MODELSIM
  • ¥20 simulink中怎么使用solve函数?
  • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题