PHP

Count page views

The total number of times you've accessed this page in your browser is:

1

We did not place a cookie on your device. We use a temporary session token.

What is PHP session?

A PHP session is a way to store information on the server to be used across multiple pages of a web application. Unlike cookies, which store data on the user’s computer, session data is stored on the server. This function must be called at the beginning of your script, before any other HTML tags.

How does a PHP session work?

PHP automatically generates a unique session ID for each user when a session is started. This ID is typically sent to the client’s browser in the form of a a temporary session token called PHPSESSID.

When does the saved session expire?

By default, PHP sessions expire when the user closes their browser, or if the session is inactive for a certain period. You can control the session’s expiration time by configuring session settings in php.ini or using session_set_cookie_params().

Sessions can also be manually destroyed or expire after a set time using custom logic on the server-side.

Example

Here is a simple example of how to count the number of times a user has opened the page in their browser:

HTML and PHP Code
<?php
session_start();
 if(isset( $_SESSION['demo_count'] )) {
  $_SESSION['demo_count'] += 1;
} 
else {
  $_SESSION['demo_count'] = 1;
}

$demo_message = "<strong>" . $_SESSION['demo_count'] . "</strong>.";
?>

<html> 
 <head>
  <title>Session Demo</title>
 </head>
<body>
 <?php echo $demo_message; ?>
</body>
</html>