Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP-cookie not saving

I'm currently implementing some stuff with session & cookies, but the cookies aren't stored on my computer. Accordingly the session could not be retrieved.

session_start();        // returns true
var_dump($_SESSION);    // array(0) {}
var_dump($_COOKIE);     // array(0) {}
$_SESSION['test'] = 5;
setcookie('aaa', '111', strtotime('+30 days'));
die;

Reloading the page should display non-empty arrays. But they are always empty. Also there is no cookie displayed on the cookies-tab in Firebug. I use another domain on the same server that works fine with sessions.

Any ideas?

like image 995
user1941613 Avatar asked Oct 24 '25 10:10

user1941613


1 Answers

Be sure not to have any output before calling any functions that make use of headers. So try this instead:

<?php
session_start();
$_SESSION['test'] = 5;
setcookie('aaa', '111', strtotime('+30 days'));
var_dump($_SESSION);
var_dump($_COOKIE);
?>
like image 119
Jeffrey Avatar answered Oct 27 '25 00:10

Jeffrey