Kyle
Kyle

Reputation: 151

PHP: Can't Set Cookie

For some reason, I can't seem to set a cookie in one of my PHP files. All of the code works fine, except it refuses to set the cookie. I've placed different versions of cookie setting with different arguments, but it doesn't seem to make a difference. On top of that, I can set a cookie using that same line of code in a separate PHP file in the same directory. I've tried placing setcookie() at different places and I still get the same result. Am I missing something?

<?php
$table_name="lfgs";
$name=$_POST['name'];
$event="[";
$level=$_POST['level'];
$comments=$_POST['comments'];
$hours=$_POST['hours']*60*60;
$minutes=$_POST['minutes']*60;
$time=$hours+$minutes+time();

setcookie("remember", $name, $time, 'www.domain.com', '/');

if(isset($_POST['event'])){
    if (is_array($_POST['event'])) {
        foreach($_POST['event'] as $value){
            $event = $event . "\"" . $value . "\",";
        }
    } else {
        $value = $_POST['event'];
        $event = $event . "\"" . $value . "\"]";
    }
} else {
    $event = "";
}

if($event[strlen($event)-1] == ',') {
    $event = substr_replace($event ,"]",-1);
}

$con=mysqli_connect("domain.com","username","password","database");

$req="INSERT INTO $table_name(name, event, level, comments, time) VALUES ('$name', '$event', '$level', '$comments', '$time')";
mysqli_query($con,$req);

mysqli_close($con);

foreach($_COOKIE as $c) {
    echo $c . "<br />";
}
?>

Edit: This is ALL the code for the entire file.

Upvotes: 3

Views: 13587

Answers (5)

Logan
Logan

Reputation: 1

From my experience, if you have any session active on the page it will not allow you to create PHP cookies. Start a new blank page and test it that way.

You should be able to set a cookie on the new generic page. Then go back to your other page that has a session started on it. Echo the details of that set cookie in the session page and you will get the stored value, no problem.

You can call cookies but you can't seem to create them in an active session page. At least, I can't with my current system settings/config.

Upvotes: 0

Gabe
Gabe

Reputation: 83

I dont't understand what you want to do, but it'll not works the way you do. Always remember: you work with PHP on the server side. So to set a cookie an then to test if it worked, you need always tow steps (because you are on the server side): The first step is to set the cookie. And then during the next request you can check if ur cookie contains in the global $_COOKIE array. if yes then ok, if not then mybe the client/user donsen't allow to set cookies.

If you need to do it in "one step", you should use JavaScript. Something like that: On submit the form, set the cookie and then propagate the submit action (send the data to the server). JQuery support a good solution to set and read cookies.

Upvotes: 1

Loic Coenen
Loic Coenen

Reputation: 853

According to the php reference, the correct way to use the setcookie function is

bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )

Didn't you swaped the $path and $domain argument?

Try

setcookie("remember", $name, $time, '/', 'www.domain.com');

Upvotes: 2

Liam Allan
Liam Allan

Reputation: 1115

try

setcookie("remember", $name, '/', $time);

Upvotes: 2

Loic Coenen
Loic Coenen

Reputation: 853

Are you sure the php interpreter doesn't send a char before the setcookie() call for some reason? The function send a HTTP header, so it have to appear before any printing on the page.

Upvotes: 0

Related Questions