Blog » PHP » Login register System using PHP and MySql Database with Session

Login register System using PHP and MySql Database with Session

This tutorial will show you how to create login register page using php mysql. user registration and login is an important feature of any website. Any other user not logged in will not be able to access the page. therefore user registration and user login feature very important part of the website. that's why We will cover this tutorial in easy steps create login register page.and very easy to understand this script to create new one and you can get a better idea to create new login register page. and you can also download this completed source code.

config.php


<?php 
$hostname = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "register";
$con = new mysqli($hostname, $dbuser, $dbpass, $dbname);
if($con->connect_error){
die("Error connect to db");
}
?>


core.php


<?php
function validateInput($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>


register.php


<?php 
include "core.php";
include "config.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Register</title>
<style>
.register input[type="text"]{
padding:10px;
border:solid 1px #ddd;
width:100%;
margin-bottom:5px;
}
.register input[type="password"]{
padding:10px;
border:solid 1px #ddd;
width:100%;
margin-bottom:5px;
}
.register input[type="submit"]{
padding:10px;
border:solid 1px #ddd;
background:#09C;
font-family:Arial, Helvetica, sans-serif;
}
.register{
padding-left:220px;
padding-right:220px;
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
color:#666;
cursor:pointer;
}
h3{
text-align:center;
}
</style>
</head>
<body>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){
$username = validateInput($_POST['username']);
$email = validateInput($_POST['email']);
$password = validateInput($_POST['password']);
$stmt = $con->prepare("INSERT INTO users (username,email,password) VALUES (?, ?, ?)"); 
$stmt->bind_param("sss", $username, $email, md5($password)); 
if($stmt->execute()){
echo "Registration completed successfully!<br/>";
}else{
echo "Registration failed<br/>";
}
}	
?>
<div class="register"><h3>Register form</h3>
<form id="form" name="form" method="post" action="register.php">
<label>Usernme</label><br/>
<input name="username" type="text" id="username" /><br/>
<label>Email</label><br/>
<input name="email" type="text" id="email" /><br/>
<label>Password</label><br/>
<input name="password" type="password" id="password" /><br/>
<input type="submit" name="Submit" value="Register" />
</form>
</div>
</body>
</html>


Login.php


<?php 
include "core.php";
include "config.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login</title>
<style>
.login input[type="text"]{
padding:10px;
border:solid 1px #ddd;
width:100%;
margin-bottom:5px;
}
.login input[type="password"]{
padding:10px;
border:solid 1px #ddd;
width:100%;
margin-bottom:5px;
}
.login input[type="submit"]{
padding:10px;
border:solid 1px #ddd;
background:#09C;
font-family:Arial, Helvetica, sans-serif;
}
.login{
padding-left:220px;
padding-right:220px;
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
color:#666;
cursor:pointer;
}
h3{
text-align:center;
}
</style>
</head>
<body>
<?php 
if($_SERVER['REQUEST_METHOD'] == "POST"){
$email = validateInput($_POST['email']);
$password = md5(validateInput($_POST['password']));
$sql = "SELECT * FROM users WHERE email='$email' AND password='$password'";
$result = $con->query($sql);
if($result->num_rows > 0){
//login success
$row=$result->fetch_assoc();
$_SESSION["username"] = $row['username']; //set session
setcookie("username", $row['username'], time()+(86400*30), "/"); //set cookie
header("Location: index.php");
}else{
//login failed
echo "Invalid login<br/>";
}
}
?>
<div class="login"><h3>Login form</h3>
<form id="form" name="form" method="post" action="login.php">
<label>Email</label><br/>
<input name="email" type="text" id="email" /><br/>
<label>Password</label><br/>
<input name="password" type="password" id="password" /><br/>
<input type="submit" name="Submit" value="Login" />
</form>
</div>
</body>
</html>


index.php


<?php 
session_start();
if(!isset($_SESSION['username']) || empty($_SESSION['username'])){
header("location: login.php");
exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>home</title>
</head>
<body>
<h3>Hi, <b><?php echo $_SESSION['username']; ?></b>. Welcome to our site.</h3>
</body>
</html>


ARTICLE POSTED BY : Admin

Hi, I'am k.g uththara. I'm a professional web developer. I write blog posts in my free time. if you want to contact me, send a message on contact page, thanks.