Forum Moderators: coopster

Message Too Old, No Replies

sessions and cookies!

need help on passing variables around

         

guzzi

4:03 pm on May 7, 2004 (gmt 0)

10+ Year Member



i have figured out how to automatically redirect the user to another page, but now i'm stuck again. i want to be able to maintain user information and variables for the duration of the user's "stay" at my site. obviously i have to use sessions and cookies but i have tried everything to it get to work PLEASE help me :(
i dont know where to start, what i'm doing wrong, where to find some good documentation, anything

StupidScript

7:06 pm on May 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Go to: [php.net...]

Read the info in the Sessions and Cookies sections to get a feel for the differences.

Try some tests. Like this one:

<?php
# IF NO COOKIE, yet, AND FORM HASN'T BEEN SUBMITTED
if (!$_COOKIE['testcookie'] &&!$user ) {
#SHOW THE "USER" FORM
print("<form action=\"");
echo $_SERVER["PHP_SELF"];
#THIS WILL PLACE THE VARIABLE "USER" INTO THE URL
print("\">User: <input type=\"text\" name=\"user\">");
print("<input type=\"submit\" value=\"Set Cookie\">");
print("</form>");
}
else {
#STILL NO COOKIE...BUT THE FORM HAS BEEN SUBMITTED
if (!$_COOKIE['testcookie']) {
#MAKE A DISTANT EXPIRATION DATE (SETS THE COOKIE 'FOREVER')
$expdate=mktime(0,0,0,1,1,2038);
#SET THE COOKIE (name of cookie,cookie value,expire date)
setcookie("testcookie",$user,$expdate);
}
#SHOW WHAT THE VALUE OF THE NEW COOKIE IS
print_r($_COOKIE['testcookie']);
}
?>

Save that to a file, upload it to your PHP-enabled web server, and open it in your browser.

At first, you should see the "User" form, where you will enter your user name. When you submit the form, you will come back to the same page which will now display the "value" of the cookie "testcookie" which is set to "expire" Jan. 1, 2038 (the Windows max date).

Have fun!

guzzi

6:20 am on May 12, 2004 (gmt 0)

10+ Year Member



thanx for ur help, i'v figured out what to do already. my problem was that i wasn't extracting data from the database properly. ps for anyone who is trying to figure out how to use a session make sure the session_start() method is called BEFORE any html or php script.