login proses


SUBMITTED BY: Guest

DATE: March 18, 2014, 2:54 p.m.

FORMAT: Text only

SIZE: 2.1 kB

HITS: 843

  1. <?php
  2. //Start session
  3. session_start();
  4. //Include database connection details
  5. require_once('koneksi_db.php');
  6. //Array to store validation errors
  7. $errmsg_arr = array();
  8. //Validation error flag
  9. $errflag = false;
  10. //Function to sanitize values received from the form. Prevents SQL injection
  11. function clean($str) {
  12. $str = @trim($str);
  13. if(get_magic_quotes_gpc()) {
  14. $str = stripslashes($str);
  15. }
  16. return mysql_real_escape_string($str);
  17. }
  18. //Sanitize the POST values
  19. $username = clean($_POST['username']);
  20. $password = md5($_POST['password']);
  21. //Input Validations
  22. if($username == '') {
  23. $errmsg_arr[] = 'Login ID missing';
  24. $errflag = true;
  25. }
  26. if($password == '') {
  27. $errmsg_arr[] = 'Password missing';
  28. $errflag = true;
  29. }
  30. //If there are input validations, redirect back to the login form
  31. if($errflag) {
  32. $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
  33. session_write_close();
  34. header("location: index.php");
  35. exit();
  36. }
  37. //Create query
  38. $qry="SELECT * FROM data_pakar WHERE username='$username' AND password='".md5($_POST['password'])."'";
  39. $result=mysql_query($qry);
  40. //Check whether the query was successful or not
  41. if($result) {
  42. if(mysql_num_rows($result) == 1) {
  43. //Login Successful
  44. session_regenerate_id();
  45. $member = mysql_fetch_assoc($result);
  46. $_SESSION['SESS_USERNAME'] = $member['username'];
  47. header("location: pakar_index.php");
  48. exit();
  49. }
  50. else {
  51. //Login failed
  52. echo "<meta http-equiv=\"refresh\" content=\"0; url=index.php?page=gagal_login2\">";
  53. exit();
  54. }
  55. }else {
  56. die("Query failed");
  57. }
  58. $act=$_GET['act'];
  59. if ($act=="logout"){
  60. session_start();
  61. unset($_SESSION['SESS_USERNAME']);
  62. "<meta http-equiv=\"refresh\" content=\"0; url=index.php>";
  63. }
  64. ?>

comments powered by Disqus