2014-02-17 214 views
0

我討厭這麼說,但我一直在努力完成30分鐘的任務,現在只需要6個小時,幾乎沒有任何進展。我試圖在表單中捕獲名稱和電子郵件,並將它們設置爲可持續10分鐘的cookie。當cookies處於活動狀態時,頁面應跳過該表單並僅顯示輸入。我已經嘗試了這兩個cookie和會話,不能讓它工作。 在這一點上,我已經寫了並刪除了至少一百行代碼,卻無法真正看到問題所在。這是我第一次使用PHP。任何幫助,將不勝感激。 當前這段代碼創建表單,獲取信息並正確地將其發佈到頁面。當我回到頁面時,它再次顯示錶單。我認爲這意味着cookie不會設置/粘貼。PHP cookie設置

所有的
<?php 
if (!empty($_POST)) { 
    setcookie('Cname',$_POST['name'], time()+600); 
    setcookie('Cemail', $_POST['email'], time()+600); 
// header("Location:HW2.php"); 
} 

?> 
<html> 
<head> 
<title> Assignment 2 Alcausin </title> 
</head> 
<body> 

<?php 

error_reporting(E_ALL); 
ini_set('display_errors', TRUE); 
ini_set('display_startup_errors', TRUE); 

$visibleForm = True; 
if(isset($_COOKIE['name'])){ 
    $visibleForm = False; 
} 
if(isset($_POST['submit'])){ 
    $visibleForm = False; 
    echo "Your Name: "; 
    echo $_COOKIE['Cname']; 
    echo "<br>"; 
    echo "Your Email: "; 
    echo $_COOKIE['Cemail']; 
} 
if($visibleForm){ // close php if form is displayed 
    ?>  
<form action ="HW2.php" method="post"> 
    Name:<font color = red>*</font> <input type="text" name="name"><br> 
    E-mail:<font color = red>*</font> <input type="text" name="email"><br> 
    <input type="submit" name="submit" value="Submit"> 
</form> 
<?php // back to php 
} 
?> 

</body> 
</html> 
+0

附加在session_start()到你的代碼的頂部除了該開口,PHP標籤先天下之憂 – Bryan

回答

1

我使用sessions重寫了腳本,以便您的數據實際存儲在服務器上,並且客戶端只有一個會話cookie,它是對服務器端數據的引用,所以客戶端無法篡改數據。

雖然這可能對您的作業並不重要,但在處理用戶帳戶和權限時(設想一個「管理員」cookie來告知用戶是否爲管理員 - 任何人都可以手動設置該cookie以及就是這樣,他是你網站上的管理員)。

這沒有經過測試,可能根本無法工作 - 隨時downvote我的答案,如果是這樣的話。

<?php 

error_reporting(E_ALL); 
ini_set('display_errors', TRUE); 
ini_set('display_startup_errors', TRUE); 
ini_set("session.cookie_lifetime","600"); // sets the session cookie's lifetime to 10 minutes/600 seconds 

session_start(); // starts the session, this will create a new session cookie on the client if there's not one already 

if (isset($_POST["name"]) && isset($_POST["email"])) { // if there's POST data 
    $_SESSION["name"] = $_POST["name"]; // this saves your values to the session so you can retrieve them later 
    $_SESSION["email"] = $_POST["email"]; // same here 
}; 

?> 
<html> 
<head> 
<title> Assignment 2 Alcausin </title> 
</head> 
<body> 
<?php 

$visibleForm = !isset($_SESSION["name"]); // visibleForm will be the opposite of isset, so if there's a "name" in the session then the form will be invisible 

if ($visibleForm) { // if there's no session data, we display the form 
    echo '<form action ="HW2.php" method="post">Name:<font color = red>*</font> <input type="text" name="name"><br>E-mail:<font color = red>*</font> <input type="text" name="email"><br><input type="submit" name="submit" value="Submit"></form>'; 
} else { // this means there is some data in the session and we display that instead of the form 
    echo "Your Name: "; 
    echo $_SESSION["name"]; 
    echo "<br>"; 
    echo "Your Email: "; 
    echo $_SESSION["email"]; 
}; 
?> 

</body> 
</html> 
0

首先,您必須是有必要的任何的這個工作添加在session_start()在你的代碼的最高水平。 session_start()實際上生成PHPSESSID cookie並且也是會話標識符;如果使用session_start(),則不需要使用setcookie()將任何內容設置爲PHPSESSID cookie。

對於一個基本的方法來做你想要達到的目的,我會嘗試設置會話,每當頁面加載,如果有一個當前會話,那麼它會跳過你說的形式。

$_SESSION['SESSID'] = $someVar; 
$_SESSION['SESSNAME'] = "someOtherVar"; 

那麼你的表格前,右,檢查是否有那些通過使用

if(isset($someVar) && isset($someOtherVar)) 

你知道這筆交易設定。

然後創建一個按鈕,執行session_destroy()以便它結束當前會話。