Old session counter in PHP


I just wanted to share some old code I got lying around. It’s a session counter in PHP basically what it does is to increase a variable with one each time a page with the code is refreshed/visited. I even have a live example of it.

The code:

<?php
// Start sessions!
session_start();
// Is $_SESSION["count"] set? If yes, increase by one, if not set to one.
isset($_SESSION["count"]) ? $_SESSION["count"]++ :  $_SESSION["count"] = 1;
echo "This page has been loaded: {$_SESSION["count"]} time(s).";
?>
<br /> Reload the page. It's a simple counter using sessions to keep the count.

Each time you refresh the page the the count will increase with one. Remove the cookie to start from 1 again.

This entry was posted in PHP, Programming. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">