Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @name PHP 5 Login Class
- * @version 1.0
- * @abstract PHP 5 Login Class with support for MySQL databases
- **/
- /* Define connection details to the MySQL server */
- define("DB_HOST", "localhost");
- define("DB_NAME", "casemanagement");
- define("DB_USER", "LoginWizard");
- define("DB_PASS", "12345");
- class Login {
- /* The username of a member */
- private $myusername;
- /* The password of a member */
- private $mypassword;
- /* Runs when an instance of the class is created. It automatically connects to the MySQL server
- and checks if the IP is not banned before contining
- */
- public function __construct() {
- session_start();
- $this->connectToMySQL();
- if(!isset($_SESSION['auth'])){
- $_SESSION['auth'] = 0;
- }
- }
- /* Return the username of a member*/
- public function getUsername() {
- return $this->myusername;
- }
- /* Return the plain text password of a member */
- public function getPassword() {
- return $this->mypassword;
- }
- /* Return the encrypted password of a member */
- public function getEncryptedPassword() {
- return sha1($this->mypassword);
- }
- /* Get a member's IP Address */
- public function getUserIP() {
- return $_SERVER['REMOTE_ADDR'];
- }
- /* Connection to a MySQL database using the defined connection details */
- public function connectToMySQL() {
- @mysql_connect(DB_HOST, DB_USER, DB_PASS)or die("Cannot connect to Database");
- mysql_select_db(DB_NAME) OR die("Cannot select database!");
- }
- /* Validate a member login from data in a MySQL Database. */
- public function verifyLogin($myusername, $mypassword) {
- $this->myusername = $myusername;
- $this->mypassword = $mypassword;
- if(empty($myusername) || empty($mypassword)) {
- header("Location: authfail.php");
- }
- else {
- $query = "SELECT * FROM cmt_user WHERE username='$myusername' and password='$mypassword'";
- $this->clean($myusername);
- $this->clean($mypassword);
- $result = mysql_query($query) OR die('Cannot perform query!');
- if (mysql_num_rows($result) == 1) {
- $this->sessionVerify();
- $_SESSION['myusername']=$myusername;
- header("Location: login_success.php");
- }
- else {
- session_destroy();
- header("Location: authfail.php");
- }
- }
- mysql_free_result($result);
- }
- /* Verify the session login.
- Used for protected pages on your website
- */
- public function sessionVerify() {
- session_regenerate_id();
- $_SESSION['auth'] = 1;
- $_SESSION['name'] = $this->myusername;
- $user = md5($_SERVER['HTTP_USER_AGENT']. $_SERVER['REMOTE_ADDR']);
- $_SESSION['user_details']=$user;
- $user= 0;
- }
- /* Checks if the Session data is correct before continuing
- the script */
- public function verifyAccess() {
- $user = md5($_SERVER['HTTP_USER_AGENT']. $_SERVER['REMOTE_ADDR']);
- if(isset($_SESSION['name']) && $_SESSION['auth'] == 1 && $_SESSION['user_details'] == $user) {
- return true;
- }
- header("Location: login.php");
- exit;
- }
- /* Escape the input */
- public function clean($input) {
- return mysql_real_escape_string($input);
- }
- }
- ?>
Add Comment
Please, Sign In to add comment