Advertisement
Guest User

Untitled

a guest
Oct 19th, 2011
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.49 KB | None | 0 0
  1. <?php
  2.  
  3. ## Class cLogin()
  4. ##
  5. ## Written by Henric Johansson, henric-johansson@hotmail.com 2011-10-19
  6. ## Takes care of your login system
  7.  
  8. require_once('cSession.php');
  9. require_once('cDatabase.php');
  10.  
  11. class cLogin extends cSession {
  12.     private $errorMessage = "";
  13.     function __construct($username = "", $password = "", $database = "", $host = "") {
  14.        
  15.         ## Check if we have all neccessary variables
  16.         if($username == "" || $database == "" || $host == "") {
  17.             ## Return false
  18.             $this->errorMessage = 'Username, Database or Host not correct<br />';
  19.             return false;
  20.         }
  21.  
  22.         ## Make sure the database connection is initialized
  23.         if(!cDatabase::isInitialized()) {
  24.  
  25.             ## If we want to debug:
  26.             ## cDatabase::debug();
  27.             ## Or cDatabase::debug(false) to stop
  28.  
  29.             ## We are not initialized, initialie connection
  30.             if(cDatabase::initialize($username, $password, $database, $host)) {
  31.                 ## Everything worked as planned
  32.             }
  33.         }
  34.  
  35.         parent::__construct();
  36.     }
  37.  
  38.     function LoggedIn() {
  39.         if($this->get('user_id') > 0) {
  40.             return true;
  41.         } else {
  42.             return false;
  43.         }
  44.     }
  45.  
  46.     function getError() {
  47.         $msg = $this->errorMessage;
  48.         $this->errorMessage = "";
  49.         return $msg;
  50.     }
  51.  
  52.     function Register($username, $password) {
  53.         ## Check if our db connection is active
  54.         if(!cDatabase::isInitialized()) {
  55.             $this->errorMessage = 'Database connection not initialized. <br />';
  56.             return false;
  57.         }
  58.  
  59.         ## Escape username
  60.         $username = mysql_real_escape_string($username);
  61.  
  62.         ## Make sure user does not exist.
  63.         $query = "select * from users where username='$username'";
  64.         $result = mysql_query($query);
  65.         if(mysql_num_rows($result) > 0) {
  66.             ## Username already exists
  67.             $this->errorMessage = 'Username already exists<br />';
  68.             return false;
  69.         }
  70.  
  71.         ## Secure password with a salt and md5 hash
  72.         $password = md5("€@£¤(%¤23!" . $password);
  73.  
  74.         $query = "insert into users (username, password, registered) values ('$username', '$password', NOW())";
  75.         if(!mysql_query($query)) {
  76.             $this->errorMessage = 'Could not insert query, query: ' . $query . '<br />';
  77.             return false;
  78.         }
  79.  
  80.         return true;
  81.     }
  82.  
  83.     function Update($username) {
  84.         $query = "update users set last_ip='{$_SERVER['REMOTE_ADDR']}', last_login=NOW() where username='$username'";
  85.         if(mysql_query($query)) {
  86.             return true;
  87.         } else {
  88.             $this->errorMessage = 'Could not update users last login and IP<br />';
  89.             return false;
  90.         }
  91.     }
  92.  
  93.     function Login($username, $password) {
  94.         ## Check if our db connection is active
  95.         if(!cDatabase::isInitialized()) {
  96.             $this->errorMessage = 'Database connection not initialized. <br />';
  97.             return false;
  98.         }
  99.  
  100.         ## Escape username
  101.         $username = mysql_real_escape_string($username);
  102.  
  103.         ## Secure password with a salt and md5 hash
  104.         $password = md5("€@£¤(%¤23!" . $password);
  105.  
  106.         ## Make sure user does not exist.
  107.         $query = "select id from users where username='$username' and password='$password'";
  108.         $result = mysql_query($query);
  109.         if(mysql_num_rows($result) <= 0) {
  110.             ## Username already exists
  111.             $this->errorMessage = 'Wrong username or password ' . $query . '<br />';
  112.             return false;
  113.         } else {
  114.             ## Update users login date and ip
  115.             if($this->Update($username)) {
  116.  
  117.                 ## If user updated, get the id and set the session vars
  118.                 $row = mysql_fetch_array($result);
  119.                 $this->set($username, 'username');
  120.                 $this->set($row['id'], 'user_id');
  121.                 return true;   
  122.             } else {
  123.                 return false;
  124.             }
  125.         }
  126.     }
  127.  
  128.     function Logout() {
  129.         parent::kill();
  130.     }
  131. }
  132.  
  133. ?>
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement