Working with Sessions

Written by @m_k_amin 21 November 2012

Sessions are an important resource to save data on the server. This script shows their usage by a simple example. In this example a random number is made and saved to a session; based on its value, a message is shown. If it is bigger than 5000, the 'ID' session is removed and the 'lottery' session is set to 'no'; otherwise it will be kept and 'lottery' session is set to 'yes'. Finally all data is removed.

Code Snippet:

                                                
                                                <!-- this script is provided by www.phpfreecode.com coded by: Kerixa Inc. -->
<!-- This Script is from www.phpfreecpde.com, Coded by: Kerixa Inc-->

<?php
session_start();
$a= rand(1000, 9999);
echo "Your random ID is $a.<br>";
$_SESSION['ID']= $a;
if ($a>=5000) {
          session_unset('ID');
          echo 'Sorry Bad Chance.';
          $_SESSION['lottery']= 'no';}
else{
          $b= $_SESSION['ID'];
          echo "Good Chance and you will be added to the Lottery with $b ID.";
          $_SESSION['lottery']= 'yes';}
session_destroy();
?>

<br><font face="Tahoma"><a target="_blank" href="http://www.phpfreecode.com/"><span style="font-size: 8pt; text-decoration: none">PHP Free Code</span></a></font>
<a target='_blank' href='www.phpfreecode.com' style='font-size: 8pt; text-decoration: none'>Php Best Codes</a>                                                
                                            

Example:


About @m_k_amin

This user is pending a biography.

M

Comments


Here you can leave us commments. Let us know what you think about this code tutorial!

0 / 300

TRENDING POST
1
2
3
4
5
VISITORS
Online Users: 12
Recent Members: grkkid, Manaakividuinfo.com, karticksv, sava, tinatina
advertisement 2