Login Register System Using PHP

Its been a long time since since I blogged. And so here I am writing a post to help you PHP newbies to get started with some good stuff. Here is tutorial to build a login register system using php. Almost every site in this world has a login registration system installed in them. So why dont you go on and create a login registration system on your own. This is really easy and simple than you think. So go ahead and get started with creating a login registration system on your own.

Getting Started With Login Register System Using PHP

The first thing that you need to do is to create a database where you are going to store all the details of the users including their username password and other details like email etc. So I here in this tutorial named my database as users and I am creating a table and 4 columns in it.

 

DOWNLOAD LOGIN REGISTER USING PHP SCRIPT

 

Below is the code which you can run in your phpmyadmin and create the required database.

CREATE DATABASE `users` ;

You can use the code following to create your table.

CREATE TABLE `users`.`users` (

`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`username` VARCHAR( 32 ) NOT NULL ,
`password` VARCHAR( 32 ) NOT NULL ,
`email` VARCHAR( 1024 ) NOT NULL

) ENGINE = MYISAM ;

And so now that you have completed creating the database which completes the first part in the Login Register system using php. Now you are gonna start to code with PHP.

The first thing that you need to do is to create a file which connects you to the database. You can include that file wherever needed. buy cialis online I am naming the file connect.php.

Connect .php

<?php

mysql_connect(“localhost”, “root”, “”);  //connects to the server

mysql_select_db(“users”);  //selects the database

?>

In the first line you are connecting to your server. I am provided with the default credentials. If you have changed them to your needs then you need to alter them.

Now lets create the file where you are gonna allow the users to register.

Register.php

Register

<form action = “register.php” method = “post”>

Username : <input type = “text” name = “username”>

Password : <input type = “password” name = “password”>

Email : <input type = “email” name = “mail”>

<input type = “submit” value = “Register”>

</form>

<?php

include ‘connect.php’;

$username = $_POST['username'];

$password = md5($_POST['password']);

$email = $_POST['mail'];

$userexists = mysql_num_rows(mysql_query(“SELECT username FROM users WHERE username = ‘$username’”));

if(empty($username) || empty($password) || empty($email){

echo “All Fields are mandatory”;

}
else if($userexists == 1){

echo “The user already exists”;

exit();

}

mysql_query(“INSERT INTO `users`.`users` (`id` ,`username` ,`password` ,`email`) VALUES (NULL , ‘$username’, ‘$password’, ‘$email’);”);
?>

Now here comes the important part of it. You need to create a file called as main.php which starts a session and it controls whether the user is logged in or not.

Main.php

<?php

session_start();

?>

Lets get started with creating the login file.

Login.php

<form action = “login.php” method = “post”>

Username : <input type = “text” name = “username”>

Password : <input type = “password” name = “password”>

<input type = “submit” value = “Login”>

</form>

You need to create the login PHP script. Add the following in your file.

<?php

include ‘main.php’;

include ‘connect.php’;

$username = $_POST['username'];

$password = md5($_POST['password']);

$query = mysql_query(“SELECT id,username, password FROM users WHERE username = ‘$username’”);

if(!$query)

echo “There is no such user with the entered username”;

else{

$details = mysql_fetch_assoc($query);

$usr = $details['username'];

$pass = $details['password'];

$id = $details['id'];

if($pass == $password){

$_SESSION['id'] = $id;

header(“Location: loggedin.html”);

}

else

echo “The username and password combination does not match”;

}

?>

And now you need to create a page where you need to redirect the user after he logs in. I have named the page as loggedin.php which contains a message “Welcome user” and the logout link.

Loggedin.php

<?php

include ‘main.php’;

if(!isset($_SESSION['id']))

echo “You need to login to view this page”;

else{

echo “Welcome User”;

echo “<a href = ‘logout.php’>Logout</a>”;

}

?>

Here comes the final part of the tut. You are now about to create the logout page. Thats pretty simple. Lets see on how to do that.

Logout.php

<?php

session_start();

session_destroy();

header(“Location: login.php”);

?>

Thats it ! You have completed creating the login register system using php. Post your doubts as comments. Stay tuned for more such tuts on PHP.

Latest Comments
  1. Carter

    I don’t want to make you look bad, but I’m over 100% sure that this will give the error “Undefined Index” for ‘username’, ‘password’, ‘mail’.

  2. expertsahil

    Carter! HE also provide for ‘username’, ‘password’, ‘mail’,”" for filling your password

Leave a Reply

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