iGuest 3 Report post Posted March 11, 2005 (edited) Why Session when I can use Cookies? Because cookies are about 30% unreliable, and that % is rising. Plus more and more browsers are coming with security and privacy settings that do not allow storing of cookies on computers. PHP has a great function(s) that have the same results as cookies and more, plus they are invisible and store information on web server. Session have great capabillity. For example if you want to have a member system on your website, with session you could identify a user, user's level and other. Session start function: session_start() First thing on using sessions: you must avoid errors! You can do that by calling session before anything is output to the browser. Error example: <?php echo "Error example</br>"; session_start(); ?> As a result you will get: Error example Warning: Cannot send session cookie - headers already sent by (output started at c:\apache\htdocs\session_error.php:2) in c:\apache\htdocs\session_error.php on line 3 Warning: Cannot send session cache limiter - headers already sent (output started at c:\apache\htdocs\session_error.php:2) in c:\apache\htdocs\session_error.php on line 3 No errors example: <?php session_start(); echo "No errors here :)"; ?> Next step is to register a session variable.When we started a session, we would use a php to start storing information about user. I used a form to post a variable 'name'. <?php session_start(); // get the variable name from the form $name = $_POST['name']; // register session key with the value $_SESSION['name'] = $name; ?> In code above i assigned $name to session key 'name' ( $_SESSION['name'] = $name; ) Another way to assign that value is: $_SESSION['name'] = $_POST['name']; Displaying the results. <?php session_start();?>Welcome to members area <? echo $_SESSION['name']; ?> This is pretty simple, we started session, and echoed a result. Unregistering variables. If we want to remove session variable 'name', we would use this code. <?phpsession_start(); $_SESSION['name'] = FALSE; ?> We cleared session variable 'name' ( $_SESSION['name'] = FALSE; ), and it does not exist any more, unless we register it again. You can check if it's exist by using echo (explained earlier). Destroying a Session. Session destroy function: session_destroy(); <?php session_start(); $_SESSION = array(); session_destroy(); ?> Session destroy function just deletes the session files and clears any traceof that session. You can use this f. if you have a log in script you will need also log out script. Thats all. I hope this tutorial would be useful for you. REMEMBER: In every script where you wanna use a session variables, you must use a session start function !!! I am sorry if you find any errors because i wrote this from my head, and if my english is bad (not my mother tonque). Notice from snlildude87: Remember to insert all your codes and errors in the proper BBCode. Thanks. Edited April 5, 2005 by snlildude87 (see edit history) Share this post Link to post Share on other sites
haron 0 Report post Posted March 11, 2005 Good! About Sessions on http://php.net/ http://php.net/manual/en/features.sessions.php Cheers. Share this post Link to post Share on other sites
karlo 0 Report post Posted March 11, 2005 Well.....Overall I gave this tutorial a 9/10 as it explains all the things needed to know about basic php....i am aware of php allready but i think this could really help members that are trying to set up a proffessional site and want it to be in php format. You could hv added more info as atm it looks very blank and less explaining about what to do....but i really think this is a good tutorial for n0bs and people like that who are just looking to start up a basic php website 58808[/snapback] But, if you need to store information for a long time, use cookies. Like Friendster, they use cookies. Share this post Link to post Share on other sites
Mike 0 Report post Posted March 18, 2005 Nice tutorial, dude. I knew how to do this already, but it was very informative about how to do it, for people that didn't know how. I'm writing my own message board source code and I'm using sessions rather than cookies. But I have a question.. If I wanted something to show up only if the $_SESSION['username'] is TRUE, then would it look like this: <?phpif($_SESSION['username'] = TRUE){echo '<a href="javascript: history( go -1 )">Back</a>';} elseif($_SESSION['username'] = FALSE){echo '<b>Log in plz</b>';}?> ?Of course, that is not what I'm putting on the site, that's just an example. Share this post Link to post Share on other sites
mizako 0 Report post Posted July 10, 2005 Good introduction to Sessions. I want to programa a shopping cart and i was looking exactly for that kind of simple introduction.Thanks! Share this post Link to post Share on other sites
snlildude87 0 Report post Posted July 10, 2005 Hmm, I might use this for my style switcher on my site since I don't like cookies very much... Share this post Link to post Share on other sites
itcom 0 Report post Posted December 14, 2007 ""SESSION fail to store"" Hi, I use Frameset and there are two files: one is Header and another one is Body. When I put the following coding in the Header file, it shows "1" <?php if(isset($_SESSION['user_id'])) { $_SESSION['user_id'] = $_SESSION['user_id'] + 1; } else {$_SESSION['user_id'] = 1;} echo "session id set up is =" . $_SESSION['user_id']; ?> Output = " session id set up is =1 " But, when I put the following code in the Body, it does not show anything <?php $cid = $_SESSION['user_id']; echo "Session ID is = " . $cid; ?> Output = "Session ID is = " Why doesn't it show? How could I get Session? I would like to create a shopping basket (cart). I was looking for the codings but most of them have very long coding and I could not understand them. So, I want to make on my own. Could you please help me? Thanks ahead. Share this post Link to post Share on other sites
hippiman 0 Report post Posted December 15, 2007 That's pretty cool. I've always used cookies, and I didn't know there was any other way to do it, unless you used a database or something, and I don't think using a database to see if a user is logged on is very smart.But when do sessions end? I saw that there was a way to do it with the PHP, but does the session end when you close the browser, or can you set a time limit or something? I'll try and look it up.But that sounds really useful. I think I can use that for this PHP project we're doing at school. Share this post Link to post Share on other sites