Php get previous url

The previous URL in PHP can be obtained using the $_SERVER['HTTP_REFERER'] variable. This variable contains the URL of the previous page that the user visited.

Here is an example of how to use it:

    <?php
      // Get the previous URL
      $previousUrl = $_SERVER['HTTP_REFERER'];
      
      // Display the previous URL
      echo "Previous URL: " . $previousUrl;
    ?>
  

When you run this code in a PHP file and visit that file from another page, it will display the URL of the previous page.

However, please note that the $_SERVER['HTTP_REFERER'] variable is not always reliable. It can be easily manipulated or blocked by the user’s browser, so you should not rely on it for critical functionality.

Leave a comment