Advanced Search

Results 1 to 8 of 8

Thread: help with the cookie

  1. #1
    Join Date
    Jul 2010
    Location
    NangYang
    Posts
    20
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default help with the cookie

    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
    Last edited by jangkoo; 04-16-2012 at 01:03 PM.

  2. #2
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    243
    Thanks
    1
    Thanked 18 Times in 18 Posts

    Default

    You need to set the third parameter of the set_cookie() for the time the cookie should exist.

  3. #3
    Join Date
    Oct 2011
    Location
    London
    Posts
    41
    Thanks
    19
    Thanked 1 Time in 1 Post

    Default

    Try this i use this site for reference all the time

    http://www.w3schools.com/php/php_cookies.asp

  4. The Following User Says Thank You to TwitterRooms For This Useful Post:

    jangkoo (04-15-2012)

  5. #4
    Join Date
    Jul 2010
    Location
    NangYang
    Posts
    20
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default

    thanks for replying, I tried:
    PHP Code:
    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?

  6. #5
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,623
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    You're skipping the $path parameter in 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 is a fantastic introductory tutorial series.
    • sitepoint is a through, straightforward reference site
    • the MDN is great for intermediate/advanced topics
    • I find quirksmode very useful in figuring out problems (it's not always completely up-to-date, but the info it provides is solid)
    • the W3C is not really aimed at beginners, but they are the standards body - w3schools is not affiliated with the w3c in any way.

    learn more
    Last edited by traq; 04-15-2012 at 05:50 PM.
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

  7. The Following User Says Thank You to traq For This Useful Post:

    jangkoo (04-15-2012)

  8. #6
    Join Date
    Jul 2010
    Location
    NangYang
    Posts
    20
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default

    Thanks traq very much, it works as your method, thanks
    I thought if we don't put the 'path', it will be set to whole domain by default.

  9. #7
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,623
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    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].
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

  10. #8
    Join Date
    Jul 2010
    Location
    NangYang
    Posts
    20
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default

    done editing :d

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •