Login PHP from DB and Say hello

Login PHP from DB and Say hello to user on dashboard handling exception

<?php
/*
 * Script handle login
 */

$mysql = mysql_connect();
$username = trim($_POST['username']);
$password = trim($_POST['password']);

try {

   //Ger User by username provided
   $q = 'SELECT * FROM users WHERE username = ' . $username . ' LIMIT 1';
   $user = mysql_query($q);

   if (!$user)
      throw new Exception(sprintf('User dengan username %s tidak ditemukan', $username));

   // buid user from DB
   $userDb = array();
   while ($row = mysql_fetch_object($user)) {
      $userDb['username'] = $row->username;
      $userDb['password'] = $row->password;
      $userDb['fullname'] = $row->fullname;
      $userDb['address']  = $row->address;
   }

   // Cek user Password
   if (md5($password) != $userDb['password'])
      throw new Exception('Password yang anda masukkan tidak sesuai');

   //Set Session USER
   $_SESSION['USER'] = $userDb;

   //arahkan user ke dashboard
   header('Location: HALAMAN_DASHBOARD');

} catch (Exception $e) {

   //kembalikan user ke halaman login dengan pesan error
   header('Location: HALAMAN_LOGIN?msg' . $e->getMessage());
}




/*
 * Script halaman dashboard
 */

echo sprintf('Selamat Datang %s', $_SESSION['USER']['fullname']);
echo "FORM DLL";

Comments

There were no comments found for this weblog.

Du mußt Dich einloggen um diesen Weblog kommentieren zu können!