PHP Session

You can use SESSIONS to store persistent user preferences on your Website. Each session has a data store related to it and its content persists between pages.

To start using a session you need to call session_start(), but you have to remember:

  • you can call session_start() just once per file
  • you need to call it before the script generates any output (before you send any data to the browser)

Session_start() will load session data in the $_SESSION associative array.

The easiest way to store data in a session is:

$_SESSION['name'] = “value”;

Here a simple example about how to use $_SESSION:

<?php 
//starting a session
session_start();

//checking if session data about visits exists
if( isset($_SESSION['visits']) ){
//incrementing visits counter
 $_SESSION['visits'] = $_SESSION['visits'] +1;
} else {
//creating a visits variable in session
$_SESSION['visits'] = 1;
}


//accessing data from session
echo "<p>You have been here <strong>" .$_SESSION['visits']. "</strong> times.</p>"
 ?>

To end a session, you can use session_destroy()

PHP Session

You can use SESSIONS to store persistent user preferences on your Website. Each session has a data store related to it and its content persists between pages.

To start using a session you need to call session_start(), but you have to remember:

  • you can call session_start() just once per file
  • you need to call it before the script generates any output (before you send any data to the browser)

Session_start() will load session data in the $_SESSION associative array.

The easiest way to store data in a session is:

$_SESSION[‘name’] = “value”;

Here a simple example about how to use $_SESSION:

To end a session, you can use session_destroy()