SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: Cookie Scoop

  1. #1
    SitePoint Enthusiast alsaffar's Avatar
    Join Date
    Aug 2002
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cookie Scoop

    Hi there,

    I want to check if the cookie is set or not, because as its mentioned in the manual:



    so I have the following scenario:

    In the index page I check if a cookie is set or not, if its not set, just set a new cookie and then redirect to a page called "TestCookie.php" to test the cookie if it was set successfully or not, but the problem is: Im always after testing the cookie it gave me that its not set, so where is the error in my scripts.

    Common Pitfalls:

    Cookies will not become visible until the next loading of a page that the cookie should be visible for.

    Note: I noticed that if the index.php is in the same directory with TestCookie.php and Cookies.php it works fine, but if index.php is in an upper directory, it will not work! Is the problem with cookie's path? Although I set it "/" to be at the home directory to be viewable to all directories.

    Here is my scripts:

    Note that index.php at the root while (TestCookie.php and Cookies.php) in the "User" directory under the root.

    index.php:
    ------------
    <?
    include("/Users/Cookies.php");
    CheckCookie($HTTP_COOKIE_VARS[GuestSID]);
    echo "Welcome";
    ?>

    TestCookie.php:
    -------------------
    <?
    $GuestSID = $HTTP_COOKIE_VARS[GuestSID];
    if (!isset($GuestSID))
    {
    echo "Not Set";
    }
    else
    {
    Header ("Location: /index.php");
    }
    ?>

    Cookies.php:
    ---------------
    <?
    Function CheckCookie($cookieGuestSID)
    {
    if (!isset($cookieGuestSID))
    {
    SetNewCookie();
    Header ("Location: /Users/TestCookie.php");
    }
    }

    Function SetNewCookie()
    {
    $GuestSID = mt_rand();
    setcookie ("GuestSID", $GuestSID,time()+900, "/", ".127.0.0.1", 0);
    }
    ?>

    Please Help ME.
    HaVe a GooD DaY

    @li @l$@ff@R

  2. #2
    SitePoint Addict mr tinkles's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    talk about spaghetti code, try an easier example...

    as in:

    index.php, checks/sets cookie, has link to doescookieexist.php that checks for cookie?

    cookies being header info and using header to redirect is problematic, and what do you gain by this?


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
  •