Android Tutorial , Programming Tutorial, Php Tutorial, Learn Android, HTML Tutorial, Coding , Java Tutorial, GTU Programs, Learning Programming

Wednesday, 19 October 2016

PHP Login Example with session

In this example, we are learning about how to create login form with session using Php.


  • Session is one type of way which is used to store information to be used in multiple pages. Session is used to store some details about purchasing product in any website , store username, pass data in multiple forms , etc.

  • for staring session in file , session_start() function is used. to set value in session variable, $_SESSION php variable is used.

  • for destroy session , session_destroy() function is used.

Let's see the example :

File Name : file1.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
<?php
session_start(); //start session
if(isset($_POST['click']))
{
 $_SESSION['user']=$_POST['username']; //set value of username in session varible
 header("Location:file2.php");
 exit;
}
?>

<html>
 <body>
  <form method="post">
   <input type="text" placeholder="Enter Username" name="username"/><br/>
   <input type="password" placeholder="Enter Password" name="password" /><br/>
   <input type="submit" name="click" value="Click Here">
   <input type="reset" name="cancel" value="Cancel">
  </form>
 </body>
</html>

File Name : file2.php


1
2
3
4
<?php
session_start();
echo "WELCOME ".$_SESSION['user'];
?>


Output :-


file1.php


file2.php


0 comments:

Post a Comment

Like us on Facebook

Site Visitor

Powered by Blogger.