Free download web templates, books and more. Free php projects ,games and apps. GTA mods and more

Breaking

PHP Session || How To Use php Session

PHP Session

the php session is very useful and Important in the php .
the session store in the online storage.the defined session name is called different web pages .
and destroy the sessions
PHP session is used to store and pass information from one page to another temporarily (until user close the website).
PHP session technique is widely used in shopping websites where we need to store and pass cart information e.g. username, product code, product name, product price etc from one page to another.
PHP session creates unique user id for each browser to recognize the user and avoid conflict between multiple browsers.

PHP session_start() function:

PHP 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
  1. bool session_start ( void )  
Example
  1. session_start();  

PHP $_SESSION

PHP $_SESSION is an associative array that contains all session variables. It is used to set and get session variable values.
Example: Store information
  1. $_SESSION["user"] = "Samadroid";  
Example: Get information
  1. echo $_SESSION["user"];  

PHP Session Example

File: session.php
  1. <?php  
  2. session_start();  
  3. ?>  
  4. <html>  
  5. <body>  
  6. <?php  
  7. $_SESSION["user"] = "Sachin";  
  8. echo "Session information are set successfully.<br/>";  
  9. ?>  
  10. <a href="session2.php">Visit next page</a>  
  11. </body>  
  12. </html>  
File: sessioncall.php
  1. <?php  
  2. session_start();  
  3. ?>  
  4. <html>  
  5. <body>  
  6. <?php  
  7. echo "User is: ".$_SESSION["user"];  
  8. ?>  
  9. </body>  
  10. </html>  

PHP Session Counter Example

File: sessioncounter.php
  1. <?php  
  2.    session_start();  
  3.   
  4.    if (!isset($_SESSION['counter'])) {  
  5.       $_SESSION['counter'] = 1;  
  6.    } else {  
  7.       $_SESSION['counter']++;  
  8.    }  
  9.    echo ("Page Views: ".$_SESSION['counter']);  
  10. ?>  

PHP Destroying Session

PHP session_destroy() function is used to destroy all session variables completely.
File: sessionunset.php
  1. <?php  
  2. session_start();  
  3. session_destroy();  
  4. ?>