PHP User Login Form Authentication


PHP User Login Form Authenticatio :– This tutorial teaches you how to save user’s form data and then how to verify a user when he starts login after sign up process.
This is done because we want to restrict unauthorised users to access our information.This approach is very comman in all websites.

Example // How to create SignUp Form in html

<?php
<html>
<head>
<style>

.sub {

   font-family:Arial, Helvetica, sans-serif;

   font-size:14px;

   float: left;

   margin-left:30px;

   margin-top:8px;

   margin-bottom:8px;

   width:85%;

}

</style>
</head>
<body>

<form method="post" action="signup.php">
<img src="dog.jpg" width="200" height="175"
alt="My abducted dog Fang" />
<table>
<tr>
<td><label for="fname">First Name</label></td>
<td><input type="text" id="fname" name="fname"/></td>
</tr>
<tr>
<td><label for="lname">Last name</label></td>
<td><input type="text" id="lname" name="lname"/></td><br/>
</tr>

<tr>
<td><label for="password">What is your password</label></td>
<td><input type="password" id="password" name="password" /></td><br /> 
</tr>

<tr>
<td><input type="submit" value="Sign Up"  class="sub" /> </td>
</tr>
</table>
</form>
</body>
</html>

Example // how to register a user when he fills out form fields

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "employee";
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "CREATE TABLE User (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
passwordinfo VARCHAR(50),
reg_date TIMESTAMP
)";

if ($conn->query($sql) === TRUE) {
    echo "Table 'User' created successfully";
} else {
    echo "Error creating table: " . $conn->error;
}
$sql = "INSERT INTO User (firstname, lastname, passwordinfo) VALUES ('mysqli_real_escape_string($_POST[fname])', 'mysqli_real_escape_string($_POST[lname])', 'mysqli_real_escape_string($_POST[password])')";
if ($conn->query($sql) === TRUE) {
    echo "Record updated successfully";
	echo "<br>";
} else {
    echo "Error updating record: " . $conn->error;
}
$conn->close();
?>

Example //How to create login form in html

<?php
<html>
<head>
<style>

.sub {

   font-family:Arial, Helvetica, sans-serif;

   font-size:14px;

   float: left;

   margin-left:30px;

   margin-top:8px;

   margin-bottom:8px;

   width:85%;

}

</style>
</head>
<body>

<form method="post" action="login.php">
<img src="dog.jpg" width="200" height="175"
alt="My abducted dog Fang" />
<table>
<tr>
<td><label for="fname">First Name</label></td>
<td><input type="text" id="fname" name="fname"/></td>
</tr>
<tr>
<td><label for="lname">Last name</label></td>
<td><input type="text" id="lname" name="lname"/></td><br/>
</tr>

<tr>
<td><label for="password">What is your password</label></td>
<td><input type="password" id="password" name="password" /></td><br /> 
</tr>

<tr>
<td><input type="submit" value="Login"  class="sub" /> </td>
</tr>

</table>
</form>
</body>
</html>
?>

Example // User Authentication

<?php
$servername = "localhost";
$username = "root";
$passwordval = "";
$dbname = "employee";
$password=mysqli_real_escape_string($_POST['password']);
// Create connection
$conn = new mysqli($servername, $username, $passwordval,$dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$sql="SELECT passwordinfo,firstname,lastname FROM User WHERE passwordinfo='$password'";
$result=$conn->query($sql);
if($result->num_rows>0){
	
	while($row = $result->fetch_assoc()) {
		echo "Your user name is:".$row["firstname"]."@hotmail.com"."<br>";
		echo "login successfully";
} 

}else {
	echo "login failed";
}


$conn->close();
?>

Advertisements

Add Comment

📖 Read More