IPB

Welcome Guest ( Log In | Register )



Tags
This content has not been tagged yet
 
Reply to this topicStart new topic

Sessions

, -tutorial-


Guest_ivanc081_*
no avatar

Guests






Post #1 post Mar 11 2005, 09:11 AM
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:
CODE
<?php
echo "Error example</br>";
session_start();
?>


As a result you will get:

Error example
QUOTE
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:

CODE
<?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'.

CODE
<?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.

CODE
<?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.

CODE
<?php
session_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();

CODE
<?php
session_start();
$_SESSION = array();
session_destroy();
?>


Session destroy function just deletes the session files and clears any trace
of 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).

[note=snlildude87]Remember to insert all your codes and errors in the proper BBCode. Thanks.[/note]

This post has been edited by snlildude87: Apr 5 2005, 03:15 PM
Go to the top of the page
+Quote Post
haron
no avatar
Newbie [Level 3]
***
Group: Members
Posts: 46
Joined: 10-March 05
Member No.: 4,343



Post #2 post Mar 11 2005, 10:54 AM
Good!

About Sessions on www.php.net

http://www.php.net/manual/en/features.sessions.php

Cheers.
Go to the top of the page
+Quote Post
karlo
no avatar
Privileged Member
*********
Group: Members
Posts: 624
Joined: 30-October 04
From: Philippines
Member No.: 2,049



Post #3 post Mar 11 2005, 06:56 PM
QUOTE(doom145 @ Mar 12 2005, 01:22 AM)
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
[right][snapback]58808[/snapback][/right]

But, if you need to store information for a long time, use cookies. Like Friendster, they use cookies.
Go to the top of the page
+Quote Post
Mike
no avatar
Owner of Sub-Zero
********
Group: Members
Posts: 159
Joined: 17-November 04
Member No.: 2,325



Post #4 post Mar 18 2005, 10:13 PM
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:

CODE

<?php

if($_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. tongue.gif
Go to the top of the page
+Quote Post
mizako
no avatar
Super Member
*********
Group: Members
Posts: 372
Joined: 16-August 04
From: Spain
Member No.: 824



Post #5 post Jul 10 2005, 06:43 PM
Good introduction to Sessions. I want to programa a shopping cart and i was looking exactly for that kind of simple introduction.Thanks!
Go to the top of the page
+Quote Post
snlildude87
no avatar
Moderator
***************
Group: Members
Posts: 2,325
Joined: 8-March 05
From: Mawson, Antarctica
Member No.: 4,254



Post #6 post Jul 10 2005, 08:06 PM
Hmm, I might use this for my style switcher on my site since I don't like cookies very much...
Go to the top of the page
+Quote Post
itcom
no avatar
Newbie [Level 1]
*
Group: Members
Posts: 23
Joined: 7-November 07
Member No.: 52,597



Post #7 post Dec 14 2007, 02:07 AM
""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.

Go to the top of the page
+Quote Post
hippiman
no avatar
Premium Member
********
Group: Members
Posts: 150
Joined: 9-April 07
From: Nebraska
Member No.: 41,301



Post #8 post Dec 15 2007, 12:15 AM
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.
Go to the top of the page
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   8 jailbox 721 7th February 2005 - 02:54 AM
Last post by: LuciferStar
No New Posts   5 electron 468 3rd July 2006 - 06:52 AM
Last post by: electron
No New Posts   2 fsoftball 1,140 9th May 2005 - 08:09 AM
Last post by: mobious
No New Posts   0 kvarnerexpress 866 23rd July 2005 - 01:21 PM
Last post by: kvarnerexpress
No New Posts   1 apple 506 25th September 2006 - 08:04 AM
Last post by: slu
No New Posts   0 ghostrider 500 28th April 2007 - 02:35 AM
Last post by: ghostrider
No New Posts   0 sinisteredd 318 8th April 2008 - 06:55 PM
Last post by: sinisteredd
No New Posts   1 sonesay 424 21st December 2007 - 11:46 AM
Last post by: shadowx
No New Posts   4 ghostrider 401 10th February 2008 - 04:40 AM
Last post by: FLaKes


 



RSS Open Discussion Time is now: 8th January 2009 - 09:22 AM