Senin, 30 Juli 2012

Membuat Login & Logout dengan script PHP


1.  Inilah Script PHP untuk Login:
<?php
      session_start();
      include "config/konek.php";
      include "config/functions.php";
     
      if (isset($_POST['login'])) {
            $user       = $_POST['nama'];
            $password = md5($_POST['password']);
           
            $pesan="";
            $pesan.=cek_kosong($user,"nama masih kosong \\n");
            $pesan.=cek_kosong($password,"Password masih kosong \\n");
           
            if ($pesan <> "") {
                  back ($pesan);
                        exit;
           
            } else {
           
                  $sql  = "SELECT * FROM user WHERE nama='".$user."' AND password='".$password."'";
                 
                  $qry  = mysql_query($sql,$koneksi);
                  $num  = mysql_num_rows($qry);
                  $data = mysql_fetch_array($qry);
                 
                  if ($num == 1) {
                        $userid     = $data['id'];
                        $userlv     = $data['status'];
                       
                        session_register("userid");
                        session_register("userlv");
                       
                        header('location:home.php');
                  } else {
                        $pesan="nama dan Password anda tidak sesuai";
                        back ($pesan);
                  }
            }
      }
?>
b.  Inilah script PHP untuk Logout :
<?php
      session_start();
      session_destroy();
      header('location: index.php');
?>

0 komentar:

Posting Komentar