Main Page Sitemap

Most viewed

ViewMol3D 5.0 with serial key
GoogleThe 37th Frame - Celebrating the Best of Photojournalism Meet consumer expectations of convenience and personalised delivery options Enterprise eCommerce Delivery Management Software Request a demo Single interface to carriers and services Integrated to over 300 carriers and 3,500 services, enabling the warehouse to process...
Read more
Smart Image Server 3.0 + License Key
Multiple bible versions Software - Free DownloadКомпьютерные программы - скачать софтRoads of rome 4 Software - Free Download roads of rome 4Linux Dictionary - The Linux Documentation Project Furk.net is your personal secure storage that fetches media files and lets you stream them immediately You...
Read more
WatchDog 9.3 Full Lifetime version cracked
Macrorit - Newest Preeminent Windows Disk Partition Software Macrorit Disk Partition Expert Free - Free download and Macrorit Partition - Free downloads and reviews - CNET 5 stars "Impressive!" | Version: Macrorit Disk Partition Expert Free 3.5.6 Pros Note: I used the portable version form...
Read more

PhpBlot 1.0 Setup and Activation


Xtags for QuarkXPress 6/7/8 Activated version

Follow Me:

A better programmer follows object oriented programming principals. It is deal with objects and easy to update the code. In this post I want to explain how to develop user registration and login system implementing with object oriented programming in PHP.


PHP Object Oriented Programming

Download Script     Live Demo
The tutorial contains a folder called include with PHP files.

login.php
registration.php
home.php
include
-- functions.php
-- config.php


Database
Sample database users table columns uid, username, passcode, name and email.

CREATE TABLE users
(
uid INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(30) UNIQUE,
password VARCHAR(50),
name VARCHAR(100),
email VARCHAR(70) UNIQUE
);


functions.php
Contains PHP code class User{} contains functions/methods.

<?php
include_once 'config.php';
class User
{
//Database connect 
public function __construct()
{
$db = new DB_Class();
}
//Registration process 
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;
}
}
// Login process
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;
}
}
// Getting name
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'];
}
// Getting session 
public function get_session()
{
return $_SESSION['login'];
}
// Logout 
public function user_logout()
{
$_SESSION['login'] = FALSE;
session_destroy();
}
}
?>


registration.php
Here $user = new User(); is the class User{} object using this calling method $user->register_user{} and inserting values.

<?php
include_once 'include/functions.php';
$user = new User();
// Checking for user logged in or not
if ($user->get_session())
{
header("location:home.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';
}
}
?>
//HTML Code
<form method="POST" action="register.php" name='reg' >
Full Name
<input type="text" name="name"/>
Username
<input type="text" name="username"/>
Password
<input type="password" name="password"/>
Email
<input type="text" name="email"/>
<input type="submit" value="Register"/>
</form>


login.php
Calling method $user->check_login{} for login verification.

<?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)
{
// Login Success
header("location:login.php");
}
else
{
// Login Failed
$msg= 'Username / password wrong';
}
}
?>
//HTML Code
<form method="POST" action="" name="login">
Email or Username
<input type="text" name="emailusername"/>
Password
<input type="password" name="password"/>
<input type="submit" value="Login"/>
</form>


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");
}
?>
//HTML Code
<a href="?q=logout">LOGOUT</a>
<h1> Hello <?php $user->get_fullname($uid); ?></h1>


config.php
Database configuration class DB_class() function __construct() is the method name for the constructor.

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


How to add new function/method.

For example if you want to get email value. Just include the following function inside class User{}

public function get_email($uid)
{
$result = mysql_query("SELECT email FROM users WHERE uid = $uid");
$user_data = mysql_fetch_array($result);
echo $user_data['email'];
}


Print email values.

<?php $user->get_email($uid); ?>


If you're planning to start running a website and you'll be using PHP. You can check the dirrerent web hosting plans online. Some even offer a free domain name.

Was this article helpful?

Yes, thanks!

Not really

Thanks! Your feedback helps us to improve 9lessons.info


Related Posts

Function, OOP, php, Programming

Newer Post Older Post Home


336
Sitemap