Php Sessions. I don't understand how it works.

Discussion in 'Programming' started by cipals15, Nov 29, 2008.

  1. #1
    I have read lots of articles about php sessions but i have a problem applying it to my login | logout | Register | My Profile tab.

    Can anyone clarify or explain to me php sessions in layman's term? Can anyone also give me codes that use php session in motion.

    Please i need to finish my code within a month. :(
     
    cipals15, Nov 29, 2008 IP
  2. juust

    juust Peon

    Messages:
    214
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    juust, Nov 29, 2008 IP
  3. misbah

    misbah Active Member

    Messages:
    265
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    68
    #3
    this is just example
    <?
    session_start();
    
    $do=$_GET["do"];
    
    if($_POST)
    	{
    if($_POST["name"]=='admin' && $_POST["pass"]='pass')
    {
    $_SESSION["username"]='Admin';
    }
    	}
    
    if($do=='logout')
    	{
    session_destroy();
    unset($_SESSION["username"]);
    	}
    ?>
    
    <html>
    <head>
    <title>A Sample Login Script | PHPGame.Net</title>
    </head>
    <body>
    <?
    // ======== IF LOGIN SESSION NOT EXIST-REQUEST TO LOGIN
    if(!$_SESSION["username"] && $do!='logout')
    {
    print "	Your are currently not login.<br/>Please login with your username and password below
    	<form action='' method='post'>
    	Username <input type='text' name='name' size='20'/> (admin)<br/>
    	Password <input type='password' name='pass' size='20'/> (pass)<br/>
    	<input type='submit' value= ' Login '/>
    	</form>";
    }
    
    // ======== LOGOUT
    if($do=='logout')
    {
    print "You have successfully logout.<br/>
    	<a href='index.php'>Click here to main page</a>";
    }
    
    // ======== LOGIN SESSION EXIST-YOU CAN ONY SEE THIS AREA ONCE YOU LOGIN CORRECTLY
    if($_SESSION["username"])
    {
    print "Welcome to the Admin area<br/><br/>
    	<a href='?do=logout'>Logout now</a>";
    }
    ?>
    </body>
    </html>
    
    PHP:
     
    misbah, Nov 30, 2008 IP