用戶登錄後(在login.php中),它們被重定向到control.php。如果他們關閉瀏覽器並重新打開control.php,他們會被要求重新提交表單。如何使用我設置的cookie,而不是重新提交表單?每次頁面重新加載時,表單都會重新提交
的login.php
<?php session_start(); /* Starts the session */
$logins = array('username' => 'pass12');
if(isset($_COOKIE['userME']) && isset($_COOKIE['passME'])){ //if cookie is set do this
$Username=$_COOKIE['userME'];
$Password=$_COOKIE['passME'];
echo $Username.'-'.$Password;
if (isset($logins[$Username]) && $logins[$Username] == $Password){
/* Success: Set session variables and redirect to Protected page */
$_SESSION['UserData']['Username']=$logins[$Username];
header("location:control.php");
exit;
} else {
/*Unsuccessful attempt: Set error message */
$msg="<span style='color:red'>1Invalid Login Details</span>";
}
}
else{ //else new person
/* Check Login form submitted */
if(isset($_POST['Submit'])){
/* Check and assign submitted Username and Password to new variable */
$Username = isset($_POST['Username']) ? $_POST['Username'] : '';
$Password = isset($_POST['Password']) ? $_POST['Password'] : '';
/* Check Username and Password existence in defined array */
if (isset($logins[$Username]) && $logins[$Username] == $Password){
/* Success: Set session variables and redirect to Protected page */
$_SESSION['UserData']['Username']=$logins[$Username];
setcookie('userME',$Username,time()+60*60*24*10,'/','71.12.145.29');
setcookie('passME',$Password,time()+60*60*24*10,'/','71.12.145.29');
header("location:control.php");
exit;
} else {
/*Unsuccessful attempt: Set error message */
$msg="<span style='color:red'>2Invalid Login Details:<?php echo $Username.'-'.$Password?></span>";
}
}
}
?>
<form action="" method="post" name="Login_Form">
<table width="400" border="0" align="center" cellpadding="5" cellspacing="1" class="Table">
<?php if(isset($msg)){?>
<tr>
<td colspan="2" align="center" valign="top"><?php echo $msg;?></td>
</tr>
<?php } ?>
<tr>
<td colspan="2" align="left" valign="top"><h3>Login</h3></td>
</tr>
<tr>
<td align="right" valign="top">Username</td>
<td><input name="Username" type="text" class="Input"></td>
</tr>
<tr>
<td align="right">Password</td>
<td><input name="Password" type="password" class="Input"></td>
</tr>
<tr>
<td> </td>
<td><input name="Submit" type="submit" value="Login" class="Button3"></td>
</tr>
</table>
</form>
control.php
<?php
session_start();
echo "Hello, ".$_COOKIE['userME'].'<br>';
if(isset($_SESSION['UserData']['Username']))
{
if (isset($_POST['submit'])) {
switch ($_POST['submit']) {
case 'room_light':
//execute code here
break;
case 'blink':
echo shell_exec("sudo ruby /home/pi/Desktop/PiControl/blinkPin1.rb");
break;
}
}
?>
<form method="post">
<input type="submit" name="submit" value="room_light">
</form>
<?php
}
else
{
header("location:login.php");
}
?>
在control.php中,爲什麼不使用'if(isset($ _ COOKIE ['userME'])&& isset($ _ COOKIE ['passME'])){// do something}'因爲您已經擁有了它文件;你嘗試過嗎?這應該工作。 –
另外,在cookies中存儲密碼不是一個好主意。 http://stackoverflow.com/q/2100356/ --- http://stackoverflow.com/q/3928630/ --- http://security.stackexchange.com/questions/9455/is-it-safe- to-store-the-password-hash-in-a-cookie-and-use-it-for-remember-me- –
這就是把它放得很溫和* Ralph *。 @ Fred-ii-哎呀! –