Well you know your data is totally 100% Secure under some Organization and you disclose them your private things but you know the Path your data choose to travel to reach destination well today i am on my way to tell you What happen in Backend when you submit a login form whether you signup or sign in
You well Heard about SQL a structured query language where data is well well if you don't know giving some short brief:
- SQL can create new databases
- SQL can insert records in a database
- SQL can execute queries against a database
- SQL can retrieve data from a database
- SQL can update records in a database
- SQL can delete records from a database
- SQL can create new tables in a database
- SQL can create stored procedures in a database
- SQL can create views in a database
- SQL can set permissions on tables, procedures, and views.
Designing a database for our login form or you may say for your website we will discuss it later but first of all step by step process what happen just after you enter your credential:
Once you enter your email id, password and enter Submit button it will hit the code which will look something like this
It will first grab the UserName from Username box and store that, Similarly it will store the password into our Password Variable and finally we built a SQL table for User query the user typed in understand why i used capital U and smaller one two different ways
with the query +tbUserName which means a user can directly inject the SQL queries or rather SQL code modify the table
Now after these steps our SQL queries will looks like this:
"SELECT * FROM Users WHERE UserName = 'admin' "
which means select each column from a Users table where the username is admin (in our case).
Likewise the code run the same for our password where we provide some data for our password
How it affects the Table you will understand here after your data is submitted it will fill out in a manner like this
"Lets discuss an another thing, The whole process takes me about 10 minutes to write, you will read it in just 1 minute but Unbelievable the system only takes about a nanosecond to do the whole process done."
Create a Database Table Using MySQL
When you create a database, you're creating a structure like you see above. Now, start your SQL Xampp server and select admin. This will direct you to the phpMyAdmin webpage. Now, log in with your username and password.
And on the MyPHPAdmin, create a new database by clicking "New" on the left-hand side of a webpage.
Next, enter a name for the database where it says "Create database".
Then, you will be directed to the next page where you will create a new table. Now, enter the desired table name. Next, you can select the desired type number for the ‘Fields’ text box.
Note: The fields are the columns; some of the fields are username, password, address, etc. In the text box, enter the number '4'. You can also increase the number based on the requirement. Once you have completed the steps, click the Go button. In the type space, select these options.
Next, set a primary key for the ID field by selecting the drop-down list option and select save.
Note: Suppose you receive a #1067 error in later versions of phpMyAdmin, change the Default drop-down list from Timestamp to None.
Once you save this, you will be directed to the structure screen. This should be the final output:
Open a Connection to a MySQL Database
Once you create a PHPMyAdmin database, your next step is to write a code on Visual Studio.
Go to Microsoft visual code -> create a new file and name it as DB connection.
Now, in the code below, you will notice the function mysqli_connect( ). As the name suggests, it does the same task. It connects the database to the form that was created.
<?php
$sname= "localhost";
$unmae= "root";
$password = "";
$db_name = "test_db";
$conn = mysqli_connect($sname, $unmae, $password, $db_name);
if (!$conn) {
echo "Connection failed!";
}
This demo has used an if statement to check whether the code is working or not. A suitable message is printed, depending on if the database was found.
Now, execute the below SQL query to create the user table within your MySQL database.
<?php
session_start();
include "db_conn.php";
if (isset($_POST['uname']) && isset($_POST['password'])) {
function validate($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$uname = validate($_POST['uname']);
$pass = validate($_POST['password']);
if (empty($uname)) {
header("Location: index.php?error=User Name is required");
exit();
}else if(empty($pass)){
header("Location: index.php?error=Password is required");
exit();
}else{
$sql = "SELECT * FROM users WHERE user_name='$uname' AND password='$pass'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) === 1) {
$row = mysqli_fetch_assoc($result);
if ($row['user_name'] === $uname && $row['password'] === $pass) {
echo "Logged in!";
$_SESSION['user_name'] = $row['user_name'];
$_SESSION['name'] = $row['name'];
$_SESSION['id'] = $row['id'];
header("Location: home.php");
exit();
}else{
header("Location: index.php?error=Incorect User name or password");
exit();
}
}else{
header("Location: index.php?error=Incorect User name or password");
exit();
}
}
}else{
header("Location: index.php");
exit();
}
Now, if you log in using the wrong credentials, you will get the following output.
Create a Logout Session
In this section, you will create a "logout.php" file.
When a user selects the logout option, the code mentioned below will automatically redirect the user back to the login page.
<?php
session_start();
session_unset();
session_destroy();
header("Location: index.php");
Do you have any questions for us? what you think about this blog were satisfying or not please feel free to put them in the comments section...
πΏπππππ & π½ππππππ...
0 comments:
Post a Comment