Location: PHPKode > scripts > Simple Login Class > simple-login-class-1-00/class.secure-jh.php
<?php

/****************************************
 *@Author: Balaji J H
 *@Version: 1.00
 *@Package Name: Simple Login Class
 *@HomePage: http://jhbalaji.com
 *@EMail: hide@address.com / hide@address.com
 *@Developed For: BloggerPoster.com
 *@Copyright: Balaji J H,BloggerPoster.com
 *@License: Free For Personal & Commerical Username but Header and Copyright information must stay intact!
*****************************************/
class encryptpass
{

	// The Password here will be first encoded five times using Base64 Algorthim then it is reversed
	// That reversed Password will be Hashed using MD5 and SHA1
	// So its unbreakable and more powerful!

	//function to encrypt the password
	function jh_password_protection($pass_string)
	{

		for($i=0; $i<5;$i++)
		{
			$$pass_string=strrev(base64_encode($pass_string)); //apply base64 first and then reverse the string
		}
		$pass_string = md5($pass_string);
		$pass_string = sha1($pass_string);
		return $pass_string;
	}

}

class loginjh
{


	// Register a New User
	function registerUser($user,$pass1,$pass2) 
	{
		session_start(); // Registering the Login Session
		$errorText = '';
		
		
		// Check password are identical
		if ($pass1 != $pass2) $errorText = "Oops! Password does not match!";
		elseif (strlen($pass1) < 6) $errorText = "LOL! The Password is too short!";
		
		// Check user existance	
		$pfile = fopen("userpwd.txt","a+");
		rewind($pfile);

		while (!feof($pfile)) 
		{
			$line = fgets($pfile);
			$tmp = explode(':', $line);
			if ($tmp[0] == $user) 
			{
				$errorText = "Hurrah! Someone already took that Username!";
				break;
			}
		}
		
		
		// If everything is OK then Store Username and Password
		if ($errorText == '')
		{
			// Secure password string
			$encrytpwd = new encryptpass();
			$userpass = $encrytpwd->jh_password_protection($pass1);
			
			fwrite($pfile, "\r\n$user:$userpass");
		}
		
		fclose($pfile);
		
		
		return $errorText;
	}


	// User Login
	function loginUser($user,$pass)
	{
		session_start(); // Registering the Login Session
		$errorText = '';
		$validUser = false;
		
		// Check user existance	
		$pfile = fopen("userpwd.txt","r");
		rewind($pfile);

		while (!feof($pfile)) {
			$line = fgets($pfile);
			$tmp = explode(':', $line);
			if ($tmp[0] == $user) 
			{
				// User exists, check password
				$encrytpwdcheck = new encryptpass();
				$userpasscheck = $encrytpwdcheck->jh_password_protection($pass);
				if (trim($tmp[1]) == trim($userpasscheck))
				{
					$validUser= true;
					$_SESSION['userName'] = $user;
				}
				break;
			}
		}
		
		fclose($pfile);
		

		if ($validUser != true) $errorText = "OMG! This is an Invalid Username or Password!";
		
		if ($validUser == true) $_SESSION['validUser'] = true;
		else $_SESSION['validUser'] = false;
		
		return $errorText;	
	}



	// User logout
	function logoutUser()
	{
		// To Get rid of the Logout errors
		session_start(); // Registering the Login Session
		unset($_SESSION['validUser']);
		unset($_SESSION['userName']);
	}



	// Check for Duplicate Usernmae
	function checkUser()
	{	session_start(); // Registering the Login Session
		if ((!isset($_SESSION['validUser'])) || ($_SESSION['validUser'] != true))
		{
			header('Location: login.php'); // After Logging out it will be redirected to login.php
			exit(); // To avoid Headers laredy sent problem
			
		}
	}
}

?>
Return current item: Simple Login Class