簡體   English   中英

PHP Cookie值不回應任何東西

[英]PHP Cookie value not echoing anything

我是第一次測試PHP cookie,根據W3Schools教程,我認為我做的一切正確,但我的cookie的價值是什么。

這是我的整個表單驗證代碼,cookie代碼位於process.php中:

function validate_post_form() {
    global $postMsg;

    $valid = true;
    if ( $_POST['course'] == null || $_POST['course'] == "") {
        $postMsg .= " You must choose a course.";
        $valid = false;
    }
    if ( $_POST['convert'] == null || $_POST['convert'] == "") {
        $postMsg .= " You must choose to convert the text or not.";
        $valid = false;
    }
    if ( $_POST['input'] == null || $_POST['input'] == "") {
        $postMsg .= " You must enter an text input to process.";
        $valid = false;
    } 

    $set = $_POST['set'];

    // check if cookies or session radio button is set
    if ($set == "cookie") {
        $value = "<img src=\"../BENSON_Cookies/PleaseNoDeleteMyCookies.JPG\" />";
        $cookie = setcookie("Cookie", $value, time()+3600);
    } else if ($set == "session") {
        echo "You have selected session.";
    }

    if ($cookie) {
        echo "The cookie is set.";
    } else {
        echo "The cookie is not set.";
    }

    echo $postMsg;
    return $valid;
} // End validate_post_form() function

這是我在index.php頁面上的表單代碼:

<form action="process.php" method="post" target="preview">
Set: <input type="radio" name="set" value="cookie">Cookies
    <input type="radio" name="set" value="session">Session<br>
    <span style="color:red;">*</span>Course: 
            <select name="course" class="form-control">
                <option value="">Choose:</option>
                <option value="HTML">HTML</option>
                <option value="CSS">CSS</option>
                <option value="PHP">PHP</option>
                <option value="JavaScript">JavaScript</option>
            </select><br>
    <span style="color:red;">*</span>Convert Text? 
            <input type="radio" name="convert" value="Yes">Yes
            <input type="radio" name="convert" value="No">No<br>
    <span style="color:red;">*</span>Text Input:<br>
        <textarea class="form-control" name="input" placeholder="Input text here."></textarea><br>
    <button class="btn btn-default" type="submit">Submit</button>
</form>

正如您從表單代碼中看到的那樣,process.php頁面將被發送到目標iframe命名預覽。 如果選擇了cookie,那么我希望它設置cookie,並打印cookie的值,這是cookie怪物的圖片。

新的輸出是:

The cookie is not set.
    You cannot see the cookie set until the script executes and then if you put 
     the println in the next page , you should be able to see it.

Here is the setcookie example :
$cookiename ='mycookie';
$value="mytestvalue";
$expiry=time()+3600;
$path="/";
$domain="my.domain.name";
$secure=true;
$httponly=true;
setcookie($cookiename,$value,$expiry,$path,$domain,$secure,$httponly);


 Use the following to access the cookie :
     $mycookie = $_COOKIE["mycookie"];

 Another easier way to check the cookies is to use the firebug tool on 
 firefox or plugins like editthiscookie on chrome. 

對我來說,它看起來不是正確的$ _POST ['set']變量,所以它不會執行cookie代碼。 我認為問題是你沒有關閉你的輸入標簽,你的瀏覽器現在正在瘋狂。 嘗試關閉它,看看會發生什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM