2014-01-22 69 views
0

以下是我的登錄PHP代碼。如何設置cookie以使瀏覽器保持用戶登錄6個月,即使他們關閉了瀏覽器?設置cookie以保持用戶登錄六個月

if (!isLoggedIn()) 
{ 
    // user is not logged in. 
    if (isset($_POST['cmdlogin'])) 
    { 
     // retrieve the username and password sent from login form & check the login. 
     if (checkLogin($_POST['username'], $_POST['password'])) 
     { 
      show_userbox(); 
+0

這裏我的評論,你做了什麼? –

+0

$ inTwoMonths = 60 * 60 * 24 * 60 + time(); setcookie('cmdlogin','cmdlogin',$ inTwoMonths);並且它不起作用 – John

回答

1
// set cookie 
// setcookie(name,value,expire); 

$expire = 6*30*24*3600; 
setcookie('username',_POST['username'], $expire); 
setcookie('password',_POST['password'],$expire); 

// get cookie 
$_COOKIE['username']; 
$_COOKIE['password']; 
+0

'@ user3221728',返回一次,然後縮進4個空格以顯示代碼。它是'$ _POST',而不是'_POST'。 – PHPglue