PHP Tutorial Tutorials - PHP Session

PHP Session

The PHP session stores data on the server rather than user's computer(cookie), it allows you to store data between requests in the $_SESSION superglobal array.

Starting a PHP Session

You can start up the session by setting session.auto_start in the configure file php.ini or calling session_start.

It will create a new session and generate a specific unique session id which sent with the request.(sesseion UID unique identification number)

On the server side ,every session's information is stored in the TMP folder with the name like sess_xxxxxxxxx. ( use phpinfo(); to get the Variable TMP value.)

<?php
// Starting session
session_start();
?>

the session_start() function must be called at the beginning of the page before any output prints.
Invalid:

(whitspace) // <--error
<?php
echo "hello"; // <--error
// Starting session
session_start();
?>

PHP $_SESSION

PHP $_SESSION is an superglobal associative array which use to store and retrieve user data in a session.

<?php
session_start(); 
$_SESSION['visits'] = 1; // store session data
echo "Pageviews = ". $_SESSION['visits']; //retrieve data
?>

Destroying a Session

If you only want to remove certain session data, simply use unset() to destroy the specified variables.
In PHP, the session_destroy() function is used to destroy all session variables completely and reset the session.

<?php
session_start();
if(isset($_SESSION["visits"])){
    unset($_SESSION["visits"]);
}
?>
<?php
// Starting session
session_start();
// Destroying session
session_destroy();
?>

The PHP session's default timeout duration is 1440 seconds. You can change it in the configuration file php.ini by setting the option session.gc_maxlifetime.

When user close the browser, browser would clear the session information (session id) in the client.
If user open the browser and visit the server again, a new session will be created.
You can set session.cookie_lifetime to non-zero value in the configuration file php.ini to prevent it.

PHP Session Example

Page views Counter:

page.php

<?php
session_start();
if(isset($_SESSION["visits"])){
    $_SESSION['visits'] +=1;
}
else{
    $_SESSION['visits'] = 1;
}
echo "Pageviews = ". $_SESSION['visits'];
?>

visit page.php more than one time,the Pageviews would be changed.

Share data between pages using session

page1.php

<?php
session_start();
echo "Page 1";
echo "<br>";

$_SESSION['user'] = 'lautturi';
echo '<a href="page2.php">Goto page 2</a>';
?>

page2.php

<?php
session_start();
echo "Page 2";
echo "<br>";

$user = $_SESSION["user"];
echo "user:". $user;
echo "<br>";
echo '<a href="page1.php">Goto page 1</a>';
?>

Is possible to keep session even after the browser is closed?
Use session_set_cookie_params() to give the session cookie a non-zero lifetime before starting the session, or set session.cookie_lifetime to non-zero.

Keep session alive

Use a keep alive.

On login:

session_start();
$_SESSION['last_action'] = time();
An ajax call every few (eg 20) seconds:

windows.setInterval(keepAliveCall, 20000);
Server side keepalive.php:

session_start();
$_SESSION['last_action'] = time();
On every other action:

session_start();
if ($_SESSION['last_action'] < time() - 30 /* be a little tolerant here */) {
// destroy the session and quit
}

Date:2019-10-01 03:09:32 From:www.Lautturi.com author:Lautturi