我正在为我所在的机器人团队编写一个PHP登录系统。登录系统的流程如下。
我的问题是会话变量没有通过login.php页面上的重定向传递。它们是空的。
我检查过并且正在运作的事情:
以下是两个页面的代码:
的index.php
<?php // this is first line of page
session_start();
// If user is not logged in, sets $log_info == 'false'
if (!isset($_SESSION['log_info']) || $_SESSION['log_info'] == '' || $_SESSION['log_info'] == '0') { $_SESSION['log_info'] = 'false'; }
function get_banner() {
// Switch statement for all cases of $log_info... Usually displays banner to user...
switch ($_SESSION['log_info']) {
case 'true':
echo '<div class="banner">You are logged in!</div>';
$_SESSION['log_info'] = '';
break;
case 'false':
echo '<div class="banner">Please login below.</div>';
$_SESSION['log_info'] = '';
break;
case 'error':
echo '<div class="banner">Your username or password do not match.</div>';
$_SESSION['log_info'] = '';
break;
case 'acc_fail_username':
echo '<div class="banner"><b>Account creation failed:</b><br/>Your selected username already exists. Please pick a different one.</div>';
$_SESSION['log_info'] = '';
break;
case 'acc_fail_parm':
echo '<div class="banner"><b>Account creation failed:</b><br/>One or more of your entry fields were invalid. Please try again.</div>';
$_SESSION['log_info'] = '';
break;
case 'acc_success':
echo '<div class="banner">Your account was successfully created! Login below.</div>';
$_SESSION['log_info'] = '';
break;
default:
$_SESSION['log_info'] = '';
//die('Something went wrong internally. Please stay calm while we fix it');
}
}
?>
<?php get_banner(); ?>
<b>Login:</b>
<form action="login.php" method="post">
Username: <input type="text" name="username"><br/>
Password: <input type="password" name="password"><br/>
<input type="submit">
</form>
的login.php
<?php // this is first line of page
session_start();
// Connect and verify connection
$con = @mysqli_connect($host, $username, $password, $dbname);
$username = $_POST['username'];
$password = $_POST['password'];
$query = mysqli_query($con, "SELECT * FROM FRC4096users WHERE username='".$username."'");
$row = mysqli_fetch_array($query);
$salt = $row['salt'];
if ($row['passwordhash'] === hash('sha512', $password . $salt)) {
$salt = hash('sha512', rand());
mysqli_query($con, "UPDATE users SET salt='". $salt ."' WHERE username='".$username."'"); // Resets salt on login for security
mysqli_query($con, "UPDATE users SET passwordhash='". hash('sha512', $password . $salt) ."' WHERE username='".$username."'"); // Resets salt on login for security
$_SESSION['log_info'] = 'true';
session_write_close();
header('Location: /');
die();
} else {
$_SESSION['log_info'] = 'error';
session_write_close();
header('Location: /');
die();
}
?>
答案 0 :(得分:0)
你应该尝试一些事情:
session_destroy()
。session_write_close()
,因为这会在解析结束时自动执行,而且您的脚本很短。error_reporting(E_ALL);
更改错误报告级别。print_r($_SESSION)
都可以帮助您找到数据的去除位置。这可以帮助您检测错误的位置。
祝你好运!答案 1 :(得分:0)
如果你这样做会怎么样......
<?php // this is first line of page
session_start();
if (!isset($_SESSION['log_info']){ header('Location: /login.php');die();}
else {
switch ($_SESSION['log_info']) {
case 'true': echo '<div class="banner">You are logged in!</div>';
unset($_SESSION['log_info']);
break;
case 'false':
echo '<div class="banner">Please login below.</div>';
unset($_SESSION['log_info']);
break;
case 'error':
echo '<div class="banner">Your username or password do not match.</div>';
unset($_SESSION['log_info']);
break;
case 'acc_fail_username':
echo '<div class="banner"><b>Account creation failed:</b><br/>Your selected username already exists. Please pick a different one.</div>';
$_SESSION['log_info'] = '';
break;
case 'acc_fail_parm':
echo '<div class="banner"><b>Account creation failed:</b><br/>One or more of your entry fields were invalid. Please try again.</div>';
unset($_SESSION['log_info']);
break;
case 'acc_success':
echo '<div class="banner">Your account was successfully created! Login below.</div>';
unset($_SESSION['log_info']);
break;
default:
unset($_SESSION['log_info']);
break;
}
}
?>
<b>Login:</b>
<form action="login.php" method="post">
Username: <input type="text" name="username"><br/>
Password: <input type="password" name="password"><br/>
<input type="submit">
</form>
然后在你的登录页面上,有......在顶部......休息是一样的....
<?php // this is first line of page
session_start();
session_unset();
//rest of code