Skip to content
PHP Logo

How can one efficiently handle situations where the user is on the same page as the link destination in PHP?

When a user clicks on a link that leads to a page they are already on, it can result in a redundant page reload. To handle this efficiently in PHP, you can check the current page URL against the link destination URL and only redirect if they are different.

php
$current_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$link_destination = "http://www.example.com/samepage.php";

if ($current_url != $link_destination) {
    header("Location: $link_destination");
    exit();
}