View Full Version : Resolved help with the cookie
jangkoo
04-14-2012, 03:25 PM
Hi friends,
I have problem with cookie
in one file a.php I did this:
setcookie("user",$_POST['username']);
then I redirect to another file b.php
try to
echo $_COOKIE["user"];
and it says something like no index "user"
but when I browse to a.php, and
echo $_COOKIE["user"];
it can print the correct one.
So what is wrong with the b.php, anything wrong or?
could you all help me.
thanks in advance
fastsol1
04-15-2012, 12:45 PM
You need to set the third parameter of the set_cookie() for the time the cookie should exist.
TwitterRooms
04-15-2012, 01:02 PM
Try this i use this site for reference all the time
http://www.w3schools.com/php/php_cookies.asp
jangkoo
04-15-2012, 02:25 PM
thanks for replying, I tried:
setcookie("user",$_POST['username'],time()+60*60*24*30);
but the problem is still on
The problem is that :
When I browse to a.php or c.php in the same folder with a.php, I can prrint $_Cookie['user']
but when I navigate to b.php in other folder, I echo $_Cookie['user'] , it produce this :
Notice: Undefined index: user in D:\xampp\htdocs\gui\createGUI.php on line 290
I know that it's because the cookie['user'] is not set, but after that I go back to a.php, I can print it out, that means it's set.
So what 's wrong?
any problem may be?
You're skipping the $path parameter in setcookie() (http://php.net/setcookie). (read up!)
The default value is the directory where the script is located (e.g., if you set a cookie in a script in /some/dir, it will be available in /some/dir or /some/dir/example, but not in /some or /some/other).
Using / will make the cookie available to the whole domain.
-------------------
tangent (re: w3schools):
the information at w3schools is misleading, incomplete, inaccurate, and/or flat wrong in many instances.
there are other, far better, resources for web development:
html,css,javascript from the ground up (http://code.google.com/edu/submissions/html-css-javascript/) is a fantastic introductory tutorial series.
sitepoint (http://reference.sitepoint.com/) is a through, straightforward reference site
the MDN (https://developer.mozilla.org) is great for intermediate/advanced topics
I find quirksmode (http://www.quirksmode.org/) very useful in figuring out problems (it's not always completely up-to-date, but the info it provides is solid)
the W3C (http://www.w3.org/community/webed/wiki/Main_Page) is not really aimed at beginners, but they are the standards body - w3schools is not affiliated with the w3c in any way.
learn more (http://w3fools.com)
jangkoo
04-15-2012, 09:02 PM
Thanks traq very much, it works as your method, thanks :D
I thought if we don't put the 'path', it will be set to whole domain by default.
no prob
If your question has been answered, please mark your thread "resolved":
On your original post (post #1), click [edit], then click [go advanced]. In the "thread prefix" box, select "Resolved". Click [save changes].
jangkoo
04-16-2012, 01:04 PM
done editing :d
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.