Complete tutorial for user registration system using PHP and MySQL database.

 

This is complete package for user registration system using PHP and MySQL database . This system tells the how new user can create the accounts by providing the username,email and password and how user can login and logout . If user is login then only access the success page otherwise can’t.

 

1st step –

Need to create database ‘registration_sytem‘ using the help of phpmyadmin—–

 

2nd step –

After creating the database just need to create ‘users’. This is following four fields for table.

id

username – varchar(230)

email – varchar(230)

password – varchar(230)

 

You can create the table by following SQL script-

 

 

 CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`username` varchar(230) NOT NULL,
`email` varchar(230) NOT NULL,
`password` varchar(230) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

 

3rd Step –

Now download xampp local server that should run on your computer or laptop. Then open htdocs folder    create folder registration_sytem

create file registration_process.php —

 

 

<?php include('server_process.php') ?>
<!DOCTYPE html>
<html>
<head>
  <title>Complete Registration system 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="registration_process.php">
  	<?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="email" name="email" value="<?php echo $email; ?>">
  	</div>
  	<div class="input-group">
  	  <label>Password</label>
  	  <input type="password" name="password">
  	</div>
  	<div class="input-group">
  	  <label>Confirm password</label>
  	  <input type="password" name="confirm_password">
  	</div>
  	<div class="input-group">
  	  <button type="submit" class="btn" name="registration">Register</button>
  	</div>
  	<p>
  		Already a member? <a href="login.php">Sign in</a>
  	</p>
  </form>
</body>
</html>

There are some important points that need to be describe –

 

<form> —- starting

</form> — Ending

It is used for transferring the data from one page to another page..

Action (attributes) – when you click on register button then it goes to the page which is mentioned in action attributes.

 

 

Step 4th —

Now we need to create file which is called server_process.php . This will take the responsibility for saving data to out database tbale ‘users‘.

 

<?php
session_start();             //defined session at top


$errors = array(); 

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

// REGISTER USER
if (isset($_POST['registration'])) {
  // getting all input values from the form
  $username = mysqli_real_escape_string($connect, $_POST['username']);
  $email = mysqli_real_escape_string($connect, $_POST['email']);
  $password = mysqli_real_escape_string($connect, $_POST['password']);
  $confirm_password = mysqli_real_escape_string($connect, $_POST['confirm_password']);

  
  
  if (empty($username)) { array_push($errors, "Username is empty!"); }
  if (empty($email)) { array_push($errors, "Email is empty!"); }
  if (empty($password)) { array_push($errors, "Password is empty!"); }
  if ($password != $confirm_password) {
  array_push($errors, "The two passwords do not match");
  }

  //check the user that already exits in our system by following code
  
  $user_check = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1";
  $result = mysqli_query($connect, $user_check);
  $user = mysqli_fetch_assoc($result);
  
  if ($user) { // if user exits in our database
    if ($user['username'] === $username) {
      array_push($errors, "Username already exists here");
    }

    if ($user['email'] === $email) {
      array_push($errors, "email already exists here");
    }
  }

  
  // if user not exists in our sytem then follow following and final step
  
  if (count($errors) == 0) {
  	$password = md5($password);//encrypt the password before saving in the database

  	$query = "INSERT INTO users (username, email, password) 
  			  VALUES('$username', '$email', '$password')";
  	mysqli_query($connect, $query);
  	$_SESSION['username'] = $username;
  	$_SESSION['success'] = "You are now logged in";
  	header('location: index.php');
  }
}

// ...

we need to describe about some points that are mentioned in above code.

session_start() – This is session function that are always defined at the top oh php script without any space. It is used for storing temporary data like user’s information.

how to define errors for every label .for that we need to create one errors.php file , where need to paste this following code..

 

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

 

When user registration successfully and store data to database then its redirect to login page ..

header(‘location: index.php’).

This function used for redirect for given page.

Now . create login page –

<?php include('server_process.php') ?>
<!DOCTYPE html>
<html>
<head>
  <title>Complete Registration system 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">
  	<?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" class="btn" name="login_user">Login</button>
  	</div>
  	<p>
  		Singnup? <a href="registration_process.php">Sign up</a>
  	</p>
  </form>
</body>
</html>

Just take deep breath and say ! yes ! yes ! going to rock !.

This is server_process.php , that we need to create coding for this and have a look at ..

// LOGIN USER
if (isset($_POST['login_user'])) {
  $username = mysqli_real_escape_string($db, $_POST['username']);
  $password = mysqli_real_escape_string($db, $_POST['password']);

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

  if (count($errors) == 0) {
  	$password = md5($password);
  	$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
  	$results = mysqli_query($db, $query);
  	if (mysqli_num_rows($results) == 1) {
  	  $_SESSION['username'] = $username;
  	  $_SESSION['success'] = "You are now logged";
  	  header('location: index.php');
  	}else {
  		array_push($errors, "Wrong username/password credential");
  	}
  }
}

?>

if everything putting right like username and password then its redirect to success page that is called success.php. have alook at how success.php page looks like ..

<?php 
  session_start(); 

  if (!isset($_SESSION['username'])) {
  	$_SESSION['message'] = "You must log in";
  	header('location: login.php');
  }
  if (isset($_GET['logout'])) {
  	session_destroy();
  	unset($_SESSION['username']);
  	header("location: login.php");
  }
?>
<!DOCTYPE html>
<html>
<head>
  <title>Success</title>
  <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

<div class="header">
  <h2>Success 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="success.php?logout='1'" style="color: red;">logout</a> </p>
    <?php endif ?>
</div>
    
</body>
</html>

As we defined about session how its work..

for storing temporary data using session , you need to use ..

$_SESSION[‘username’]..

Once you set session , when you need to destroy the session. you need to use session_destroy() function.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *