Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: PHP  |  size: 1.67 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. //Authentication Script, log a user in or out of the system
  3. //Include required functions
  4. require_once('sessionfunc.php');
  5.  
  6. //Create shorter names for variables
  7. @$username= $_POST['username'];
  8. @$passwd= $_POST['passwd'];
  9.  
  10. //Clear errorcode variable if it already exists
  11. if(isset($_GET['errcode']))
  12. { unset($_GET['errcode']); }
  13.  
  14. session_start();
  15.  
  16. //Execute for login
  17. if ($_POST['action']=="Login")
  18. {
  19. //Check if either field is empty
  20.  if ((empty($username)) || (empty($passwd)))
  21.   {
  22.   $url= $_SESSION['pageurl']."?errcode=1";
  23.   header('Location:' . $url);
  24.   }
  25.  else
  26.  {
  27.  //Connect to the database
  28.  $handle = dbhandle();
  29.  //cannot use dberror as not using $result
  30.   if (!$handle)
  31.    {
  32.    $url= $_SESSION['pageurl']."?errcode=6";
  33.    header('Location:' . $url);
  34.    exit;
  35.    }//otherwise connect to database
  36.   else {$connect = mysql_select_db('a7464386_justbe', $handle);}
  37.  
  38.   //Check if provided username and password match
  39.  
  40.   $result = mysql_query("SELECT * FROM user WHERE username= '$username' AND passwd = PASSWORD('$passwd')");
  41.   //Check for database error
  42.   dberror($result);
  43.  
  44.  //If result is found, true otherwise username or password is wrong
  45.   if (mysql_num_rows($result)>0)
  46.    {
  47.    $_SESSION['validuser'] = $username;
  48.    $url = $_SESSION['pageurl'];
  49.    header('Location:' . $url);
  50.    exit;
  51.    }
  52.   else
  53.    {
  54.    $url = $_SESSION['pageurl']."?errcode=12";
  55.    header('Location:' . $url);
  56.    exit;
  57.    }
  58.  }
  59.  }
  60.  
  61. //Execute for logout
  62. else
  63. {
  64. //No longer passes as logged in but rest of session data can remain until expiry
  65. unset($_SESSION['validuser']);
  66. $url = $_SESSION['pageurl'];
  67. echo $url;
  68. header('Location:' . $url);
  69. exit;
  70. }
  71. ?>