【发布时间】: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 © 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