Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cookie Scoop

Status
Not open for further replies.

alsaffar

Programmer
Oct 25, 2001
165
KW
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(&quot;/Users/Cookies.php&quot;);
CheckCookie($HTTP_COOKIE_VARS[GuestSID]);
echo &quot;Welcome&quot;;
?>

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

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

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

Please Help ME.
 
Some problems I'm seeing in your code.

You're issuing a &quot;Location&quot; header in the same page fetch that you are setting your cookie. With what web server are you running PHP? If it's IIS, this won't work.

Is your web client running on the same machine as your web server? If not, the domain of &quot;127.0.0.1&quot; will keep the client from returning a cookie to the server.

Have you tried setting a cookie without the path or domain? Want the best answers? Ask the best questions: TANSTAAFL!
 
Status
Not open for further replies.

Similar threads

Replies
21
Views
638
Replies
2
Views
173
Replies
3
Views
145

Part and Inventory Search

Sponsor

Back
Top