Vous êtes sur la page 1sur 4

Lecture 23 Web Systems & Technologies

Login and Registration System


Create a database in your server named ‘login_system’ and then
insert table named ‘users’

Confg.php
define("DB_HOST", 'localhost');
define("DB_USER", 'root');
define("DB_PASSWORD", '');
define("DB_DATABSE", 'login system');

The define() function defines a constant.

Constants are much like variables, except for the following differences:

 A constant's value cannot be changed after it is set


 Constant names do not need a leading dollar sign ($)
 Constants can be accessed regardless of scope
 Constant values can only be strings and numbers

Dbconnect.php
class dbconnect {
function __construct() {
require_once('confg.php');
$conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysql_select_db(DB_DATABSE, $conn);
if(!$conn)// testing the connection
{
die ("Cannot connect to the database")
}
return $conn;
Lecture 23 Web Systems & Technologies

}
public function Close(){
mysql_close()
}
}

 PHP mysqli_connect function


The PHP mysql connect function is used to connect to a MySQL database server.

 PHP mysqli_select_db function


The mysqli_select_db function is used to select a database.

 PHP mysqli_close function


The mysqli_close function is used to close an open database connection.

 PHP die() function


The die() function is an alias of the exit() function.

Dbfunction.php
require_once 'dbconnect.php';
session_start();
class dbfunction {
function __construct() {
// connecting to database
$db = new dbconnect();;

}
// destructor
function __destruct() {

}
Lecture 23 Web Systems & Technologies

public function UserRegister($username, $emailid, $password){


$password = md5($password);
$qr = mysql_query("INSERT INTO users(username, emailid, password)
values('".$username."','".$emailid."','".$password."')") or die(mysql_error());
return $qr;

}
public function Login($emailid, $password){
$res = mysql_query("SELECT * FROM users WHERE emailid = '".$emailid."' AND
password = '".md5($password)."'");
$user_data = mysql_fetch_array(res);
//print_r($user_data);
$no_rows = mysql_num_rows($res);

if ($no_rows == 1)
{

$_SESSION['login'] = true;
$_SESSION['uid'] = $user_data['id'];
$_SESSION['username'] = $user_data['username'];
$_SESSION['email'] = $user_data['emailid'];
return TRUE;
}
else
{
return FALSE;
}
}
public function isUserExist(emailid){
$qr = mysql_query("SELECT * FROM users WHERE emailid = '".$emailid."'");
Lecture 23 Web Systems & Technologies

echo $row = mysql_num_rows($qr);


if($row > 0){
return true;
} else {
return false;
}
}
}

 PHP mysqli_query function


The mysqli_query function is used to execute SQL queries.

The function can be used to execute the following query types;

 Insert
 Select
 Update
 delete

 PHP mysqli_num_rows function


The mysqli_num_rows function is used to get the number of rows returned from a select
query.

 PHP mysqli_fetch_array function


The mysqli_fetch_array function is used fetch row arrays from a query result set.

 PHP require_once() function


In the case of the require_once(), if the file PHP file is missing, then a fatal error will arise
and no output is shown and the execution halts.

‘include’ and ‘require’ statements are identical, except upon failure:

 require will produce a fatal error (E_COMPILE_ERROR) and stop the script
 include will only produce a warning (E_WARNING) and the script will continue

Vous aimerez peut-être aussi