Posted By:
7 Mar, 2014 11:52 pm PST

How to store data of current user session?

 How to store data of current user session?

Session means the time from starting application or opening web page till you end application or close web page. This duration is called session for that user. Session variable stores information about user session and various setting of user and activities for during session.

 

PHP support session to preserve certain data of users across subsequent accesses of same user.

When user visit the web page for first time, new unique session ID is created and stored in a cookie on the user side or is propagated in the URL.

 

This data is accessible through $_SESSION superglobal array. The session_start() function is used to start session and it must appear BEFORE the <html> tag.

<?php

if(isset($_COOKIE['test']))

header('location:secure.php');

 

?>

<html>

<body align="center">

<form name="login" action="validate.php" method="POST" >

<table border="1" align="center">

 

<tbody>

<tr>

<td>Username</td>

<td><input type="text" name="username" value="" size="20" placeholder="ex.abc" /></td>

</tr>

<tr>

<td>password</td>

<td><input type="password" name="password" value="" size="20" placeholder="ex.abc" /></td>

</tr>

<tr>

<td><input type="submit" value="Submit" name="submit" /></td>

<td><input type="reset" value="Reset" name="reset" /></td>

</tr>

</tbody>

</table>

 

</form>

</body>

</html>

 

//Loginpage.php

  

<html>

<head>

 

</head>

<body>

<?php

session_start () ;

$user_db = array ( " sachin " = > array ( " firstname " = > " sachin " , " lastname " = > " Tendulkar " , “password " = > " abc " ," profession" = > " cricketer " ) ," rahul " = > array ( " firstname " = > " rahul " , "lastname " = > " dravid " , " password " = > " xyz " , " profession " = > " businessman " ), " anil " = > array ( " firstname " = > " anil " , " lastname " = > " kumble " , " password " = > " pqr " ,"profession " = > " engineer " ));

 

if ( array_key_exists ( $_POST [ ’ username ’] , $user_db ) && $_POST [ ’password ’ ]== $user_db $_POST [ ’ username ’ ]][ ’ password ’ ])

{

echo " welcome " ;

 

$_SESSION [ ’ user ’ ]= $user_db [ $_POST [ ’ username ’ ]];

 

setcookie ( ’ test ’ , 45 , time () +50000) ;

header ( " location : secure . php " ) ;

}

else

{

header ( " location : LoginPage . php " ) ;

?>

</body>

</html>

Validate.php

 

  

       
   

<? php

session_start () ;

echo " < br / > < h1 > welcome " . ucfirst ( $_SESSION [ ’ user ’ ][ ’ firstname’ ]) . " </ h1 > " ;

echo " < br / > < h2 > Firstname " . ucfirst ( $_SESSION [ ’ user ’ ][ ’ firstname ’ ]) . " </ h2 > " ;

echo " < br / > < h2 > lastnaem " . ucfirst ( $_SESSION [ ’ user ’ ][ ’lastname ’ ]) . " <h2 > " ;

echo " < br / > < h2 > Profession " . ucfirst ( $_SESSION [ ’ user ’ ][ ’profession ’ ]) . " <h2 > " ;

?>

< html >

< body >

<a href = " LogoutPage . php " > Logout </ a >

</ body >

</ html >

Secure.php

 
 
 

<?php

 

setcookie ('test ', 45, time () -50000) ;

 

header ('Location : LoginPage .php ');

 

?> LogoutPage.php

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


LoginPage

 

SessionResult

Attachment: validate.php


What is session and how to create session in PHP?

 

Session in PHP are one which store session variables and their values in temporary directory on server.A cookie is called PHPSESSID is automatically sent to the computer of user to store unique session.

 

//To start PHP session

Session  can start using session_start() function.Call to the session_start is done at the beginning. and these session variables are stored in array known as $_SESSION[].

 

<html>

<body>

<?php

session_start();

$_SESSION['th']='the';

$_SESSION['ph']='php';

$_SESSION['pr']='program';

                                                                                                                      

echo 'Setting session values';

foreach($_SESSION as $key=>$value)

{

echo 'The value of $_SESSION['."'".$key."'".'] is'."'".$value."'".' <br />';

}

?>

</body>

</html> 

 

Attachment: session.php
Was it useful?  




Please login to reply to this problem.