我已尝试过所有内容,但没有任何作用,因此,我创建了一个登录页面。现在当我首先登录时出现错误:
注意:未定义的索引:在第39行的C:\ wamp \ www \ ucp \ index.php中登录
然后当我登录时,它首先不会改变页面外观:
这是整个代码
<?php
$con = mysqli_connect("localhost","root","","samp");
if (mysqli_connect_errno())
{
echo "Failed to connect to the database: " . mysqli_connect_error();
die();
}
session_start();
if(isset($_GET['logout']))
{
setcookie("login", '',time()-86400); # delete cookie
exit(); # stop executing here
header('index.php');
}
if(!empty($_POST['username']) && !empty($_POST['password']))
{
$userName = isset($_POST["username"]) ? $_POST["username"] : null;
$userPass = isset($_POST["password"]) ? $_POST["password"] : null;
$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("login", $session,time()+3600);
echo "You are now logged in with hash: ".htmlspecialchars($_COOKIE["login"]). '<a href="index.php?logout=1">logout</a>?';
}
else
{
die('Account has not been found.');
}
}
if(isset($_COOKIE["login"]) && mb_strlen(isset($_COOKIE["login"]) == '32'))
{
$session = $con->real_escape_string($_COOKIE["login"]);
$query = "SELECT Ime FROM Igraci WHERE session = '$session' LIMIT 1";
$result = mysqli_query( $con, $query); $row = mysqli_fetch_array($result);
if($row['Ime'])
{
echo "User is already logged in with username ".$row['Ime']. " and hash: ".htmlspecialchars($_COOKIE["login"]). '<a href="index.php?logout=1">logout</a>?';
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 action="index.php" method="POST">
<input type="text" placeholder = "Username" name="username">
<input type="password" placeholder = "Password" name="password">
<input type="submit" value="Login">
</form>
<div class="footer">
<p>roleplay factory © 2016 all rights reserved</p>
</div>
</body>
</html>
答案 0 :(得分:1)
您创建了之后的全局变量$ _COOKIE(其中包含PHP通过HTTP请求收到的Cookie信息)填充了数据,因此您无法访问它。
检查cookie是否存在。如果没有,它首先登录并输出值。
if ( isset( $_COOKIE[ "login" ] ) ) {
// your original line of code
echo "You are now logged in with hash: ".htmlspecialchars($_COOKIE["login"]). '<a href="index.php?logout=1">logout</a>?';
} else {
echo "You are now logged in with hash: ".htmlspecialchars($session). '<a href="index.php?logout=1">logout</a>?';
}
答案 1 :(得分:0)
第39行,如错误消息所示,如下所示:
echo "You are now logged in with hash: ".htmlspecialchars($_COOKIE["login"]). '<a href="index.php?logout=1">logout</a>?';
您不会检查是否设置了$_COOKIE["login"]
。