Pages

Monday 25 June 2012

PHP Sessions STARTING

Starting of PHP Sessions:-
Our first step is to initialise function  session_start(); This will check if any other session have been already been started or not.If it is not this function will start it .Session variables are stored in associative array called $_SESSION[]. These variables can be accessed during lifetime of a session.
Now Let us have a look over a simple program related to starting of seesion.


/*
 The code below starts a session then register a variable called counter that is incremented each time the page is visited during the session.We must make sure
the use of isset() function to check if session variable is already set or not.
*/

<?php
  session_start(); //initialising the function
if( isset( $_SESSION['counter'] ) )
{
$_SESSION['counter'] += 1;
}
else
{
$_SESSION['counter'] = 1;
}
$msg = "You have visited this page ". $_SESSION['counter'];
$msg .= "in this session.";
?>
<html>
<head>
<title>Setting up a PHP session</title>
</head>
<body>
<?php echo ( $msg ); ?>
</body>
</html>

We must take note tto save this file as .php extension not as a html file although we see some html tags in use here.


No comments:

Post a Comment