Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

PHP sessions

session

  • Please log in to reply
6 replies to this topic

#1 Csabi

Csabi

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 60 posts

Posted 05 December 2010 - 01:48 AM

Sessions can transfer data between two or more pages. It`s somehow like a variable that keep`s it`s value even if you call it from an another file. Let`s say that we have a website and in the index.php file we are asking for the name of the visitor and we store it in the $name variable (e.g.:$name = 'Tom').
If the visitor navigates to an another page the value of $name will be lost, so we would need to ask for the name on each page so in this case is way better to use sessions.

In order to use a session first we need to "start" it:
<?php
session_start();
?>
NOTE: The session_start(); function must be the at the very beginning of your code, even before the <html> tag.

A session variable look`s and works like an element of an associative array:
$_SESSION['name'] = 'Tom';
echo $_SESSION['name'];
And because sessions are keeping their value even if you go to an another page:
page1.php
<?php
session_start();
$_SESSION['name'] = 'Tom';
?>
page2.php
<?php
session_start();
echo $_SESSION['name'] ;
?>
Will output
Tom
Let`s say that we have a page that is displayed only to members. If somebody is logged in than it`s username is stored in a session. In order to show the content of the page to the visitor we need to check if the username session exists or not. This can be easily done using this function:
isset($_SESSION['username']); //Returns True if exists and False if not
So if we use the isset function our code will look like this:
<?php
session_start();
if(isset($_SESSION['username'])){
echo 'Welcome ' . $_SESSION['username'];
}
else{
echo 'You are not logged in!';
}
?>
When the user leaves your website or closes the browser the sessions are automatically deleted, but if you want to delete a session yourself use the following code:
unset($_SESSION['username']);
This is useful when you want to delete only a single session.

If you want to delete all the sessions use the following code:
session_destroy();
NOTE: After this function you can`t use more sessions on the page.

Example of using the session_destroy(); function:
log-out.php
session_start();
session_destroy();
By destroying the sessions the user is logged out.
Here is the original tutorial: php sessions
This is what you need to know about sessions... If you have any questions please ask me! :w00t:

Edited by Csabi, 06 December 2010 - 11:46 AM.

  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#2 renu

renu

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 21 December 2010 - 06:23 AM

can i use session to unlimited pages?
if yes then how?
  • 0

#3 Csabi

Csabi

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 60 posts

Posted 21 December 2010 - 07:09 AM

Yes, you just need to start sessions (session_start(); ) on each page
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#4 oldbrown

oldbrown

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 07 February 2011 - 05:36 PM

I am very new to php, wondering if you could assist. Once a new member registers. Example: Bob registers through the registration form to my site, he then logs in. How do I use the session variable to to allow bob access to "Bobs" page without other registered users access to bobs page? Any assistance would be greatly appreciated
  • 0

#5 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 07 February 2011 - 07:41 PM

How do I use the session variable to to allow bob access to "Bobs" page without other registered users access to bobs page?


On the profile page you can check this quite easily, for example when you access profile.php?profile=bob


if(isset($_GET['profile']) {
//You can check here if the profile exists, I have left this out
if(isset($_SESSION['name']))
if($_GET['profile'] == $_SESSION['name']) {
echo "Welcome to " . htmlspecialchars($_GET['profile']) . "'s page!"
// Then get information for profile
// mysql_query("SELECT info FROM users WHERE name = '" . mysql_real_escape_string($_GET['bob']) . "'";
} else {
echo "You are not " . htmlspecialchars($_GET['profile']) . "." ;
}
} else {
echo "You are not logged in.";
}
} else {
echo "You have not selected a profile.";
}


This is a very simplistic example.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#6 oldbrown

oldbrown

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 07 February 2011 - 08:33 PM

Thanks very much for your rapid reply, it is really appreciated

Oldbrown:w00t:
  • 0

#7 om sa

om sa

    CC Newcomer

  • Just Joined
  • PipPip
  • 11 posts

Posted 08 May 2011 - 02:39 AM

Thanks man, Keep the good work :thumbup:,
  • 0





Also tagged with one or more of these keywords: session

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download