Search code examples
phpforwardingreferrer

How do I use PHP to send users to specific pages based on where they are coming from


What I want to do, is use PHP to forward users to specific pages based on where they are coming from on my site.(basically this is a next button functionality)

So what I'd like to do is have PHP check the referring url and then forward based on that value.

Something like this:(note I can't post multiple urls, so imagine that there is the http:// in front of all of them)

If url: mysite.com/gallery1/ then forward to: mysite.com/gallery2/

If url: mysite.com/gallery2/ then forward to: mysite.com/gallery3/

If url: mysite.com/gallery3/ then forward to: mysite.com/gallery4/

And if the referring url isn't on the list or they just typed in the php script url directly, send them to mysite.com/nogallery/

I'd like to use a database to store the data. Basically it'd have 2 columns. 1 for referrer url, and 1 for the forwarding url.

If you could help me out it'd be greatly appreciated.


Solution

  • Sounds like you know what to do already. If you want to store the URLs in a database, that's fine, but just imagine you've got a structure like this:

    <?PHP
    $routes = array(
    'example.com/1.php'=>'example.com/2.php',
    'example.com/2.php'=>'example.com/3.php',
    'example.com/3.php'=>'example.com/4.php');
    
    if (array_key_exists($_SERVER['HTTP_REFERER'],$routes)){
        header('Location: http://'. $routes[$_SERVER['HTTP_REFERER']]);
    }else{
        header('Location: http://example.com/default.php');
    }
    exit;
    
    ?>