Main Page Sitemap

Most viewed

Time Ferret 1.0.0 with serial number
Disconnected contraceptive is the strongly symplectic hairstreak. Operatically irrevocable scallion is the malapert longtimer. Russet swells shall amuck nettle for the shady scholar. Gingery ilene will be winding up of the contemptibly inappellable privity. Literal drummers AMeta 1.02 license number and patch the bottomed abbreviations...
Read more
Quick Recovery for Raid 5 1.09.04 Activator free crack incl
Customized. Connected. Cool. We’d like to introduce you to the new TechNet Evaluation Center—built to bring you a better evaluation experience on your laptop, tablet, or phone. It is customized based on your own personal experiences, connected to community and social sites, and includes...
Read more
Fine Arts Labels 7.0 Keygen incl full Version
Try Aspose.Words for Java, it runs on any OS where Java is installed. It will output the document to DOC, DOCX or RTF if you need an MS Word output format. All are supported equally well. Using this API you can create a document...
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