在php中获取cookie值

时间:2014-04-15 09:03:31

标签: php cookies

我正在PHP使用此代码设置Cookie

// file1.php
$email = 'abc@xyz.com';
setcookie('songs_email',$email,time() + (86400 * 30));

我在浏览器Cookie中检查过我发现它的名称为songs_email。 但是当我尝试使用此代码检索此cookie时

// file2.php
echo $_COOKIE["songs_email"];

我收到了这些通知。

// file2.php
Notice: Undefined index: songs_email in C:\wamp\www\practise\login.php on line 12

file1.php 中,如果我写echo $_COOKIE['songs_email'],那么我就会获得该值。但是在 file2.php 中,我没有得到它。

可以任意1请告诉我是什么问题。我会感激你的。

伙计们请帮助!!获得了很多观点但没有得到任何解决方案


我在另一台电脑上尝试了相同的代码。它工作正常。但为什么它不能在我的电脑上工作

2 个答案:

答案 0 :(得分:0)

调用setcookie会将其应用于将来的请求,但不会将新Cookie添加到$_COOKIE超全局。你必须自己做。简单明了:$_COOKIE['songs_email'] = $email;但如果你想在当前请求中访问它,它确实需要手动。

答案 1 :(得分:0)

这是传递单引号的解决方案.echo $ _COOKIE ['songs_email'];

你以这种方式传递$ _COOKIE [“songs_email”];像$ _COOKIE ['songs_email'];

这样的单引号传递它