Is there a way to monitor the last five pages accessed using cookies in a PHP environment?

  <?php

   session_start();
   if(empty($_SESSION['track']))
       $_SESSION['history'] = array($_SERVER['PHP_SELF']);
   else {
       $_SESSION['track'][] = $_SERVER['PHP_SELF'];
   if(count($_SESSION['track']) > 5)
       array_shift($_SESSION['track']);
   }

   function display()
   {
       print_r(array_values($_SESSION['track']));
   }
  ?>

I managed to accomplish this using sessions, but I am looking for a solution that only involves cookies and PHP to keep track of the last 5 visited pages. Any suggestions or insights are greatly appreciated. Thank you in advance.

Answer №1

$history = unserialize($_COOKIE['visited_history']);
array_push($history, $current_url);
setcookie('visited_history', serialize($history));

When a page loads, the code above retrieves the value of the 'visited_history' cookie, adds the current URL to it, and then updates the cookie with the new information.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Modifying a POST parameter that aligns with a given text pattern

Currently working on a basic PHP form. My goal is to modify the variable if it matches the placeholder text in the input field, so that it's not included in the email content. <p>Base Colour Preference <input type="text" name="baseco ...

"Make updates to the data with the help of a button input

I am working on a form that has two input type buttons. The first button is labeled "edit", and when clicked, it enables the input field. The second button is labeled "save", and when clicked, it disables the input field and should save the new values in a ...

"Exploring the world of Codeigniter 2 alongside the capabilities of

I have successfully installed CodeIgniter framework version 2 and created a PHPunit test in /tests/unit/PostTest.php with the following code: <?php class PostTest extends PHPUnit_Framework_TestCase { private $CI; public function s ...

Avoid multiple clients from re-evaluating Redis values simultaneously

Imagine a scenario where our website has important counters on every page, making it heavily loaded. These counters are stored in Redis keys that automatically expire in an hour. Within our code, we have a check to see if a key containing a counter exists ...

Time when the client request was initiated

When an event occurs in the client browser, it triggers a log request to the server. My goal is to obtain the most accurate timestamp for the event. However, we've encountered issues with relying on Javascript as some browsers provide inaccurate times ...

Class not found error encountered while trying to extend another class

In a directory called "classes," there are two files. The first file is the main class named "Database" which is used to connect to the database. The second file is named "Branch" and it extends the Database class, designed to retrieve data from the databa ...

Can a PHP script be executed through an Ajax event?

Is it feasible to execute the script of a PHP file when an AJAX event is triggered? Consider this scenario: on the AJAX error, could we send the data to the error.php file, record the error, notify the admin via email, and perform any other desired action ...

PHP-MySQL FlipClock: Counting Down in Reverse

I am looking for a solution to implement a reverse counter using Flipclock. The timer should start from a given time (for example, 19:40:46) and count down to 00:00:00. Here is the code: var clock; $(document).ready(function() { // Get the current ...

Challenges in generating a PHP array from a CSV file with non-English characters

When working with PHP, I have encountered an issue while trying to create an array from a CSV file using the following code: // CSV file: $csv = array_map('str_getcsv', file("{$uploadPath}/{$myFile}")); Although this code functions correctly for ...

Installing mikehaertl-phpwkhtmltopdf on xampp made easy

I've attempted to utilize the following post for guidance: How do I get WKHTMLTOPDF to run through PHP? but it seems like I'm overlooking something. What I really need is a clear and simple step-by-step guide... Setting up mikehaertl-phpwkhtmlt ...

What is the method for retrieving the row that matches a specific statement?

I need assistance grabbing a feed from an API and specifically extracting the row that begins with ",,[[,2". Can someone guide me on how to accomplish this task? ...

Can we retrieve the SOAP XML data prior to calling __soapCall?

Is there a way to access the XML generated by SOAP Client before it is sent to the webservice? I am looking for a solution because when the webservice responds with an error due to incorrect parameters, I receive messages like: Server was unable to read ...

How can JavaScript be utilized to disable the use of the spacebar?

I have implemented a live search feature on the website I'm working on. Currently, the search function queries the MySql database as soon as the first character is entered and updates the results with each subsequent character input. However, I'v ...

Error 500 encountered while making an Ajax request to the server

I am trying to send a post request using ajax to a controller in Laravel. The ajax request includes two input arguments, and the goal is for the controller to find the column in the database with the first argument and set the name attribute with the secon ...

Displaying group field data in one page on JQGrid when pagination is enabled

Is there a way in JQGrid to display group data on one page? Specifically, if displaying all items from an order would exceed the list limit, I want to only show data up to the previous order without exceeding the limit. How can this be achieved? ...

Obtaining a Timestamp with PHP

Attempting to retrieve the timestamp in php using the code below $date = new DateTime(); echo $date->getTimestamp(); An error occurs stating that getTimestamp() is undefined, despite references from this source http://php.net/manual/en/da ...

Data sent as FormData will be received as arrays separated by commas

When constructing form data, I compile arrays and use POST to send it. Here's the code snippet: let fd = new FormData(); for (section in this.data.choices) { let key = section+(this.data.choices[section] instanceof Array ? '[]' : '& ...

Navigating through Laravel to access a folder within the public directory

After installing Laravel, I decided to place Wordpress inside the public folder of Laravel under a directory named wordpress. Now, my goal is to set up a route at domain.com that directs users to the public/wordpress folder... How can I achieve this? I ...

Executing SQL Commands

Trying to figure out how to dynamically insert a row into an HTML table and then update the corresponding database entry. There's a SQL prepare and execute statement in a separate PHP file, but I need to find a way to include variables inside the exec ...

Experiencing Setbacks while Implementing AJAX in a PHP Object Orient

I'm currently attempting to create an object-oriented programming (OOP) login system using Ajax. However, I am encountering issues with starting the session or being redirected to the "Directivo.php" page. When I run the code, there is no output displ ...