New Trending Topics and Study Material

Breaking

Tag

Tricks & Tips (17) News (5) PHP (5) Android (3) SEO (1) free paytm cash (1)

Monday, 26 February 2018

How to create Session in PHP and use of session | vrsavani 2018


First, create one PHP page paste this code in this file



<div class="jumbotron bg-white text-center text-white">
 <div class="container bg-danger" >
  <form action="SessionDemo.php" method="post">
   <br>
   <hr class="bg-light">
   <h2 > Enter Session Value </h2>
   <hr class="bg-light">
   <div class="form-group">
    <input type="text" name="name" class="form-control" placeholder="Enter Session Value..." >
   </div>
   <input type="submit" value="Save Session" name="Save"  class="btn btn-success btn-block"/>
   <input type="submit" value="Show Session" name="show"  class="btn btn-warning btn-block"/>
   <input type="submit" value="Destroyed Session" name="del"  class="btn btn-light btn-block"/>
   <br><br>
  </form>
 </div>
</div> 
        
  
  






Design 1

Session in php | vrsavani blog | use of session



1) Start a PHP Session

A session is started with the session_start() function.
Session variables are set with the PHP global variable: $_SESSION.

$_SESSION["Session Name"] = "Value"; 

in this tutorial ,I set session like this


if (isset($_POST['Save'])) {
 if (!empty($_POST['name'])) {
  $a = $_POST['name'];
  // Set session variables
  $_SESSION["demo"] = $a;
  echo "<h3 class='alert alert-success alert-dismissable fade show text-center'>Session variables are set.</h3><br><br>";
 } else {
 //if session is not set ...
  echo "<h3 class='alert alert-danger alert-dismissable fade show text-center'>Session variables are Not set.</h3><br><br>";
 }
} 



If User Click One Save Button So that time Session is created and set the values of the session.....


Design 2


error in session | vrsavani | session variables are not set
2.1





session variables are set | vrsavani | session img
2.2



2) Get PHP Session Variable Values

Also notice that all session variable values are stored in the global $_SESSION variable:

if (isset($_POST['show'])) {
 // Show session variables
 echo "<h3 class='alert alert-success alert-dismissable fade show text-center'>Your 
        Session variables =  " . $_SESSION["demo"] . "</h3><br><br>";
} 



Design 3

show session variables | vrsavani | savani blog

3) Destroy a PHP Session: 

To remove all global session variables and destroy the session, use session_unset() and session_destroy():

if (isset($_POST['del'])) {
 // destroy  the session 
 session_destroy();
 echo "<h3 class='alert alert-success alert-dismissable fade show text-center'>All 
        session variables are now removed, and the session is destroyed.</h3><br><br>";
} 



Design 4


removed session | destroyed session | vrsavani blog | PHP




Download Source File With Designing...

vrsavani blog | download tutorial






No comments:

Post a Comment