GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Sunday, January 2, 2011

Session In PHP

To use session, type
session_register("username");    // username is variable 
                                 // $username="lady gaga"
The you check it by using:
session_start();    // this must the first you type in the file.
$name = $_SESSION['username'];
echo $name;

For complete example, look
checklogin.php
<?php

    $username = $_POST['username'];
    $password = $_POST['password'];

    mysql_connect("localhost","root","");
    mysql_select_db("satu");
    $query = "SELECT * FROM login WHERE username='$username'
              AND password='$password'";
    $result = mysql_query($query);
    $count = mysql_num_rows($result);

    if($count==1){
        session_register('username');
        session_register('password');
        header("Location: hello.php");
    }else{
        header("Location: index.php");
    }
?>


Type this in hello.php
<?php 
    session_start();
    if(!session_is_registered('username')){
        header("location: index.php");
    }
  
    echo "<h1>Hello, $_SESSION['username']<h1>";
?>


To make a logout app, type:
<?php
    session_start();
    session_destroy();
?>

           
Share/Bookmark

No comments:

Post a Comment