You are on page 1of 7

Experiment No 4 -Login/Registration using PHP

Submission Date: 09/09/2021

Esha Rakesh Shah


201071902, T. Y. B. Tech Comps
Open Source Computing Lab

AIM: -
To create login/registration forms for Web Applications using PHP script

To write validation code for fields in the Forms using PHP script

DESCRIPTION: -

Implementing User Authentication Mechanism

User authentication is very common in modern web application. It is a security mechanism


that is used to restrict unauthorized access to member-only areas and tools on a site.

In this tutorial we'll create a simple registration and login system using the PHP and MySQL.
This tutorial is comprised of two parts: in the first part we'll create a user registration form,
and in the second part we'll create a login form, as well as a welcome page and a logout
script.

Building the Registration System

In this section we'll build a registration system that allows users to create a new account by
filling out a web form. But, first we need to create a table that will hold all the user data.

Building the Login System

In this section we'll create a login form where user can enter their username and password.
When user submit the form these inputs will be verified against the credentials stored in the
database, if the username and password match, the user is authorized and granted access to
the site, otherwise the login attempt will be rejected.
CODE:-
Register.php

<!DOCTYPE html>

<html>

<body>

<h1> <center> Signup page </center> </h1>

<form method="Post">

<center>

Enter Username: <input type="Text" name="ui" placeholder="username" required>

Enter Password: <input type="Password" name="pass" placeholder="password" required>

Confirm Password: <input type="Password" name="conpass" placeholder="password" required>

Email-ID:<input type="Text" name="eid" placeholder="email_id" required>

<input type="Submit" name="submit" value="Register">

</center>

</form>

<?php

if(isset($_POST['submit']))

$dbhost = "localhost";

$dbuser = "root";

$dbpass = "pass@123";

$db = "osc_exp4";

$conn = new mysqli($dbhost, $dbuser, $dbpass,$db,"3304") or die("Connect failed: %s\n". $conn ->
error);

$userid=$_POST["ui"];

$pass=$_POST["pass"];

$con_pass=$_POST["conpass"];

$email_id=$_POST["eid"];

$sql="INSERT INTO users (user_id, password,con_password,email_id)

VALUES ('".$userid."','".$pass."','".$con_pass."','".$email_id."')";

$result=mysqli_query($conn,$sql);
if($result)

echo '<script> alert("Registered Successfully") </script>';

else

echo "Error".$sql. "" .mysqli_error($conn);

$conn -> close();

?>

Login.php

<!DOCTYPE html>

<html>

<body>

<b> Login Page </b>

<form method="Post">

Username: <input type="Text" name="ui" placeholder="username" required>

Password: <input type="Password" name="pass" placeholder="password" required>

<input type="Submit" name="submit" value="Login">

</form>

<?php

if(isset($_POST['submit']))

$dbhost = "localhost";

$dbuser = "root";

$dbpass = "pass@123";

$db = "osc_exp4";

$conn = new mysqli($dbhost, $dbuser, $dbpass,$db,"3304") or die("Connect failed: %s\n". $conn ->
error);
$userid=$_POST["ui"];

$pass=$_POST["pass"];

$sql="select * from users where user_id='".$userid."'AND password='".$pass."'";

$result1=mysqli_query($conn,$sql);

$rows1=mysqli_num_rows($result1);

if($rows1>0)

echo '<script> alert("'.$userid.' Logged in Successfully") </script>';

else

echo '<script> alert("'.$userid.' You are Already Logged in") </script>';

$conn -> close();

?>

</body>

</html>

Database (phpmyadmin)
OUTPUT: -
Signup Page

Successful signup result


Login Page

Successful Login Result


CONCLUSION: -
From this above experiment we learnt about creating a login and signup page using php and mysql.
In this experiment, we created a register page where the user can register and then that users’s data
would be stored in the database. After that the user would be redirected to the login page where he
can enter his credentials. Those credentials would be checked against the database and then the
user would be allowed or dis-allowed in the application.

You might also like