【发布时间】:2010-09-15 08:50:38
【问题描述】:
我有一个从数据库中抓取散列密码的登录表单。如果“提交检查”<input type="hidden"> 等于 1(下面的示例将更好地解释这一点),则显示页面内容,如果不等于 1,则显示登录表单。形式如下:
<div id="login" style="<?php echo $style ?>"> //$style is by default "visibility:visible;" but will change to "visibility:hidden;" when correct login info is given
<p>Log in</p>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<input type="text" id ="username" name="username" value="Username" onfocus="if (this.value == 'Username') this.value=''">
<input type="password" id="password" name="password" value="passpass" onfocus="if (this.value == 'passpass') this.value=''">
<br>
<input type="submit" name="submit" value="Log Ind">
<input type="hidden" name="_submit_check" value="1"> //Submit checker. if set, process login information
</form>
</div>
<p>No user? Make one <a href="register.php">here.</a></p>
</div>
这对我的 PHP 示例很有效,但有一点令人讨厌……每次查看页面时,您都必须登录。因此我在我的 PHP 脚本中这样做了:
<?php
session_start();
$db = DB::connect('mysql://username:pass@host/database');
if (DB::isError($db)){
die("Can't connect: " . $db->getMessage());
}
$style = "visibility:visible;";
$passwordHash = sha1($_POST['password']);
$_SESSION['login'] = $_POST['_submit_check']; //This is the submit checker I mentioned before
$sql = 'SELECT username FROM user WHERE username = ? AND passwordHash = ?';
$result = $db->query($sql, array($_POST['username'], $passwordHash));
if ($_SESSION['login'] == 1) {
if ($result->numRows() < 1)
{
echo '<p>Correct your username and password please</p>';
}
else {
$style = "visibility: hidden;";
echo '<p>This is the page content</p>';
}
}
?>
我将 $_POST['_submit_check'] 值添加到名为 'login' 的 $_SESSION[] 变量中的事实不应该使用户只需要每 24 分钟登录一次吗?这就是我想要的,但它没有发生......
希望您能理解我的问题,如果没有,请对您不理解的地方发表评论。我很难解释我在这个问题上的想法;)
【问题讨论】:
标签: php session-variables