饼干重定向循环不起作用


cookies redirect loop not working

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

这是索引.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>

这是值得欢迎的.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>

有几个问题:

1. 索引重定向错误.php

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

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

你不想要那个。相反,您需要显示索引.php页面或重定向到一些不身份验证.php(如果您有类似的东西(。但是,如果您想在索引上留下未经授权的用户.php以便他们可以重新登录,那么只需省略else部分:

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

2.欢迎条件不好.php

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

即使$auth == "ok" .这是因为!运算符仅适用于$auth而不适用于相等性。

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

而是写:

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注入的影响。

请改用预准备语句。以下是更改代码以防止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);

问题:

注销时,您首先删除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();
}

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