2012-12-18 109 views
-1

我有一個名爲signin.php的登錄頁面,用戶可以在其中輸入電子郵件和密碼。點擊提交按鈕,頁面指向connection_validate.php。此頁面用數據庫驗證用戶輸入的數據。如果是註冊用戶,則該頁面指向calendar.php。如果輸入的數據不正確,它應該重定向到signin.php。如果輸入的數據是不正確的,我已經把餅乾這樣的:以編程方式清除cookie

//action to be done if e mail id and password matches with database records    
if(mysql_num_rows($result)>0) 
{ 
    header('location:calendar.php'); 
} 
//action to be done if e mail id and password does not matches with database records 
else 
{ 
    setcookie('message','incorrect login data'); 
    header('location:signin.php'); 
} 

在signin.php,我已經寫了顯示警告的代碼,如果登錄信息不正確,這樣的:

<?php 
include("include/minfooter.php"); 
if(isset($_COOKIE["message"])) 
{ 
    if(!($_COOKIE["message"]==" ")) 
    { 
     echo "<script> 
    alert('Incorrect login information'); 
    </script>"; 
    setcookie("message"," ",time()-3600); 
    } 
} 
?> 

我的問題是,如果我一次輸入了錯誤登錄數據,每次加載登錄頁面時都會顯示警報。如果我也按下從calendar.php的後退按鈕signin.php,警報開始顯示。我瞭解到問題出在cookie上。 Cookie尚未刪除。我該如何解決這個問題。

回答

1

更新您的signin.php如果您正在使用一個會話,你可以使用$ _SESSION變量,而不是一個cookie值如下

<?php 
    include("include/minfooter.php"); 
    if (isset($_COOKIE["message"])) 
    { 

     echo "<script> 
       var delete_cookie = function(name) { 
        document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;'; 
       }; 
       var msg = '" . $_COOKIE["message"] . "'; 
       if (msg != '') 
        alert('Incorrect login information'); 
       delete_cookie('message'); 
      </script>"; 
    } 
?> 
+0

這是錯誤的,setcookie()設置一個HTTP頭,在你已經輸出內容到瀏覽器後不能放置。你的語法也是錯的。 –

+0

更新了我的答案,現在它的100%正確 – user1911857

+0

我說75%是正確的。你有if(isset($ _ COOKIE [「message」]))兩次,但它仍然不是正確的方法。 –

1

。你也不能使用setcookie()因爲你有輸出內容,因爲setcookie()會發送一個HTTP頭,在發送任何內容之前必須發送這個頭。

session_start(); 
//action to be done if email id and password matches with database records 
if (mysql_num_rows($result) > 0) 
{ 
    header('Location: calendar.php'); 
    exit; 
} 
//action to be done if email id and password does not matches with database records 
else 
{ 
    $_SESSION['message'] = 'incorrect login data'; 
    header('Location: signin.php'); 
    exit; 
} 

然後:

<?php 

session_start(); 
include("include/minfooter.php"); 

if (!empty($_SESSION['message'])) 
{ 
    echo "<script>alert('" . $_SESSION["message"] . "');</script>"; 
    $_SESSION['message'] = ''; 
} 

?> 
0

好吧,也許是更好地爲使用一個索引[「消息」] $ _SESSION數組上使用會話,然後清理,餅乾,應當你要使用在用戶離開你的頁面之後引用一些信息。我在你的代碼上使用cookies,但考慮使用會話:

<?php include("include/minfooter.php"); 
     if(isset($_COOKIE["message"]) && !empty($_COOKIE["message"]) 
     { 
       echo "<script> 
        var msg = '<?php echo $_COOKIE["message"];?>'; 
        if (msg != "") 
        alert('Incorrect login information'); 
        </script>"; 
       unset($_COOKIE["message"]); 
     } 
?> 
相關問題