所以我不知道还能做什么,它造成太多问题,所以这是代码,我希望他保持登录状态但是这不符合预期的工作,如果他登录并且去了索引页面将他重定向到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 © 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>
答案 0 :(得分:0)
问题:
当您注销时,首先删除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();
}
你不再需要它了,这可能会导致问题。
答案 1 :(得分:0)
有几个问题:
在这段代码中,即使用户未获得授权,您也会将用户重定向到 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();
}
if(!$auth == "ok") {
即使$auth == "ok"
,这种情况也总会失败。这是因为!
运算符仅适用于$auth
而不适用于相等。
事实上,由于这个错误,你的第一个问题并没有那么糟糕,因为如果这个条件已经写好了,那么一旦用户登录就会有一个重定向循环! / p>
而是写:
if($auth !== "ok") {
您永远不应将echo
或print
与header(location. ...)
合并。无论哪个先发生,都会使另一个声明失败。
另外,如果在exit
语句后没有header
,则可能会继续执行其他不合适的代码。
所以这段代码:
...
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
,并在每个exit
语句后添加header
:
...
header( "Location:welcome.php");
exit();
}
else
{
header("location: index.php?err=1");
exit();
}
当您通过请求($_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);