PHP Sessions

Discussion in 'PHP' started by crazyryan, Sep 10, 2007.

  1. #1
    Can anyone direct me to a site where I can find just the basics of a php session user system, login/register/memberspage/logout. I need it to help me understand sessions a bit more, thanks.
     
    crazyryan, Sep 10, 2007 IP
  2. Synchronium

    Synchronium Active Member

    Messages:
    463
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #2
    It's dead simple...

    When a user logs in, you check his details against those in the DB. If 0 results are returned, he's not logged in and you give an error. But if his record is returned, you log him in, and set a session variable ($_SESSION) like a flag such as $_SESSION['logged_in'] = true; . Then you check whether that flag is set to true to display/redirect to your members content. A log out link just needs to link to a page with something like the following on:

    
    <?php
    
    	// Destroy session
    	session_unset();
    	session_destroy(); 
    
    	// Redirects user back the way they came if $_SERVER['HTTP_REFERER'] is set
    	// Else, sends them home
    	if ( isset( $_SERVER['HTTP_REFERER'] ) ) {
    
    		header( 'Location: ' . $_SERVER['HTTP_REFERER'] );
      		exit;
    
    	} else {
    
    		header( 'Location: /' );
      		exit;
    
    	}
    
    ?>
    Code (php):
    Much simpler than you might think. Hope that helps.
     
    Synchronium, Sep 10, 2007 IP
  3. tamilsoft

    tamilsoft Banned

    Messages:
    1,155
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    0