|
-
May 11th, 2005, 11:57 AM
#1
Thread Starter
Frenzied Member
Set and View Cookies
I'm am having some trouble with cookies.
I can set the cookie and view it on the same page, but I have not been able to get another page (in the same directory) to access the cookie.
Here is my code:
PHP Code:
<?php
$c_data = "[Your Name]";
if (setcookie("cookietest", $c_data, 0, "/", "", 0) == FALSE)
{
print "There was an error while attempting to set the cookie.";
exit(0);
}
else
{
print "The cookie was set to $c_data successfully.<br>";
if (isset($_COOKIE["cookietest"]))
{
$c_data2 = $_COOKIE["cookietest"];
print "I see the cookie as being set to $c_data2.";
}
print "Click <a href=\"test.php\">HERE</a> to test the cookie on another page.<br>";
exit(0);
}
?>
and here is the code on the test.php page
PHP Code:
<?
if (isset($_COOKIE['cookietest']))
{
print "Are you " . $_COOKIE['cookietest'] . "?";
}
else
{
print "The cookie could not be found.";
}
exit (0);
?>
Any ideas on why this is not working?
Thanks,
Squirrelly1
Now happily married and still crankin' away at the keyboard.  Life is grand for a coder, no?
-
May 11th, 2005, 12:40 PM
#2
Re: Set and View Cookies
Try using sessions, they may be better in this case 
I have had simmilar problems with cookies in the past - they yusually involved the directory in which they are set and the reference to that directory but that code seems to be correct.
Try sessions and see if they work, they are just as easy 
http://www.php.net/sessions
Cheers,
RyanJ
-
May 11th, 2005, 01:27 PM
#3
Thread Starter
Frenzied Member
Re: Set and View Cookies
Well, the code I need to be setting the cookie/session in is not my own and it seems to already have a session started.
Sessions terminate when the browser closes, don't they?
and do they carry on if I were to have a link that opened another browser window?
Squirrelly1
Now happily married and still crankin' away at the keyboard.  Life is grand for a coder, no?
-
May 11th, 2005, 01:35 PM
#4
Re: Set and View Cookies
Actually I think they are terminated when the user's internet connection is reset e.g. when they disconnect and then reconnect.
Cheers,
Ryanj
-
May 11th, 2005, 01:39 PM
#5
Thread Starter
Frenzied Member
Re: Set and View Cookies
I found some code on the net and basically... the sesson won't last between browsers unless I use the SID... so as long as I have that in my link i'll be ok
but what about having multiple sessions inside of the same page?
Squirrelly1
Now happily married and still crankin' away at the keyboard.  Life is grand for a coder, no?
-
May 11th, 2005, 01:44 PM
#6
Re: Set and View Cookies
 Originally Posted by squirrelly1
I found some code on the net and basically... the sesson won't last between browsers unless I use the SID... so as long as I have that in my link i'll be ok
but what about having multiple sessions inside of the same page?
Squirrelly1
You can do that the same way you can have multiple cookies 
Have a look at these:
http://uk2.php.net/session
http://www.oreilly.com/catalog/learn...apter/ch08.pdf
Cheers,
RyanJ
-
May 11th, 2005, 01:53 PM
#7
Thread Starter
Frenzied Member
Re: Set and View Cookies
yep... i got it I just had to use session_name("whatever")
i'm gonna play with this for a little while... so much easier than cookies!
Squirrelly1
Now happily married and still crankin' away at the keyboard.  Life is grand for a coder, no?
-
May 11th, 2005, 02:37 PM
#8
Re: Set and View Cookies
 Originally Posted by squirrelly1
yep... i got it  I just had to use session_name("whatever")
i'm gonna play with this for a little while... so much easier than cookies!
Squirrelly1
Yea, I agree 100% with that, cookies cna cause too many problems...
Cheers,
RyanJ
-
May 11th, 2005, 04:24 PM
#9
Re: Set and View Cookies
Just to clarify, the PHP session module sets a cookie on the client machine by default when the session_start() function is invoked. The difference between this cookie and the cookie you are/were trying to set, is that it only stores a session identifier.
The session identifier is sent to your scripts and PHP uses this to get the session data, which is stored on the server and populate the $_SESSION array. This array can contain as many or as few varaibles as you wish. By default the session using cookies only lasts for the current browsing session, you can change this by modifying the session.cookie_lifetime directive.
When to use sessions instead of cookies?
- You want to store lots of different pieces of data, which would otherwise require multiple / large cookies.
- You want to store a large amount of data which may put you over the quota per domain allocated to cookies.
- You want to keep track of personal / sensitive data pertaining to the user. (as sessions are stored on the server, this data never gets stored on the client as a cookie)
If you find yourself storing only a small piece of information then I would actually recommend a cookie, conversly, if you want to store data for a long time such as user details I would recommend a database.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|