What are the different methods to track and display the last 10 visited pages on a website using PHP?
To track and display the last 10 visited pages on a website using PHP, you can utilize sessions to store the visited pages in an array. You can then display the last 10 visited pages by retrieving them from the session array and displaying them in reverse order.
<?php
session_start();
// Track visited pages
if(!isset($_SESSION['visited_pages'])) {
$_SESSION['visited_pages'] = [];
}
// Add current page to visited pages
$current_page = $_SERVER['REQUEST_URI'];
array_push($_SESSION['visited_pages'], $current_page);
// Limit visited pages to 10
if(count($_SESSION['visited_pages']) > 10) {
array_shift($_SESSION['visited_pages']);
}
// Display last 10 visited pages
echo "<h2>Last 10 Visited Pages:</h2>";
echo "<ul>";
foreach(array_reverse($_SESSION['visited_pages']) as $page) {
echo "<li>$page</li>";
}
echo "</ul>";
?>
Keywords
Related Questions
- What is the appropriate format for passing the current date and time to be stored in a database column with DATETIME or DATE data type in PHP?
- What is the purpose of using the "exec" function in PHP and what potential issues can arise when using it with Linux commands like "wget" and "mv"?
- What are the potential pitfalls of using Composer to manage dependencies from non-Composer projects in PHP?