User Registration and Login System - PHP and MySQL
The main page is accessible only to logged in user. Any other users not logged in will not be able to access in their information page. This program is perfect for new developers who want to reuse the code on for just to have fun with it.
Yo can watch the whole process of running this system on my YouTube channel in the video below:
File structure
registration.sql
CREATE TABLE users(
id INT(11) NOT NULL AUTO_INCREMENT,
firstname VARCHAR(50) NOT NULL,
lastname VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL,
username VARCHAR(50) NOT NULL,
password text(50) NOT NULL,
gender VARCHAR(10) NOT NULL,
birthday DATE NOT NULL,
created_date DATETIME NOT NULL,
PRIMARY KEY(id)
);
index.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>login, Simple PHP Login and Register System</title>
</head>
<body>
<ul align='right'>
<a href="signup.php">Sign up</a>
</ul>
<br><br>
<form method='post' action='index.php'>
<table width='400' border='3' align='center'>
<tr>
<th bgcolor='dodgerBlue' colspan='2'>Sign in</th>
</tr>
<tr >
<td align='right'>Username:</td>
<td><input type='text' name='username' maxlength="50" required>
</td>
</tr>
<tr>
<td align='right'>Password:</td>
<td><input type='password' name='password' maxlength="50" required>
</td>
</tr>
<tr>
<td align='center' colspan='2'>
<input type='submit' name='submit' value='Submit'>
</td>
</tr>
</table>
</form>
</body>
</html>
<?php
$conn = mysqli_connect("localhost","root","","registration");
if(!$conn){
die("Connection failed: ".mysql_connect_error());
}
if(isset($_POST['submit'])){
$user_name = $_POST['username'];
$user_password = md5($_POST['password']);
$sql = "SELECT * FROM users WHERE username = '$user_name' AND password = '$user_password'";
$query = mysqli_query($conn,$sql) or die("Bad Query: $sql");
$row=mysqli_fetch_row($query);
$resultcheck = mysqli_num_rows($query);
$_SESSION['id'] = $row[0];
$_SESSION['firstname'] = $row[1];
$_SESSION['lastname'] = $row[2];
$_SESSION['email'] = $row[3];
$_SESSION['gender'] = $row[6];
$_SESSION['birthday'] = $row[7];
$_SESSION['created_date'] = $row[8];
if($resultcheck > 0){
$_SESSION['user_name'] = $user_name;
echo "<script>window.open('init.php','_self')</script>";
}else{
echo "<script>alert('Incorrect username or password.')</script>";
}
}
?>
signup.php
<!DOCTYPE html>
<html>
<head>
<title>Signup, Simple PHP Login and Register System</title>
</head>
<body>
<ul align='right'>
<a href="index.php">Sign in</a>
</ul>
<br><br>
<form method='post' action='signup.php'>
<table width='500' border='3' align='center'>
<tr>
<th bgcolor='dodgerBlue' colspan='2'>Registration Form</th>
</tr>
<tr>
<td align='right'>First Name:</td>
<td><input type='text' name='firstname' maxlength="50" required >
</td>
</tr>
<tr>
<td align='right'>Last Name:</td>
<td><input type='text' name='lastname' maxlength="50" required >
</td>
</tr>
<tr>
<td align='right'>Email :</td>
<td><input type='email' name='email' maxlength="50" required>
</td>
</tr>
<tr>
<td align='right'>Username: </td>
<td><input type='username' name='username' maxlength="50" required>
</td>
</tr>
<tr>
<td align='right'>Password: </td>
<td><input type='password' name='password' maxlength="50" required>
</td>
</tr>
<tr>
<td align='right'>Confirm Password: </td>
<td><input type='password' name='password2' maxlength="50" required>
</td>
</tr>
<tr>
<td align='right'>Gender: </td>
<td>
<input type="radio" name="gender" value="male" checked> Male
<input type="radio" name="gender" value="female"> Female
<input type="radio" name="gender" value="other"> Other
</td>
</tr>
<tr>
<td align='right'>Birthday: </td>
<td><input type='date' name='birthday' required>
</td>
</tr>
<tr>
<td align='center' colspan='2'>
<input type='submit' name='submit' value='Submit'>
</td>
</tr>
</table>
</form>
</body>
</html>
<?php
$conn = mysqli_connect("localhost","root","","registration");
if(!$conn){
die("Connection failed: ".mysql_connect_error());
}
if(isset($_POST['submit']))
{
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$username = $_POST['username'];
$password = md5($_POST['password']);
$gender = $_POST['gender'];
$birthday = $_POST['birthday'];
if($_POST['password'] != $_POST['password2']){
echo "<center><b><br>Passwords do not match please try again.</b></center><br>";
}else{
$sql = "INSERT INTO users(firstname, lastname, email, username, password, gender, birthday, created_date) VALUES('$firstname', '$lastname','$email','$username','$password','$gender','$birthday',NOW())";
$query = mysqli_query($conn, $sql);
if($query){
echo "<center><b><br>Registration completed successfully!</b></center><br>";
}else{
echo "<center><b><br>Something went wrong please try again!</b></center><br>";
}
}
}
?>
init.php
<?php
session_start();
if (!$_SESSION['user_name']){
header('location:index.php');
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Simple PHP Login and Register System</title>
</head>
<body>
<ul align='right'>
Welcome:
<font color="blue">
<?php echo $_SESSION['user_name']; ?>
</font>
<a href="logout.php">Log out</a>
</ul>
<br><br>
<form>
<table width='500' border='3' align='center'>
<tr>
<th bgcolor='dodgerBlue' colspan='2'>Profile Information</th>
</tr>
<tr>
<td align='right'>First Name:</td>
<td><?php echo $_SESSION['firstname']; ?></td>
</tr>
<tr>
<td align='right'>Last Name:</td>
<td><?php echo $_SESSION['lastname']; ?></td>
</tr>
<tr>
<td align='right'>Email :</td>
<td><?php echo $_SESSION['email']; ?></td>
</tr>
<tr>
<td align='right'>Username: </td>
<td><?php echo $_SESSION['user_name']; ?></td>
</tr>
<tr>
<td align='right'>Gender: </td>
<td><?php echo $_SESSION['gender']; ?></td>
</tr>
<tr>
<td align='right'>Birthday: </td>
<td><?php echo $_SESSION['birthday']; ?></td>
</tr>
</table>
</form>
</body>
</html>
logout.php
<?php
session_start();
session_destroy();
header('location:index.php')
?>
Comments
Post a Comment