PHP Cookie 值不回显任何内容


PHP Cookie value not echoing anything

我是第一次测试PHP cookie,根据W3Schools教程,我认为我做的一切都是正确的,但我的cookie的价值不算什么。

这是我的整个表单验证代码,cookie代码正在处理中.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

这是我在索引.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>

进程.php页面正在发送到名为预览的目标 iframe,从表单代码中可以看到。如果选择了饼干,那么我希望它设置饼干,并打印饼干的值,这是饼干怪物的图片。

新的输出是:

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代码。我认为问题是你没有关闭你的输入标签,你的浏览器现在从它发疯了。尝试关闭它,看看会发生什么。