+ Reply to Thread
Results 1 to 9 of 9

Thread: Sessions in PHP

  1. #1
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Sessions in PHP

    Sessions is a good way for storing temporary data between your pages. A session is usually closed pretty fast after that the user have left the site so as I said, it's good for temporary storage, an example is the user's name or items the user have put in his/her shopping cart.




    Starting a Session

    To use a session you first have to start it, the session must be started before anything is sent to the page so you can't have any html tags before you starts the session. To start a new session you do like this:

    Code:
    <?php
    session_start
    ();
    ?>

    But since there's no point in starting a session that is already started we can first check if there's any sessions:

    Code:
    <?php 
    if(!isset($_SESSION))
    {
    session_start();
    }
    ?>





    Session variables

    The data stored in a session is stored in different session variables, to read or write from/to a session variable you use $_SESSION['']; together with the name of the session.


    An example on writing:

    Code:
    <?php 
    $_SESSION
    ['username'] = "Me"
    ?>

    An example on reading:

    Code:
    <?php 
    echo "Hello " $_SESSION['username'] . "!"
    ?>

    If you want to check if a session variable has been set you only use isset on the session variable, like so:

    Code:
    <?php 
    if (isset($_SESSION['username'])) {
    echo 
    "The session variable called username as the value " $_SESSION['username'];
    }else{
    echo 
    "The session variable called username haven't been set yet.";
    }
     
    ?>




    Remove session variables

    You can also delete a session variable and all its info by using unset. Make sure this session variable exists first since there no point trying to remove anything that doesn't exists. Remember that this info will be lost forever. Here comes an example on how you can use unset after you've checked if the session variable exists:


    Code:
    <?php

    if (isset($_SESSION['username'])) {
    echo 
    "Bye " $_SESSION['username'];
    unset(
    $_SESSION['username']);
    }

    ?>


    Destroying your session

    If you're completely finished with the whole session you can remove it. This will make all your information disappear so only use it when you don't have any necessary information in it left, also remember to make sure that's the case. Same here, there's no idea to try to delete a session that doesn't exist so before destroying the session (by using session_destroy() it could be a good idea to check if it exists. Here's how you do it:



    Code:
    <?php 
    if(isset($_SESSION))
    {
    session_destroy();
    }
    ?>


    That was everything for this tutorial. If you wonder anything just ask

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Sessions in PHP

    You should also regenerate the session ID at each page to prevent session hijacking. Nicely done, +rep!

  4. #3
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: Sessions in PHP

    Nice +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  5. #4
    Join Date
    Nov 2009
    Posts
    20
    Rep Power
    0

    Re: Sessions in PHP

    thanks i appreciate the info

  6. #5
    hardinera's Avatar
    hardinera is offline Learning Programmer
    Join Date
    Jul 2009
    Posts
    42
    Rep Power
    0

    Re: Sessions in PHP

    i get undefined index when using session >.> whats that?? -.-


    i solved the problem lol thanks for the post ^_^
    Last edited by hardinera; 02-17-2010 at 11:40 PM. Reason: i already solved my problem code

  7. #6
    Sphexa is offline Newbie
    Join Date
    Mar 2010
    Posts
    21
    Rep Power
    0

    Re: Sessions in PHP

    Great tutorial +rep

  8. #7
    kimjones is offline Newbie
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Re: Sessions in PHP

    thank you for providing well detailed tutorials here. I have been trying to learn Php from other sites but this site gives me exactly what I am looking for.

    ________________________________
    DUI Texas

  9. #8
    SoN9ne's Avatar
    SoN9ne is offline Programmer
    Join Date
    Mar 2010
    Location
    Juno Beach, Florida, United States
    Posts
    125
    Rep Power
    0

    Re: Sessions in PHP

    Great tutorial, I have one suggestion.

    For the destroy session it should be (according to php.net):
    Code:
    <?php
    if (isset($_SESSION)) {
    	// Unset all of the session variables.
    	$_SESSION = array();
    	
    	// If it's desired to kill the session, also delete the session cookie.
    	// Note: This will destroy the session, and not just the session data!
    	if (ini_get("session.use_cookies")) {
    	    $params = session_get_cookie_params();
    	    setcookie(session_name(), '', time() - 42000,
    	        $params["path"], $params["domain"],
    	        $params["secure"], $params["httponly"]
    	    );
    	}
    	
    	// Finally, destroy the session.
    	session_destroy();	
    }
    ?>
    There should be some security measures in place for Sessions but it's still a great start!
    Last edited by SoN9ne; 04-06-2010 at 12:59 PM.

  10. #9
    Prog4rammer's Avatar
    Prog4rammer is offline Newbie
    Join Date
    Jan 2010
    Location
    Gaza
    Posts
    14
    Rep Power
    0

    Re: Sessions in PHP

    nice and Very Simple

    Some Words Me : "♪●Software Engineer and Love Programming in Java and PHP under Ubuntu System ... ♪♥ "

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Intermediate PHP sessions
    By Csabi in forum PHP Tutorials
    Replies: 6
    Last Post: 05-08-2011, 03:39 AM
  2. [help+ask]sessions
    By kiddies in forum PHP Development
    Replies: 3
    Last Post: 05-20-2009, 11:54 AM
  3. Problem using sessions
    By Alhazred in forum PHP Development
    Replies: 5
    Last Post: 03-20-2009, 01:45 AM
  4. Question on Sessions
    By shiyam198 in forum PHP Development
    Replies: 1
    Last Post: 09-03-2008, 08:36 PM
  5. Basic Sessions?
    By phpforfun in forum PHP Development
    Replies: 1
    Last Post: 04-21-2008, 10:20 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts