[HELP] Sessions

Discussion in 'PHP' started by fi3h3r, May 19, 2010.

  1. #1
    Ok i am creating a middle ages online text based game i know the basic sessions but i want to know a way i can keep my user logged in till they close there browser does anyone know how i could do this with some sample code and an explanation?
     
    fi3h3r, May 19, 2010 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    You can do it by cookies.
     
    s_ruben, May 20, 2010 IP
  3. chtdatweb

    chtdatweb Well-Known Member

    Messages:
    1,473
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    160
    #3
    Hi,

    Use php sessions, something like this would work

    <?php
    session_start();
    $valid_user = $_POST['valid_user'];
    session_register('valid_user');
    ?>

    This loads the user variable into a session, all you need to now is vaildate that user on each page they visit. So every page might have something like:

    <?
    session_start();
    $valid_user = $_SESSION['valid_user'];
    if ( empty( $valid_user ) ) {
    //user not validated, goto login page.
    } else {
    //user valid, continue script
    }

    The process above registers $valid_user into a session, however, for more secure pratices you could verify the register variable with a mysql database.

    By no means an expert but this should do what you need.
     
    chtdatweb, May 20, 2010 IP
  4. qasimbilal

    qasimbilal Member

    Messages:
    25
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    36
    #4
    You have to use simple session variable. A session variable expires when you close the browser.

    Cookies are created on timely basis so you cannot remove a cookie when browser closes.
     
    qasimbilal, May 21, 2010 IP
  5. Christian Little

    Christian Little Peon

    Messages:
    1,753
    Likes Received:
    80
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That's not true. The session variable will expire when they leave the page UNLESS every page has session_start(); declared at the start. If that code doesn't exist, then the session will end after the leave the current page, not just when they close the browser.
     
    Christian Little, May 21, 2010 IP