首页 > 解决方案 > (Mahara) manually set cookies disappear on the next page

问题描述

I'm about to write some code for mahara. I'm trying to store a variable in a cookie. If I do, it will disappear on the next page.

Example:

foo.php:

...
$myfoo = 'bar';
setcookie('mycookie', $myfoo)
var_dump($_COOKIE) 
...

executing foo.php: all the mahara cookies & 'mycookie' is set. Like expected, everything's fine.

bar.php

...
var_dump($_COOKIE) 
...

executing bar.php after foo.php: only mahara standard cookies set, but no 'mycookie'.

I can't really explain that.

Also $_SESSION does not work like intended.

My server is set up correctly, cookies generally work.

Has anyone an idea?

Edit: I see the cookies via var_dump in my foo.php. Even if I stop to set them. They are there. But not on other pages.

标签: phpcookiessession-cookiesmahara

解决方案


<?php
$myfoo = 'bar';
setcookie('mycookie', $myfoo, time() + (86400 * 30), "/"); // 86400 = 1 day
var_dump($_COOKIE);
?>

您的 cookie 过期是因为 指定 cookie 何时过期,因为如果省略过期时间或设置为 0,则 cookie 将在会话结束时(浏览器关闭时)过期。将其更改为某个值,例如 time()+86400*30

更多详情:https ://www.w3schools.com/php/func_network_setcookie.asp


推荐阅读