Results 1 to 5 of 5

Thread: [RESOLVED]how to pass user info to diffrent pages using sessions?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Resolved [RESOLVED]how to pass user info to diffrent pages using sessions?

    Hi all i wonder how i can pass user information such as login name to diffrent pages using sessions. My intention is that i want in diffrent pages the php pages automatically knows who is using the page and where user log name is necessary it is avaliable by just doing echo $username or using $username in queries.Furthermore, i want the sessions some how expire within 15 min or so.I be happy if an expert show me how i can create sessions and how to use it in php pages for above purpuse. Thanks
    Last edited by tony007; Feb 25th, 2006 at 08:37 AM.

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: how to pass user info to diffrent pages using sessions?

    Session variables are stored in the super global array $_SESSION. Before using this array you need to initialise the session with session_start(). Once you have done this, any variable you save into it will be avialable accross all the scripts on your site.
    PHP Code:
    session_start();

    // to set the variable
    $_SESSION['username'] =  $_POST['username'];

    // to retrieve the variable
    $username $_SESSION['username']; 
    To expire a session you could use the session.gc_maxlifetime conffig option. This will cause PHP to see the session as stale after a certain time and delete it. However, garbage cleanup is not performed on every script execution so it cannot be considered accutrate.
    PHP Code:
    ini_set('session.gc_maxlifetime''900'); // causes sessions to become stale after 15min 
    The best way to do it is to use an additional variable called expire time. Each time the user makes a request to one of your scripts, set it too the current time and at the beginning of each script check to ensure the time elapsed from the previous request is not greater than 15 mins.
    PHP Code:
        session_start();

        if (isset(
    $_SESSION['request_time'])) {
            if (
    time() - ((int) $_SESSION['request_time']) > 900) {
                
    // session expired
            
    }
        }

        
    $_SESSION['request_time'] = time(); 
    If you check the link in my signature, I have a session class, which allows for easier management of sessions and for storage of session data in a MySql database.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: how to pass user info to diffrent pages using sessions?

    visualAd many thanks for u helpfull post. I have a few questions :

    1) do i need to use session_start(); in every page inside the directory that
    i want to use seession?

    2) if i define a variable inside sesssion where that php file containing session_start() is in one directory . Do u think that variable will be availabe in php pages inside other
    folders?


    3) where i want to utilize session variable do i need
    to do :
    $username = $_SESSION['username'];
    echo $username


    or do i have to add any other code before these 2 lines such
    as using session_start()


    4) May i know why u used $_POST['username']; is that
    the way to recive form posted value and put it equal to session variable ?

    // to set the variable
    $_SESSION['username'] = $_POST['username'];


    5) do i need to put session life time code after session start in every page ?

    session_start();
    ini_set('session.gc_maxlifetime', '900'); // causes sessions to become stale after 15min


    6)
    do i need to post this line in every page that i want use the stored variable in session ? or just once when i
    login in to protectd folder ?
    session_start();

    Code:
    if (isset($_SESSION['request_time'])) { 
            if (time() - ((int) $_SESSION['request_time']) > 900) { 
                // session expired 
            } 
        } 
    
        $_SESSION['request_time'] = time();

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: how to pass user info to diffrent pages using sessions?

    1) do i need to use session_start(); in every page inside the directory that i want to use seession?
    Yes

    2) if i define a variable inside sesssion where that php file containing session_start() is in one directory . Do u think that variable will be availabe in php pages inside other folders?
    Yes, you can limit where the session cookie wil be seen by modifying the session.cookie_path setting.

    3) where i want to utilize session variable do i need
    to do :
    $username = $_SESSION['username'];
    echo $username


    or do i have to add any other code before these 2 lines such
    as using session_start()
    You need to use session_start() first. If you do not, then using $_SESSION will simply generate an undefined variable notice.

    4) May i know why u used $_POST['username']; is that
    the way to recive form posted value and put it equal to session variable ?

    // to set the variable
    $_SESSION['username'] = $_POST['username'];
    It was an example. Variables inside the $_POST array come from forms submitted via the post method. $_GET contains variables from the query string. You can assign any variable to the $_SESSION array.

    5) do i need to put session life time code after session start in every page ?

    session_start();
    ini_set('session.gc_maxlifetime', '900'); // causes sessions to become stale after 15min
    Yes, you need to call session_start() before you do anything with $_SESSION

    6) do i need to post this line in every page that i want use the stored variable in session ? or just once when i
    login in to protectd folder ?
    session_start();

    Code:
    if (isset($_SESSION['request_time'])) { 
            if (time() - ((int) $_SESSION['request_time']) > 900) { 
                // session expired 
            } 
        } 
    
        $_SESSION['request_time'] = time();
    If you want your session to expire properly, then yes.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5
    Fanatic Member jcavard's Avatar
    Join Date
    Jul 2005
    Location
    Quebec, CANADA
    Posts
    534

    Re: how to pass user info to diffrent pages using sessions?

    dang visualad... that is one oe the best reply I seen, so detailed....

    I dare you to do the same with this thread now
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

    accoustic emo-rock band: a tailormade fable

    Visual Studio 2003 / Framework 1.1

Posting Permissions

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



Click Here to Expand Forum to Full Width