0

Below are my test files. This is all they contain.

File test.php:

<?php    
setcookie("test", "test", time() + 3600);
header("Location:test2.php");

File test2.php:

<?php
if(!isset($_COOKIE['test'])) {
    echo 'cookie not set';
} else {
    echo 'cookie set';
}

When I try to display the test.php page I got "cookie not set". This is driving me crazy...

Here is what i checked so far:

  • I tested with Safari / Chrome / Firefox,
  • My browsers accept cookies,
  • As you can see in my example there is no output sent to browser before calling setcookie function,
  • When displaying the result of the setcookie function, I got true,
  • it works on my local computer, but not on my server,

Last point let me believe there is something wrong in the server configuration? I'm on a share hosting server so there is not much I can do about that.

PGBI
  • 650
  • 6
  • 18
  • Can you look in your developer tools resources tab and look to see if there are cookies present? – Darren Jan 26 '15 at 23:10
  • 1
    Add error reporting to the top of your file(s) right after your opening ` – Funk Forty Niner Jan 26 '15 at 23:13
  • @Fred-ii- I've added `error_reporting(E_ALL); ini_set('display_errors', 1);` in both files. No error occur. @Darren no cookie set in developer tools as well. – PGBI Jan 26 '15 at 23:16
  • 1
    Try using the domain name, for example `setcookie("TestCookie", $value, time()+3600, "/~rasmus/", "example.com", 1);` as per the manual http://php.net/manual/en/function.setcookie.php - That has happened to me before. – Funk Forty Niner Jan 26 '15 at 23:20
  • Plus add the dot prefix to ensure compatibility with subdomains `if ( substr($domain, 0, 1) != '.' ) $domain = '.'.$domain;` if it's a subdomain. See also http://stackoverflow.com/questions/5258126/domain-set-cookie-for-subdomain – Funk Forty Niner Jan 26 '15 at 23:27
  • I edited the setcookie call : `setcookie("test", "test", time() + 3600, '/', '.mydomain.com', false, false);`. (6th arg set to false because this is not over https). Still no cookie set. – PGBI Jan 26 '15 at 23:33
  • What happens if you do not redirect to the second page automatically, but just call it “manually” after you visited the first one? – CBroe Jan 26 '15 at 23:44
  • What @CBroe said if IIS. http://support.microsoft.com/kb/176113 and PGBI, count the arguments, 6th is secure, not HTTPS only. – ficuscr Jan 26 '15 at 23:46

1 Answers1

0

Finally, I've tried to access my test page with an IP address located in France through a VPN (my host is OVH in France, and i'm living in the US). Cookies are now working.

I guess OVH messes up somewhere with their CDN or I don't know what. I'll check with them directly.

PGBI
  • 650
  • 6
  • 18