droirqk4795 2016-06-08 20:09
浏览 19
已采纳

不安全的PHP登录表单练习(返回没有错误,无法弄清楚。)

I have a Login script (it's a basic demonstration exercise of PHP for the web class i'm in) that is insecure, but intentionally so. Advice regarding exploits will go unnoticed, as this is for testing purposes only.

I have stumped the teacher. The code I have written has been bugfixed a number of times, yet my code still does not run correctly. I double-checked the functions I was using with the version of PHP on my webhost (I'm running 5.4) to make sure things were correctly written.

We can't figure out what's wrong. Maybe someone else can?

Here's the three files:

login.php

<html>
<body>
<form action="process.php" method="post">
Username: <input type="text" name="name"><br>
Password: <input type="text" name="pass"><br>
<input type="submit">   
<?php
        if (isset($_SESSION ["errorwelcome"])){
          echo ($_SESSION ["errorwelcome"]);
          unset($_SESSION ["errorwelcome"]);
      }
      if (isset($_SESSION ["errorunpass"])){
          echo ($_SESSION ["errorunpass"]);
          unset($_SESSION ["errorunpass"]);
      }
    ?>
</form>
</body>
</html>

process.php

    <?php
if ($_POST ["name"] == "shmailey"){
        if ($_POST ["pass"] == "password"){
            $_SESSION["login"] = 1; 
            header('Location: welcome.php');
            exit;
        }
}

    if ($_POST ["name"] != "shmailey"){
            $_SESSION ["errorunpass"] = "Uh oh, we didn't recognize those credentials. Incorrect Username and/or Password.";
            header('Location: login.php');
            exit;
    }
    ?>

welcome.php

    <?php
    if (isset($_SESSION ["login"])){
        unset ($_SESSION["login"]);
        }
    else{
            $_SESSION["errorwelcome"] = "You need to login first.";
            unset ($_SESSION["login"]);
            header('Location: login.php');
            exit;
        }
    ?>
<html>
<body>
    <?php
        echo "Login Success";
        echo "Username: shmailey";
        echo "Password: password";
    ?>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • douyu4535 2016-06-08 20:43
    关注

    You don't start your sessions. You must start sessions using session_start(); before using any session variables. Try the following code:

    Login.php:

        <html>
    <body>
    <form action="process.php" method="post">
    Username: <input type="text" name="name"><br>
    Password: <input type="text" name="pass"><br>
    <input type="submit">   
    <?php
            if (isset($_SESSION ["errorwelcome"])){
              echo ($_SESSION ["errorwelcome"]);
              unset($_SESSION ["errorwelcome"]);
          }
          if (isset($_SESSION ["errorunpass"])){
              echo ($_SESSION ["errorunpass"]);
              unset($_SESSION ["errorunpass"]);
          }
        ?>
    </form>
    </body>
    </html>
    

    process.php:

       <?php
       session_start();
    if ($_POST ["name"] == "shmailey"){
            if ($_POST ["pass"] == "password"){
                $_SESSION["login"] = 1; 
                header('Location: welcome.php');
                exit;
            }
    }
    
        if ($_POST ["name"] != "shmailey"){
                $_SESSION ["errorunpass"] = "Uh oh, we didn't recognize those credentials. Incorrect Username and/or Password.";
                header('Location: login.php');
                exit;
        }
        ?>
    

    welcome.php:

        <?php
        session_start();
        if (isset($_SESSION ["login"])){
            unset ($_SESSION["login"]);
            }
        else{
                $_SESSION["errorwelcome"] = "You need to login first.";
                unset ($_SESSION["login"]);
                header('Location: login.php');
                exit;
            }
        ?>
    <html>
    <body>
        <?php
            echo "Login Success";
            echo "Username: shmailey";
            echo "Password: password";
        ?>
    </body>
    </html>
    

    From the little testing I did, it seems to work when including the session starts.

    EDIT-- Patrick M noticed this in the comments of the question, I did not see that until after I posted the answer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64