此表单未提交,但如果密码正确,我试图让它重定向到一个页面,如果不正确,则转到主页。我还试图加入一个记住我的 cookie 功能。一点帮助将不胜感激。
代码:
<?php
if($logged_in){header('Location: index.php');}
function checkLogin(){
/* Check if user has been remembered */
if(isset($_COOKIE['cookpass'])){
$_SESSION['password'] = $_COOKIE['cookpass'];
}
/* Username and password have been set */
if(isset($_SESSION['password'])){
/* Confirm that username and password are valid */
if($_SESSION['password'] != 'password'){
/* Variables are incorrect, user not logged in */
unset($_SESSION['password']);
return false;
}
return true;
}
/* User not logged in */
else{
return false;
}
}
if(isset($_POST['sublogin'])){
//Set Variables
$password = $_POST['password'];
if($password == 'password'){
$md5pass = md5($password);
$_SESSION['password'] = $md5pass;
if(isset($_POST['remember'])){
setcookie("cookpass", $_SESSION['password'], time()+60*60*24*100, "/");
}
}
}
echo 'Not Logged In';
$logged_in = checkLogin();
?>
形式:
<form id="form1" name="form1" method="post" action="">
Password: <input name="password" type="password" /><br/><br/>
Remember Me: <input type="checkbox" name="remember" id="remember" />
<input name="sublogin" type="submit" value="Login" style="float:right;" />
<br/>
<p></p>
</form>