Help - Search - Members - Calendar
Full Version: [PHP] - [Sessions] - [ltjfansite]
Pixel2Life Forum > Member Tutorials and Requests > Forum Tutorial Archives > PHP Tutorials
ltjfansite
This Tut Was Writen By Freendom Of Zanmato Design and i had permision to post this tut.

You can find the ut on this page here
Click here

-------------------------------------

Here is a simple session tutorial. First, sessions are things stored on the server, therefor, the person cannot see them unless you print it. Here are how you use this.


First off
    You must put session_start(); at the beginning of every page that you will use sessions in! (Make sure it is inside the php tags)
To make a session
    You use this syntax:
    <?php
    $_SESSION['session_name'] = "SESSION CONTENT";
    ?>

    NOTE: Sessions must be made at the VERY top of a file, before ANYTHING is sent to the viewers browser, OR it will NOT work... but you can access them at any time.
To access a session
    Accessing a session is easy. Just like this...
    <?php
    echo "The content of a session is $_SESSION['session_name']";
    ?>

    So, if the content of the session 'session_name' was CONTENT, on the screen you would see:
    The content of a session is COTNENT

    NOTE: If you are using echo ''; and not echo ""; you will need to put { and } around the session when printing it, example:
    <?php
    echo 'The content of a session is {$_SESSION['session_name']}';
    ?>
To delete a session and the cookie where the session ID is stored
    The session ID is something to identify the current session, it doesn't store any of the actual sessions content. So, to delete all sessions by that person, you need to use this:
    <?php
    session_destroy();
    $_SESSION = array();
    setcookie ('PHPSESSID', '', time-300, '/', '', 0);
    ?>
That's about all! Ask if I forgot something bigwink.gif
iPixel
How do you keep a session alive ... because right now by closing a browser your sessions gets automatically destroyed.
adam123
QUOTE(iPixel @ Jun 2 2005, 09:31 PM)
How do you keep a session alive ... because right now by closing a browser your sessions gets automatically destroyed.

That's how sessions work, if you want them to last, use cookies instead.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.