繁体   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