Complete User Registration System using PHP and MySql database

                                         



register.php

<?php include('server.php'); ?>
<!DOCTYPE html>
<html>
<head>
<title>User registration system using PHP and MySQL</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="header">
<h2>Register</h2>
</div>

<form method="post" action="register.php">

<!-- display validation errors here -->
<?php include('errors.php'); ?>

<div class="input-group">
<label>Username</label>
<input type="text" name="username" value="<?php echo $username; ?>">
</div>
<div class="input-group">
<label>Email</label>
<input type="text" name="email" value="<?php echo $email; ?>">
</div>
<div class="input-group">
<label>Password</label>
<input type="password" name="password_1">
</div>
<div class="input-group">
<label>Confirm Password</label>
<input type="password" name="password_2">
</div>
<div class="input-group">
<button type="submit" name="register" class="btn">Register</button>
</div>
<p>
Already a member? <a href="login.php">Sign in</a>
</p>
</form>
</body>
</html>

Create a database


style.css

* {
margin: 0px;
padding: 0px;
}
body {
font-size: 120%;
background: #F8F8FF;
}

.header {
width: 30%;
margin: 50px auto 0px;
color: white;
background: #5F9EA0;
text-align: center;
border: 1px solid #B0C4DE;
border-bottom: none;
border-radius: 10px 10px 0px 0px;
padding: 20px;
}

form, .content {
width: 30%;
margin: 0px auto;
padding: 20px;
border: 1px solid #B0C4DE;
background: white;
border-radius: 0px 0px 10px 10px;
}

.input-group {
margin: 10px 0px 10px 0px;
}

.input-group label{
display: block;
text-align: left;
margin: 3px;
}

.input-group input {
height: 30px;
width: 93%;
padding: 5px 10px;
font-size: 16px;
border-radius: 5px;
border: 1px solid gray;
}
.btn {
padding: 10px;
font-size: 15px;
color: white;
background: #5F9EA0;
border: none;
border-radius: 5px;
}
.error {
width: 92%;
margin: 0px auto;
padding: 10px;
border: 1px solid #a94442;
color: #a94442;
background: #f2dede;
border-radius: 5px;
text-align: left;
}

.success {
color: #3c763d;
background: #dff0d8;
border: 1px solid #3c763d;
margin-bottom: 20px;
}



login.php

<?php include('server.php'); ?>
<!DOCTYPE html>
<html>
<head>
<title>User registration system using PHP and MySQL</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="header">
<h2>Login</h2>
</div>

<form method="post" action="login.php">
<!-- display validation errors here -->
<?php include('errors.php'); ?>

<div class="input-group">
<label>Username</label>
<input type="text" name="username">
</div>
<div class="input-group">
<label>Password</label>
<input type="password" name="password">
</div>
<div class="input-group">
<button type="submit" name="login" class="btn">Sign in</button>
</div>
<p>
Already a member? <a href="register.php">Sign up</a>
</p>
</form>
</body>
</html>

server.php

<?php
session_start();
$username = "";
$email = "";
$errors = array();

//connect to the database
$db = mysqli_connect('localhost', 'root', '','registration');

//if the register button is clicked
if (isset($_POST['register'])){
$username = $db->real_escape_string($_POST['username']);
$email = $db->real_escape_string($_POST['email']);
$password_1 = $db->real_escape_string($_POST['password_1']);
$password_2 = $db->real_escape_string($_POST['password_2']);

//ensure that form fields are filled properly
if (empty($username)){
array_push($errors, "Username is required");

}
if (empty($email)){
array_push($errors, "Email is required");
}
if (empty($password_1)){
array_push($errors, "Password is required");
}

if($password_1 != $password_2){
array_push($errors, "The two passwords do not match");
}

//if there are no errors, save user to database
if (count($errors) == 0){
$password = md5($password_1); // encrypt password before storing in database (security)
$sql = "INSERT INTO users (username, email, password) VALUES ('$username','$email', '$password')";
mysqli_query($db, $sql);
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are Logged in";
header('location: index.php'); //redirect to home page
}

}

// log user in from login page
if (isset($_POST['login'])){
$username = $db->real_escape_string($_POST['username']);
$password = $db->real_escape_string($_POST['password']);

//ensure that form fields are filled properly
if (empty($username)){
array_push($errors, "Username is required");

}
if (empty($password)){
array_push($errors, "Password is required");
}

if (count($errors) == 0){
$password = md5($password); // encrypt password before comparing with that from database
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = mysqli_query($db, $query);
if(mysqli_num_rows($result) == 1){
// log user in 
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are Logged in";
header('location: index.php'); //redirect to home page

}else{
array_push($errors, "Wrong username/password combination");
//header('location: login.php');
}
}
}


//logout
if (isset($_GET['logout'])){
session_destroy();
unset($_SESSION['username']);
header('location: login.php');
}



?>

errors.php

<?php if (count($errors) > 0): ?>

<div class="error">
<?php foreach ($errors as $error): ?>
<p><?php echo $error; ?></p>
<?php endforeach ?>
</div>
<?php  endif ?>


index.php

<?php include('server.php'); 

//if user is not logged in, they cannot access this page 
if (empty($_SESSION['username'])){
header('location: login.php');
}
?>
<!DOCTYPE html>
<html>
<head>
<title>User registration system using PHP and MySQL</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="header">
<h2>Home Page</h2>
</div>

<div class="content">
<?php if (isset($_SESSION['success'])): ?>
<div class="error success">
<h3>
<?php
echo $_SESSION['success'];
unset($_SESSION['success']);
?>
</h3>
</div>
<?php endif ?>

<?php if(isset($_SESSION['username'])): ?>
<p>Welcome <strong><?php echo $_SESSION['username']; ?></strong></p>
<p><a href="index.php?logout='1'" style="color: red;">Logout</a></p>
<?php endif ?>
</div>
</body>
</html>

Comments

Popular posts from this blog

Tang Maphi _ Lyrics_DJ Wanshan ft. Kyntiewlin Mawphniang

Khasi Catholic Hymns Lyrics