Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP setcookie() not working

Tags:

php

cookies

On one page I have something like this

setcookie('user', 'value' ,6000, '/', 'mydomain.co.uk');

On the subsequent page I have

var_dump($_COOKIE);

I can see all the automatically generated ones, like PHPSESSID but I cannot see user.

If I do echo setcookie('user', 'value' ,6000, '/', 'mydomain.co.uk'); it returns true. So I'm not sure why I can't see it.

I have tried a lot of different ideas, but nothing has worked. Also, I have using .htaccess to redirect all requests via one page index.php not sure if this is doing anything.

like image 877
Alex Avatar asked Mar 13 '12 19:03

Alex


People also ask

Why is Setcookie not working PHP?

The PHP isset() checks whether a cookie is set. Reminder: if you detect PHP setcookie not working, make sure it appears before the <html> element in your code, and that the set path parameter is correct.

What is Setcookie function in PHP?

The setcookie() function defines a cookie to be sent along with the rest of the HTTP headers. A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too.

How set multiple values in cookie in PHP?

php'; function setCookieData($arr) { $cookiedata = getAllCookieData(); if ($cookiedata == null) { $cookiedata = array(); } foreach ($arr as $name => $value) { $cookiedata[$name] = $value; } setcookie('cookiedata', serialize($cookiedata), time() + 30*24*60*60); } function getAllCookieData() { if (isset($_COOKIE[' ...

Where are cookies set in PHP?

To create cookies in PHP, you need to use the setcookie function. Let's have a look at the basic syntax which is used to create a cookie. setcookie ( string $name , string $value = "" , int $expires = 0 , string $path = "" , string $domain = "" , bool $secure = false , bool $httponly = false );


1 Answers

Try this:

setcookie('user', 'value' ,time() + 6000, '/', 'mydomain.co.uk');

The expires Parameter needs to be a timestamp. 6000 as a timestamp is in the past and therefore removes the cookie.

like image 102
js-coder Avatar answered Oct 13 '22 18:10

js-coder