Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Sessions, -tutorial-
ivanc081
post Mar 11 2005, 09:11 AM
Post #1





Guests






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).

Notice from snlildude87:
Remember to insert all your codes and errors in the proper BBCode. Thanks.


This post has been edited by snlildude87: Apr 5 2005, 03:15 PM
Go to the top of the page
 
+Quote Post
haron
post Mar 11 2005, 10:54 AM
Post #2


Newbie [Level 3]
***

Group: Members
Posts: 46
Joined: 10-March 05
Member No.: 4,343



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
post Mar 11 2005, 06:56 PM
Post #3


Privileged Member
*********

Group: Members
Posts: 622
Joined: 30-October 04
From: Philippines
Member No.: 2,049



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
*


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


Owner of Sub-Zero
********

Group: Members
Posts: 159
Joined: 17-November 04
Member No.: 2,325



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
post Jul 10 2005, 06:43 PM
Post #5


Super Member
*********

Group: Members
Posts: 372
Joined: 16-August 04
From: Spain
Member No.: 824



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
post Jul 10 2005, 08:06 PM
Post #6


Moderator
***************

Group: Members
Posts: 2,325
Joined: 8-March 05
From: Mawson, Antarctica
Member No.: 4,254



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


Newbie [Level 1]
*

Group: Members
Posts: 23
Joined: 7-November 07
Member No.: 52,597



""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
post Dec 15 2007, 12:15 AM
Post #8


Premium Member
********

Group: Members
Posts: 150
Joined: 9-April 07
From: Nebraska
Member No.: 41,301



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

Topics Topics
  1. Sessions And Our First Project(0)


 



-