简体   繁体   中英

PHP Cookie value not echoing anything

I am testing out PHP cookies for the first time and according to the W3Schools tutorial I thought I was doing everything right but the value of my cookie is nothing.

Here is my whole form validation code which the cookie code is in in 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

Here is my form code which is on the index.php page:

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

The process.php page is being sent to a target iframe named preview as you can see from the form code. If the cookie is selected then I want it to set the cookie, and print the value of the cookie which is a picture of cookie monster.

The new output is :

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. 

What it looks like to me is that it's not acessing $_POST['set'] variable right, so it doesn't execute the cookie code. I think the problem is that you didn't close your input tags, and your browser is now going insane from it. Try closing it and see what happens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM