简体   繁体   中英

Is it possible to go back two referral URLs in PHP?

I currently use this snippet to create a go back to the previous page button:

<?php
    $referral_url = $_SERVER['HTTP_REFERER'];
    if (strpos($referral_url, 'product/') !== false) {
        echo '<a href="'.$_SERVER['HTTP_REFERER'].'" class="referral-url" title="Back"><i class="fas fa-chevron-left"></i> Back to previous product</a>';
    } else if (strpos($referral_url, 'products/') !== false) {
        echo '<a href="'.$_SERVER['HTTP_REFERER'].'" class="referral-url" title="Back"><i class="fas fa-chevron-left"></i> Back to products</a>';
    } elseif (!empty($referral_url)) {
        echo '<a href="'.$_SERVER['HTTP_REFERER'].'" class="referral-url" title="Back"><i class="fas fa-chevron-left"></i> Back</a>';
    } else {
        // Do Nothing
    }
?>

I am however experiencing a couple issues with this back button, if a user adds the product to cart, the referrer URL becomes itself, trapping the back link in a loop.

Is there any existing methodology to allow me to check if the referrer URL matches the current, if so use the link before the referrer URL?

So it would be something like this:

<?php
    $referral_url = $_SERVER['HTTP_REFERER'];
    var_dump($referral_url);

    $current_url = 'https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    var_dump($current_url);

    if ($referral_url == $current_url) {
        echo '<a href="'.$_SERVER['HTTP_REFERER[1]'].'" class="referral-url" title="Back"><i class="fas fa-chevron-left"></i> Back to products</a>';
    } else if (strpos($referral_url, 'product/') !== false) {
        echo '<a href="'.$_SERVER['HTTP_REFERER'].'" class="referral-url" title="Back"><i class="fas fa-chevron-left"></i> Back to previous product</a>';
    } else if (strpos($referral_url, 'products/') !== false) {
        echo '<a href="'.$_SERVER['HTTP_REFERER'].'" class="referral-url" title="Back"><i class="fas fa-chevron-left"></i> Back to products</a>';
    } elseif (!empty($referral_url)) {
        echo '<a href="'.$_SERVER['HTTP_REFERER'].'" class="referral-url" title="Back"><i class="fas fa-chevron-left"></i> Back</a>';
    } else {
        // Do Nothing
    }
?>

Stating the obvious but this is the additional code:

$current_url = 'https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
var_dump($current_url);

if ($referral_url == $current_url) {
    echo '<a href="'.$_SERVER['HTTP_REFERER[1]'].'" class="referral-url" title="Back"><i class="fas fa-chevron-left"></i> Back to products</a>';
}

Where:

$_SERVER['HTTP_REFERER[1]']

Is not a real thing...

But what is? Is there a way to achieve this?

Here is how I achieved it, with a mix of JS and PHP:

<?php
    $referral_url = $_SERVER['HTTP_REFERER'];
    var_dump($referral_url);

    $current_url = 'https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    var_dump($current_url);

    if ($referral_url == $current_url) {
        echo '<a href="javascript:void(0)" onclick="javascript:history.go(-2)" class="referral-url" title="Back"><i class="fas fa-chevron-left"></i> Back to products</a>';
    } else if (strpos($referral_url, 'product/') !== false) {
        echo '<a href="'.$_SERVER['HTTP_REFERER'].'" class="referral-url" title="Back"><i class="fas fa-chevron-left"></i> Back to previous product</a>';
    } else if (strpos($referral_url, 'products/') !== false) {
        echo '<a href="'.$_SERVER['HTTP_REFERER'].'" class="referral-url" title="Back"><i class="fas fa-chevron-left"></i> Back to products</a>';
    } elseif (!empty($referral_url)) {
        echo '<a href="'.$_SERVER['HTTP_REFERER'].'" class="referral-url" title="Back"><i class="fas fa-chevron-left"></i> Back</a>';
    } else {
        // Do Nothing
    }
?>

I would still very much be interested in the answer should anyone know how to achieve this with PHP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM