PHP的SESSION处理

From Yefu's notes
Jump to: navigation, search
<?php
// Starting the session
session_start(); //这个似乎要放在html之前?

if(isset($_SESSION['user']))
    {
        // Code for Logged members

        // Identifying the user
        $user = $_SESSION['user'];
       
        // Information for the user.
    }
else
    {
        // Code to show Guests
   
    }
?>
    
Code for Logging a User:
<?php
//Username Stored for logging
define("USER", "user");

// Password Stored
define("PASS", "123456");

// Normal user section - Not logged ------
        if(isset($_REQUEST['username']) && isset($_REQUEST['password']))
            {
                // Section for logging process -----------
                $user = trim($_REQUEST['username']);
                $pass = trim($_REQUEST['password']);
                if($user == USER && $pass = PASS)
                    {
                        // Successful login ------------------
                       
                        // Setting Session
                        $_SESSION['user'] = USER;
                       
                        // Redirecting to the logged page.
                        header("Location: index.php");
                    }
                else
                    {
                        // Wrong username or Password. Show error here.
                       
                    }
               
            }
?>
User can be logged successfully with username as "user" and password as 123456.

<comments/>