php-login with cookie and Authentication Using Session
What Will I Learn?
In This Tutorial, We are learning the Login With Cookie and Authentication Using Session.
- We Will learn from this tutorial How to Login with Cookie
- Also Learn from this Tutorial, Login Authentication Using Session.
Requirements
There are Some requirements for learning this tutorial in the given below :
- Basic Knowledge of Cookie
- Basic Knowledge of Session
- Establish Database Connection
Difficulty
There is no difficulty in this tutorial. It is Primary Level and easy to learn.
- Basic
Tutorial Contents
In This Tutorial, We are mainly focused the php-login with Cookie and Login-Authentication Using Session.
php-Login With Cookie : First Of all We discuss What is Cookie .
Cookie : Cookie is Used to Identify a User. Cookie Saves in Browser. In php , User can Create and Retrieve the Cookies Value.
Let's we discuss php-Login using cookie - First of all , we create a Login platform using php , Login platform have Id and password for Logged In the Home page .
Let's see an example of the Login page Code -
<?php
if(isset($_COOKIE['id'])){
session_start();
$_SESSION['id']=$_COOKIE['id'];
header("location: home.php");
}
if($_SERVER['REQUEST_METHOD']=="POST"){
$id = $_POST['id'];
$password = $_POST['password'];
$remember = $_POST['remember'];
$conn = mysqli_connect("127.0.0.1", "root", "", "user_db", 3306);
$result = mysqli_query($conn, "SELECT password FROM person WHERE id=$id");
$row = mysqli_fetch_assoc($result);
if($row['password']==$password){
if(isset($remember)){
setcookie("id", $id, time()+60);
}
session_start();
$_SESSION['id']=$id;
header("location: home.php");
}
else{
echo "Invalid User ID or Password";
}
}
?>
<form method="post">
Login<hr/>
ID:<br/><input name="id"/>
<br/>Password:<br/><input type="password" name="password"/>
<br/><input type="checkbox" name="remember"/>Remember ME
<hr/><input type="submit"/>
</form>
After running the program, we see the following Output-
Database Structure- Here is a database of person that's include person's id and password.
After Successfully Login , We go to the Home page .
Let's see an example of home.php code-
<?php
session_start();
if(isset($_SESSION['id'])==false){
header("location: login.php");
}
?>
<a href="logout.php">logout</a>
<?php
var_dump($GLOBALS);
?>
After running the home.php code we will see the following Output-
After Visiting the home page , We can easily Click Logout button and returning to the Login page -
Let's see an example of Logout .php code-
<?php
session_start();
//unset($_SESSION['id']);
session_destroy();
header("location: login.php");
?>
After clicking the Logout button , we see the following output-
Here, is the rest of the part of this tutorial discuss in the given below :
Login Authentication Using Session : Let's we discuss about the Session.
Session : In Theoritically, Between Session Starting time and Ending Time , It's performed some action. It's Client Specific.
Here is an example of Session Login Code -we create a Login platform using php , Login platform have Id and password for Logged In the Home page .
<?php
if($_SERVER['REQUEST_METHOD']=="POST"){
$id = $_POST['id'];
$password = $_POST['password'];
$conn = mysqli_connect("127.0.0.1", "root", "", "user_db", 3306);
$result = mysqli_query($conn, "SELECT password FROM person WHERE id=$id");
$row = mysqli_fetch_assoc($result);
if($row['password']==$password){
session_start();
$_SESSION['id']=$id;
header("location: home.php");
}
else{
echo "Invalid User ID or Password";
}
}
?>
<form method="post">
Login<hr/>
ID:<br/><input name="id"/>
<br/>Password:<br/><input type="password" name="password"/>
<hr/><input type="submit"/>
</form>
After running the following program , We will see the output -
After Successfully Login , We go to the Home page .
Let's see an example of home.php code-
<?php
session_start();
if(isset($_SESSION['id'])==false){
header("location: login.php");
}
?>
<a href="logout.php">logout</a>
<?php
var_dump($GLOBALS);
?>
After running the program, We see the following output-
Here , We see Logout.php code- After checking the home page we logout the home page and returning to the Login page-
<?php
session_start();
//unset($_SESSION['id']);
session_destroy();
header("location: login.php");
?>
After running the program , We will see the following Output-
Curriculum
Here is an previous tutorial link-
Posted on Utopian.io - Rewarding Open Source Contributors
Your contribution cannot be approved because it does not follow the Utopian Rules.
Violated Rule:
Design or video editing related tutorials, gameplay, simple on-screen instructions, ubiquitous functions (Save, Open, Print, etc.) or basic programming concepts (variables, operators, loops, etc.) will not be accepted.
The writing style should be professional.
My Opinion:
A tutorial must be informative and explanatory, but also "tutor". This tutorial lacks "tutor"ing, and it is nothing more than "Do whatever I do." kind of tutorial.
Also, there are other tutorials which shows session better than this tutorial. So, showing "login example" does not change our subject. Our subject is still session.
You can contact us on Discord.
[utopian-moderator]
This post has received a 0.70% upvote from thanks to: @mcplexer.
For more information, click here!!!!
Send minimum 0.050 SBD|STEEM to bid for votes.
Do you know, you can also earn daily passive income simply by delegating your Steem Power to @minnowhelper by clicking following links: 10SP, 100SP, 500SP, 1000SP or Another amount