Wednesday, 26 October 2011

Creating Login_Register page Using OOPS in PHP

The Root folder like:


OOP_sample
                 =>include (folder)
                             =>config.php
                             =>functions.php
                =>register.php
                =>login.php
                =>home.php
config.php


<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'admin1234');
define('DB_DATABASE', 'test');
class DB_Class {
function __construct() {
$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD)
or die('Oops Sql connection error -> ' . mysql_error());
mysql_select_db(DB_DATABASE, $connection) or
die('Database error -> ' . mysql_error());
}


}
?>

functions.php


<?php
include_once 'config.php';
class User {
public function __construct()
{
$db = new DB_Class();
}
public function register_user($name, $username, $password, $email)
{
$password = md5($password);

$sql = mysql_query("SELECT uid from users WHERE username = '$username'
or email = '$email'");
$no_rows = mysql_num_rows($sql);

if ($no_rows == 0)
{
$result = mysql_query("INSERT INTO users(username, password, name,
email)
values
('$username', '$password','$name','$email')") or die(mysql_error());
return $result;
}
else
{
return FALSE;
}

}
public function check_login($emailusername, $password)
{
$password = md5($password);
$result = mysql_query("SELECT uid from users WHERE email = '$emailusername'
or username='$emailusername' and password = '$password'");
$user_data = mysql_fetch_array($result);
$no_rows = mysql_num_rows($result);

if ($no_rows == 1)
{
$_SESSION['login'] = true;
$_SESSION['uid'] = $user_data['uid'];
return TRUE;
}
else
{
return FALSE;
}
}
public function get_fullname($uid)
{
$result = mysql_query("SELECT name FROM users WHERE uid = $uid");
$user_data = mysql_fetch_array($result);
echo $user_data['name'];
}
public function get_session()
{
return $_SESSION['login'];
}
public function user_logout()
{
$_SESSION['login'] = FALSE;
session_destroy();
}
}
?>


login.php


<?php
session_start();
include_once 'include/functions.php';
$user = new User();
if ($user->get_session())
{
header("location:home.php");
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$login = $user->check_login($_POST['emailusername'], $_POST['password']);
if ($login) {
// Registration Success
header("location:login.php");
} else {
// Registration Failed
echo 'Username / password wrong';
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">


<style>
body
{
font-family:Arial, Helvetica, sans-serif;
}
</style>
<script language="javascript" type="text/javascript">
function submitregistration() {
var form = document.login;
if(form.emailusername.value == "")
{
alert( "Enter email or username." );
return false;
}
else if(form.password.value == "")
{
alert( "Enter password." );
return false;
}


}



</script>
</head>
<body>
<div id="container">

<div id="main-body">
<br/><br/>
<form method="POST" action="" id="login_form" name="login">
<div class="head">
<b> Login Here !</b><br/><br/>
</div>
<label>Email or Username</label><br/>
<input type="text" name="emailusername" required="true"/><br/> <br/>
<label>Password</label><br/>
<input type="password" name="password" id="password"
required="true"/><br/><br/>
<input type="hidden" name="flag" value="login"/>
<input type="submit" name="login_btn" onclick="return( submitregistration());"
value="Login"/><br/><br/>
<label><a href="register.php">Register new user</a></label>
</form>

</div>

</div>
</body>
</html>


register.php


<?php


include_once 'include/functions.php';
$user = new User();


// Checking for user logged in or not
if (!$user->get_session())
{
header("location:login.php");
}


if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$register = $user->register_user($_POST['name'], $_POST['username'], $_POST['password'], $_POST['email']);
if ($register) {
// Registration Success
echo 'Registration successful <a href="login.php">Click here</a> to login';
} else {
// Registration Failed
echo 'Registration failed. Email or Username already exits please try again';
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">


<style>
body
{
font-family:Arial, Helvetica, sans-serif;
}
</style>
<script language="javascript" type="text/javascript">


function submitregistration() {
var form = document.reg;
if(form.name.value == "")
{
alert( "Enter name." );
return false;
}
else if(form.username.value == "")
{
alert( "Enter username." );
return false;
}
else if(form.password.value == "")
{
alert( "Enter password." );
return false;
}
else if(form.email.value == "")
{
alert( "Enter email." );
return false;
}


}
</script>
</head>
<body>
<div id="container">
<div id="main-body">
<br/><br/>
<form method="POST" action="register.php" id="register_form" name="reg">
<div class="head">
<b> I am new user !</b><br/><br/>
</div>
<label>Full Name</label><br/>
<input type="text" name="name" required="true"/><br/><br/>
<label>Username</label><br/>
<input type="text" name="username" required="true"/><br/><br/>
<label>Password</label><br/>
<input type="password" name="password" required="true"/><br/><br/>
<label>Email</label><br/>
<input type="text" name="email" id="email" required="true"/><br/><br/>
<input type="submit" name="register_btn" onclick="return( submitregistration());"
value="Register"/><br/><br/>
<label><a href="login.php">I already Registered. Login here</a></label>
</form>
</div>
</div>
</body>
</html>

home.php


<?php
session_start();
include_once 'include/functions.php';
$user = new User();
$uid = $_SESSION['uid'];
if (!$user->get_session())
{
header("location:login.php");
}
if ($_GET['q'] == 'logout')
{
$user->user_logout();
header("location:login.php");
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">


<style>
body
{
font-family:Arial, Helvetica, sans-serif;
}
h1
{
font-family:'Georgia', Times New Roman, Times, serif;
}
</style>
</head>
<body>
<div id="container">
<div id="header">
<a href="?q=logout">LOGOUT</a>
</div>
<div id="main-body">
<br/><br/><br/><br/>
<h1>
Hello <?php $user->get_fullname($uid); ?>
</h1>
</div>
<div id="footer"></div>
</div>
</body>
</html>

No comments:

Post a Comment

Visual comparison of the two methods, creating a simple table.

Option 1, using PHP: // PHP $html = '<table>' ;     foreach ( $data as $row ) {     $html .= '<tr>...