Latest Posts
  • How to Advertise Your Vape Business on Social Media: A Complete Guide
  • How To Create Custom Module Form with Menu Link in Drupal
  • Some Common Mistakes to Avoid While Doing SEO for Shopify
  • iPhone 8 & iPhone X Features Prices and Specifications
  • Make Money Easily to Automatically Link Keywords with Affiliate Links in WordPress
  • Yoast SEO Premium License Key Cracked or Patched
  • 6 Reasons To Choose WordPress For Blogging
  • Most Popular Coding Language In 2018
  • Facebook
  • Twitter
  • Instagram
  • RSS
  • YouTube
Developer Desks Developer Desks
  • Home
  • Codes
    • WordPress
    • PHP
    • Jquery
    • Javascript
    • Guest
  • Demos
  • blog
  • Tech News
  • Submit Your Articles
  • Contact
Php tutorials

Php code for login page using session without stored details in database

May 21, 2015
Share on Facebook Share on Twitter Google+ Pinterest LinkedIn Tumblr Email

For Previous chapter we already know what is session and how to set and destroy.

So here, We are going to learn how to using PHP code for login page using session without stored details in database.

Php code for login page using session

Example
1. First Create login page and named as Index.php
Code

<?php
session_start();    // Session start

if(isset($_POST['check']))    // Check form submit with IF Isset function
{
$username="admin";    // set variable value
$password="123";        // set variable value
if($_POST['username']==$username && $_POST['password']==$password)   // Check Given user name, password and Variable user name password are same
{
$_SESSION['username']=$username;    // set session from given user name
header('location:mainpage.php');
}
else
{
$err="Authentication Failed Try again!";
}
}
?>

<html>
<head>
<title>Main Page</title>
</head>
<body>
<Center>
<?php if(isset($err)){ echo $err; } ?>      <!-- Print Error -->

<form method="POST" name="loginauth" target="_self">

User Name: <input name="username" size="20" type="text" />
<br/><br/>
Pass Word: <input name="password" size="20" type="password" />
<br/><br/>
<input name="check" type="submit" value="Authenticate" />

</form>
</center>
</body>
</html>

 

Explanation
This index.php page will Set username and password as “admin” and “123”.
Check whether given user name and password is correct or wrong.
If the given username and password is correct. Set $_SESSION variables as username and redirect to mainpage.php

2. Second Create mainpage.php
Code

<?php
session_start();    //session start
if(!isset($_SESSION['username']))     //if session not found redirect to homepage
{
header('location:index.php');
}
echo "Welcome &nbsp;";
echo $_SESSION['username'];      //retrieved using session
?>

<html>
<head>
</head>
<body>
<center>
<h2>Welcome to Main page</h2>
<a href="logout.php">logout</a>
</center>
</body>
</html>

 

Explanation
Session variable retrieve the session value from index.php page to this page.

HINT : We can Use many number of pages with session variables.

<?php
session_start();    //session start
if(!isset($_SESSION['username']))     //if session not found redirect to homepage
{
header('location:index.php');
}
echo "Welcome &nbsp;";
echo $_SESSION['username'];      //retrieved using session
?>

 

simply place this in the top of the page.

3. Create logout.php page for destroy the session or unset the session

<?php
session_start();
if(!isset($_SESSION['username']))     //if session not found redirect to homepage
{
header('location:index.php');
}
unset($_SESSION['username']);       // Session Found Unset the variable values
session_destroy();                  // Destroy the session
header('location:index.php');
?>

 

Explanation
When you click Logout.php its check the session and unset the variable values and destroy the session

Developer desks- session – Full code Download here

To know Login With session in php using stored data in MySQL database here

Was this article helpful?

   

Awesome, share it:

Share Tweet Google Plus LinkedIn

Thanks!

Thanks for getting in touch with us.

Share this:

  • Twitter
  • Facebook
code php sessionlogin codephp arrayphp sessionsession codesession examplesession loginsession php codesession variablestutriaolswithlogin
Raja sekar

I'm Rajasekar - Web developer, Freelancer, Blogger and Owner of DeveloperDesks. From India lives in Bahrain. I love to do coding, Creating websites and trying different with code and designs. You Can Hire Me

  • Website

Related Posts

Most Popular Coding Language in 2017

Most Popular Coding Language In 2018

February 18, 2017
How to Create Like & Unlike code like FB uisng PHP, MySQL and jQuery

How to Create Like & Unlike code like FB using PHP, MySQL and jQuery

November 25, 2016
live-search-in-php-and-mysql-using-jquery

Live search in PHP and Mysql using Jquery

October 13, 2016
  • Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

    Thank you for subscribing.

    Something went wrong.

    we respect your privacy and take protecting it seriously

  • Help Me To Improve My Desk
    Buy Me A Coffee
  • About

    Developer Desks is Teach Coding and updates latest technology news in our blog. We have Highly professional web designer and Developer. We do Website developing work as a freelancer. Founded in 2015, This blog focus on the latest trends, tutorials, opinion articles as well as tips and tricks to empower our readers to become better web developers.

    Read More
    Facebook Twitter Instagram LinkedIn

    Copyrights © 2014 - 2022 Developer desks. All rights reserved. Designed by Developer desks.

  • Latest Posts
    • guest-blog-post
      How to Advertise Your Vape Business on Social Media: A Complete Guide
      Guest Blog May 30, 2018
    • How To Create Custom Module Form with Menu Link in Drupal
      Drupal April 4, 2018
    • Shopify Mistakes
      Some Common Mistakes to Avoid While Doing SEO for Shopify
      SEO September 18, 2017
    • iphone 8 features
      iPhone 8 & iPhone X Features Prices and Specifications
      Android updates September 13, 2017
  • Downloads
    Icon
    Multi select filter ajax 1794 downloads 3.43 KB
    Download
    Icon
    Check Existing username and match password using ajax 348 downloads 3.51 KB
    Download
    Icon
    Export excel from mysql table data 345 downloads 3.55 KB
    Download
    Icon
    php session without database 326 downloads 1.64 KB
    Download

Powered by Developer Desks. Privacy Policy

Top
Developer Desks