Complete PHP User Login and Registration System with Image upload, In this article we create both Login and Registration system where we also upload images for all the specific users on the part of registration.

Here are some major Steps to create a PHP User Login and Registration System with Image upload,
Table of Content
- 1 5+ Steps to Create Complete Login and Registration System with PHP
- 2 Image Upload using PHP MySQL Snippet
5+ Steps to Create Complete Login and Registration System with PHP
- Create Database
- Create Database Table with the following fields: name, username, password, city, image, gender.
- Create a PHP registration functionality
- Create a Login Form
- Connect to the MySQL Database
- Authenticate Logged in User
- Create a Home Page with all User data
Create Database
To creating a database, go to PHPMyAdmin and click on SQL Tab. On SQL tab write below given query which create the DB with the given name.
1 |
CREATE TABLE `reg` |
Create Database Table
Execute below query which can create the DB table with listed fields,
1 2 3 4 5 6 7 8 9 |
CREATE TABLE `reg` ( `name` varchar(20) NOT NULL, `username` varchar(20) NOT NULL, `password` varchar(50) NOT NULL, `city` varchar(15) NOT NULL, `image` varchar(100) NOT NULL, `gender` varchar(10) NOT NULL, `id` int(11) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; |
Connect to Database
To establish the connection to MySQL DB or PHPMYADMIN we create a file named connect.php
where we use mysqli_connect()
function.
connect.php
1 2 3 4 |
<?php session_start(); $con= mysqli_connect("localhost","DBUSERNAME","DBPASSWORD","DBNAME") ?> |
Create a PHP registration functionality
Here we create a HTML form with POST method, also get the user data using POST method and insert it to database with image upload.
reg.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
<?php include 'connect.php'; if(isset($_POST['sub'])){ $t=$_POST['text']; $u=$_POST['user']; $p=$_POST['pass']; $c=$_POST['city']; $g=$_POST['gen']; //code for image uploading if($_FILES['f1']['name']){ move_uploaded_file($_FILES['f1']['tmp_name'], "image/".$_FILES['f1']['name']); $img="image/".$_FILES['f1']['name']; } $i="insert into reg(name,username,password,city,image,gender)values('$t','$u','$p','$c','$img','$g')"; if(mysqli_query($con, $i)){ echo "inserted successfully..!"; } } ?> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <form method="POST" enctype="multipart/form-data"> <table> <tr> <td> Name <input type="text" name="text"> </td> </tr> <tr> <td> Username <input type="text" name="user"> </td> </tr> <tr> <td> password <input type="password" name="pass"> </td> </tr> <tr> <td> city <select name="city"> <option value="">Select</option> <option value="knp">kanpur</option> <option value="lko">lucknow</option> </select> </td> </tr> <tr> <td> Gender <input type="radio"name="gen" id="gen" value="male">male <input type="radio" name="gen" id="gen" value="female">female </td> </tr> <tr> <td> Image <input type="file" name="f1"> </td> </tr> <tr> <td> <input type="submit" value="submit" name="sub"> </td> </tr> </table> </form> </body> </html> |
Creating a Login Form with Functionality
Here we create a HTML form with username and password field. Also we create user authentication query using same username and password MySQL query and redirect to home page.
login.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<?php include'connect.php'; if(isset($_POST['sub'])){ $u=$_POST['user']; $p=$_POST['pass']; $s= "select * from reg where username='$u' and password= '$p'"; $qu= mysqli_query($con, $s); if(mysqli_num_rows($qu)>0){ $f= mysqli_fetch_assoc($qu); $_SESSION['id']=$f['id']; header ('location:home.php'); } else{ echo 'username or password does not exist'; } } ?> <html> <head> <title>User Login Form</title> </head> <body> <form method="POST" enctype="multipart/form-data"> <table> <tr> <td> Username <input type="text" name="user"> </td> </tr> <tr> <td> password <input type="password" name="pass"> </td> </tr> <tr> <td> <input type="submit" name="sub" value="submit"> </td> </tr> </table> </form> </body> </html> |
Authenticate Logged in User on Redirection
Here we authenticate the user on the home page redirection after login. If anyone directly wants to access the home page it checks the user is currently logged in or not.
1 2 3 4 5 6 7 |
<?php session_start(); if(!isset($_SESSION["id"])){ header("Location: login.php"); exit(); } ?> |
Creating Home Page
After successful login user enters to the home page and also see their data with image and also log out from there.
home.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<?php session_start(); if(!isset($_SESSION["id"])){ header("Location: login.php"); exit(); } include'connect.php'; $s="select*from reg where id='$_SESSION[id]'"; $qu= mysqli_query($con, $s); $f=mysqli_fetch_assoc($qu); ?> <html> <body> <table> <tr> <td>Name</td> <td><?php echo $f['name'];?></td> </tr> <tr> <td> Username</td> <td><?php echo $f['username'];?></td> </tr> <tr> <td>Password</td> <td><?php echo $f['password']."<br>";?></td> </tr> <tr> <td> City </td> <td><?php echo $f['city']."<br>";?></td> </tr> <tr> <td>Gender</td> <td><?php echo $f['gender']."<br>";?></td> </tr> <tr> <td> Image</td> <td><img src="<?php echo $f['image'];?>" width="100px" height="100px"></td> </tr> </table> <a href="logout.php">Logout</a> </body> </html> |
Creating a Logout page
For creating logout operation we have to destroy session using PHP session_destroy()
function.
logout.php
1 2 3 4 5 6 7 8 9 |
<?php session_start(); // Destroying All Sessions if(session_destroy()) { // Redirecting To Home Page header("Location: login.php"); } ?> |
Image Upload using PHP MySQL Snippet
For uploading the image we make sure using
enctype="multipart/form-data"
on form tag, like given below.
1 |
<form method="POST" enctype="multipart/form-data"> |
For image uploading the PHP MySQL code snippet is given below,
1 2 3 4 5 6 7 |
<?php //code for image uploading if($_FILES['f1']['name']){ move_uploaded_file($_FILES['f1']['tmp_name'], "image/".$_FILES['f1']['name']); $img="image/".$_FILES['f1']['name']; } ?> |
To know more about file upload: https://www.php.net/manual/en/features.file-upload.php
Download Simple PHP CRUD Operation source code:
Also, check:
- jQuery Contact Form Send Email Using Ajax
- How to Store Data In Cookies Using JavaScript
- Steps to Upload Multiple Images in PHP with Preview
- Multiple Barcode Generator in PHP
Complete tutorial to PHP User Login and Registration System with Image upload which helps to develop a small PHP project.
Happy Coding..!
Its a very good example sir..
You are a great
dear Admin I need to View Sql Data(images) in horizontal same line.
my source code is as follows please help me:
runQuery(“SELECT * FROM `tbl_image` ORDER BY ‘id’ ASC “);
if (! empty($query)) {
foreach ($query as $key => $value) {
?>
<img src="”/>
<img src="width=”96px” height=”56px”/>
Hi,
Please check this. If anything else please let me know.
<?php
$qry = mysqli_query(“SELECT * FROM `tbl_image` ORDER BY ‘id’ ASC”);
while($f=mysqli_fetch_assoc($qry)){ ?>
<img src=”image_directory_loaction/<?php echo $f[‘image.png’];?>” width=”96px” height=”56px”>
<?php } ?>