【问题标题】:cookies redirect loop not workingcookie 重定向循环不起作用
【发布时间】:2016-05-10 11:25:15
【问题描述】:

所以我不知道还能做什么,这会导致很多问题,所以这是代码,我希望他保持登录状态,但这没有按预期工作,我希望他登录并转到将他重定向到welcome.php的索引页面,但它不能正常工作,有时它会说这个网页有重定向循环

这是 index.php

<?php 

    error_reporting(0);

    session_start();


    $con = mysqli_connect("localhost","root","","samp");

    if (mysqli_connect_errno())
    {
        echo "Failed to connect to the database: " . mysqli_connect_error();
        die();
    }

    if(isset($_POST['login_button']))
    {

        $userName = $_POST['username']; 
        $userPass = $_POST['password']; 

        $hashedPass = hash('whirlpool', $userPass);
        $query = "SELECT Ime FROM Igraci WHERE Ime = '$userName' AND Lozinka = '$hashedPass'";

        $result = mysqli_query( $con, $query);

        $row = mysqli_fetch_array($result);

        if($row)
        {
            $session = md5($userName.$hashedPass);
            mysqli_query($con, "UPDATE Igraci SET session = '$session' WHERE Ime = '$userName' AND Lozinka = '$hashedPass'");
            setcookie("username", $_POST['username'], time()+3600*24);
            setcookie("authorization","ok");
            header( "Location:welcome.php");
            echo "You are now logged in with hash: ".htmlspecialchars($_POST['username']). ' <a href="index.php?logout=1">logout</a>?';
        }
        else
        {
            header("location: index.php?err=1");
        }
    }

    if(isset($_GET['logout']))
    {
        setcookie("username", "", time()-60);
        setcookie("authorization", "no" );
        header( "Location:index.php");
        exit(); # stop executing here
    }


    if($_COOKIE['authorization'] == "ok") {
        header ("Location:welcome.php");
        exit();
    }
    else if($_COOKIE['authorization'] == "no")
    {
        header ("Location:welcome.php");
        exit();

    }
?>

<!DOCTYPE html>
<html>
<head>
    <title>Roleplay Factory User Control Panel</title>
    <link rel="stylesheet" type="text/css" href="style.css">
    <link href='https://fonts.googleapis.com/css?family=Roboto:300' rel='stylesheet' type='text/css'>
</head>
<body>

<h1>Welcome, please login to your account.</h1>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="text" required placeholder = "Username" name="username">
    <input type="password" required placeholder = "Password" name="password">
    <input type="submit" name="login_button" value="Login">
</form> 

<div class="footer">
<p>roleplay factory &copy; 2016 all rights reserved</p>
</div>

</body>
</html>

这是welcome.php

<?php 
    $auth = $_COOKIE['authorization'];
    header ("Cache-Control:no-cache");
    if(!$auth == "ok") {
        header ("Location:index.php");
        exit();
    }
?>

<html>
<head> <title>Logged In</title> </head>
<body>
    <p>Successful log-in.</p>
</form>
</body>
</html>

【问题讨论】:

    标签: php session cookies mysqli error-handling


    【解决方案1】:

    有几个问题:

    1。 index.php 中的错误重定向

    在这段代码中,即使用户未获得授权,您也会将用户重定向到 welcome.php

    if($_COOKIE['authorization'] == "ok") {
        header ("Location:welcome.php");
        exit();
    }
    else if($_COOKIE['authorization'] == "no")
    {
        header ("Location:welcome.php");
        exit();
    }
    

    你不想这样。相反,您将希望显示 index.php 页面或重定向到某些 unauth.php (如果您有类似的东西)。但是,如果您想在 index.php 上留下未经授权的用户以便他们可以重新登录,那么只需省略 else 部分:

    if($_COOKIE['authorization'] == "ok") {
        header ("Location:welcome.php");
        exit();
    }
    

    2。 Welcome.php 状态不佳

    if(!$auth == "ok") {
    

    这种情况总是会失败,即使$auth == "ok"。这是因为 ! 运算符仅适用于 $auth 而不适用于相等。

    事实上,因为这个bug,你的第一个问题并没有想象中那么糟糕,因为如果这个条件被写成它应该写的,一旦用户登录,你总是会有一个重定向循环!

    改为写:

    if($auth !== "ok") {
    

    3。重定向时生成输出

    切勿将echoprintheader(location. ...) 结合使用。以先到者为准。

    另外,如果在header 语句之后不exit,您可能会继续执行其他不合适的代码。

    所以这段代码:

            ...
            header( "Location:welcome.php");
            echo "You are now logged in with hash: ".htmlspecialchars($_POST['username']). ' <a href="index.php?logout=1">logout</a>?';
        }
        else
        {
            header("location: index.php?err=1");
        }
    

    ... 有这些类型的问题。由于没有exit,它下面的代码也被执行,其中也有header 语句!这肯定会导致不良行为。

    您应该删除echo,并在每个header 语句之后添加一个exit

            ...
            header( "Location:welcome.php");
            exit();
        }
        else
        {
            header("location: index.php?err=1");
            exit();
        }
    

    4。 SQL 注入漏洞

    当您从通过请求 ($_POST) 传入的数据构建 SQL 查询字符串时,您的代码容易受到 SQL injection 的攻击。

    改为使用准备好的语句。这是您可以更改代码以防止 SQL 注入的方法:

    // Use placeholders where you want to insert user-supplied data:
    $query = "SELECT Ime FROM Igraci WHERE Ime = ? AND Lozinka = ?";
    // Let the DB engine compile this statement:
    $stmt = mysqli_prepare($con, $query) or die(mysqli_error($con));
    // Tell DB engine what the parameters are (this is safe):
    mysqli_stmt_bind_param($stmt, "ss", $userName, $hashedPass);
    // Execute the query with these parameter values:
    mysqli_stmt_execute($stmt);
    // Fetch the result object:
    $result = mysqli_stmt_get_result($stmt);
    $row = mysqli_fetch_array($result);
    

    【讨论】:

      【解决方案2】:

      问题:

      当您注销时,您首先删除了 cookie,然后将其设置为 no。请注意,如果您有$_COOKIE['authorization']=='no',您将陷入循环,因为:

      if($_COOKIE['authorization'] == "ok") {
          header ("Location:welcome.php");
          exit();
      }
      else if($_COOKIE['authorization'] == "no")
      {
          header ("Location:welcome.php");
          exit();
      }
      

      所以,正如你所说:

      有时它说这个网页有重定向循环

      我猜第一次(没有设置 cookie)你可以看到索引,因为它忽略了 $_COOKIE['authorization'] == "ok"$_COOKIE['authorization'] == "no"。然后,当您登录和注销时,您会陷入循环,因为现在 $_COOKIE['authorization'] == "no"true

      解决方案:

      只删除cookie,不要设置为"no"(删除或评论setcookie("authorization", "no" );)。

      奖励:

      删除

      else if($_COOKIE['authorization'] == "no")
      {
          header ("Location:welcome.php");
          exit();
      
      }
      

      你不再需要它了,这可能会导致问题。

      【讨论】:

      • 所以是这样的:我已经对其进行了测试,我认为它有效,你认为我做了什么让 sql 注入变得容易,我能做些什么来预防它? i.imgur.com/Hu5cTp7.jpg
      • 我认为实际上以页面编程方式注入 MySQL 代码很容易,@trincot 的回答解释了如何解决这个问题以及其他一些问题,请查看。还有,
      • [错误发送上一条评论]:我认为实际上以页面编程的方式注入MySQL代码很容易,@trincot的回答解释了如何解决这个问题以及其他一些问题,采取看。另外,考虑使用另一种方式来存储 cookie。用户可以从他的一端更改 cookie 的内容,因此,如果我知道您的用户名,我可以让系统认为我与您的用户登录。 Here are some ideas。如果我的第一个答案解决了问题,请考虑投票并标记为解决方案。谢谢。
      • 我认为如果我对密码进行哈希处理并将其存储为 cookie 是不可能的,对吗?
      • 密码是敏感信息,也许您可​​以在用户注册帐户时对用户 ID、用户名和一些随机生成的字符串的组合进行哈希处理(这样您可以确保所有用户都有唯一的 cookie 字符串,没有人知道他/她自己的令牌是如何生成的)。
      猜你喜欢
      • 1970-01-01
      • 2018-01-22
      • 2012-12-31
      • 2012-05-18
      • 2015-12-14
      • 1970-01-01
      • 1970-01-01
      • 2018-11-27
      • 2012-12-21
      相关资源
      最近更新 更多