irwan

Example Login

Mar 9th, 2012
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.86 KB | None | 0 0
  1.     /* Snippet #1 */
  2.     <?php
  3.     // login_check.php
  4.     define("server", "your_server");
  5.     define("user", "your_name");
  6.     define("password", "your_pass");
  7.     define("name", "your_dbname");
  8.      
  9.     var $connection;
  10.     $this->connection = mysql_connect(server,user,pass) or die(mysql_error());
  11.     mysql_select_db(name, $this->connection) or die(mysql_error());
  12.      
  13.     function is_logged_in () {
  14.       if (!($_SESSION["id"]) || ($_SESSION["id"] == "") || ($_SESSION["id"] == 0)) {
  15.         Header("Location: ./login.php");
  16.         exit();
  17.       }
  18.     }
  19.      
  20.     function clean_input($input) {
  21.      
  22.       $clean = array("\\",'<','>','`',':',';','/','(',')','{','}','[',']');
  23.       //$with = array();
  24.       return str_ireplace($clean,'', $input);
  25.     }
  26.      
  27.     function login_check ($forms) {
  28.       $error = "";
  29.       $username = clean_input($forms["username"]);
  30.       $password = clean_input($forms["password"]);
  31.       if (trim($username) == "") $error .= "<li>Your username is empty.</li>";
  32.       if (trim($password) == "") $error .= "<li>Your password is empty.</li>";
  33.       /* from here, do your sql query to query the database to search for existing record with correct username and password */
  34.       $query = "SELECT password, username FROM users WHERE username = '".mysql_real_escape_string($username)."' AND password = '".mysql_real_escape_string($password)."'";
  35.       $result = mysql_query($query, $this->connection);
  36.       if(!$result || (mysql_numrows($result) < 1)) {
  37.          $error = "Invalid username or password";
  38.       }else
  39.           {
  40.             $error = "";
  41.           }
  42.       if (trim($error)!="") return $error;
  43.     }
  44.      
  45.     function login ($forms) {
  46.       $username = clean_input($forms["username"]);
  47.       $password = clean_input($forms["password"]);
  48.       /* do your sql query again, but now returning the id of member */
  49.       $query = "SELECT member_id FROM users WHERE username = '".mysql_real_escape_string($username)."' AND password = '".mysql_real_escape_string($password)."'";
  50.       $result = mysql_query($query, $this->connection);
  51.       $result = mysql_query($query, $this->connection);
  52.       if(!$result || (mysql_numrows($result) < 1)) {
  53.          $id = 0;
  54.       }else
  55.           {
  56.             $id = $result;
  57.           }
  58.       return $id;
  59.     }
  60.     ?>
  61.      
  62.  
  63.  
  64.  
  65.  
  66.  
  67.     /* Snippet #2 */
  68.     <?php
  69.     // login.php
  70.     session_start();
  71.     include ("login_check.php");
  72.     if ($_POST) {
  73.       $error = login_check($_POST);
  74.       if (trim($error)=="") {
  75.         $_SESSION["id"] = login($_POST);
  76.         Header("Location: ./index.php") /* Redirect validated member */
  77.         exit();
  78.       } else {
  79.         print "Error:$error";
  80.       }
  81.     }
  82.     ?>
  83.      
  84.     /* Snippet #3 */
  85.     <?php
  86.       // index.php
  87.       include("login_check.php");
  88.       session_start();
  89.       is_logged_in();
  90.     ?>
Advertisement
Add Comment
Please, Sign In to add comment