Saturday, 24 February 2018

Session in PHP

A session is used to store information across multiple pages and the information is stored on the server.
Sessions are stored in server  and it can store unlimited amount of dataSo it is more secure.
By default session variables last until the user closes the browser. So Session  variables hold   information about one single user, and are available to all pages in one application.

Create session:
session_start() function is used to start the session.
It starts a new or resumes existing session. It returns existing session if session is created already. If session is not available, it creates and returns new session.

Syntax
    bool session_start ( void )  
Example
    session_start();  
$_SESSION:
$_SESSION is an associative array that contains all session variables. It is used to set and get session variable values.

Example: Set information
    $_SESSION["admin"] = "dipak";  
Example: Get information
     echo $_SESSION["dipak"];  
Note: 
The session_start() function must be declare top of your php page, Before any HTML tags

Session Example
   demo.php
<?php  
session_start();  
?>  
<html>  
<body>  
<?php  
$_SESSION["admin"] = "dipak";  
echo "Session information is set<br/>";  
?>  
<a href="result.php">Click to get session value</a>  
</body>  
</html>  
result.php
<?php  
session_start();  
?>  
<html>  
<body>  
<?php  
echo "Admin is: ".$_SESSION["admin"];  
?>  
</body>  
</html>  
 Destroy Session
 session_destroy()function is used to destroy the session.
 session_unset()  function is used to remove all session variables.
Example:
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
session_unset();   // remove all session variables
session_destroy();  // destroy the session
?>
<body>
<html>

No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...