[ACCEPTED]-Set a cookie to the entire domain from a page inside a folder-setcookie

Accepted answer
Score: 14

This code definitely works:

setcookie("name", $value, time()+31536000,'/');

You should take 3 note that mydomain.com is different from www.mydomain.com or sub.mydomain.com.

Other 2 than this, take note that the value of $value should 1 be a string.

Score: 1

@SPL_Splinter: Make sure you're setting 10 the cookie prior to any output. So, for example:

<?php
$name = 'Some name';

setcookie("name", $name, time() + 31536000, '/', '.mydomain.com');
if (isset($_COOKIE['name'])) 
{
    echo $_COOKIE['name'];
}
?>

Update

From 9 http://php.net/manual/en/function.setcookie.php

The domain that the cookie is available 8 to. To make the cookie available on all 7 subdomains of example.com (including example.com 6 itself) then you'd set it to '.example.com'. Although 5 some browsers will accept cookies without 4 the initial ., » RFC 2109 requires it to 3 be included. Setting the domain to 'www.example.com' or 2 '.www.example.com' will make the cookie 1 only available in the www subdomain.

More Related questions