PHP Referral Redirect - Not Working

Hi all,

I have a PHP code that is to be used on specific pages within my site. When a user lands on the page from a specific domain OR a specific URL that has a tracking ID (ex: www.mysite.com OR www.mysite.com/?tid=1) the code is suposed to take them to another specific site or page. Bottom line, the code as is is not working now.

Also FYI, I am not a coder so take it easy on me. Just would like someone to tell me what to correct to make it work.

Also, I used Sharepoint to create the index.php file. I simply pasted the code into a new Sharepoint file, saved it with the .php extension and FTP’d it to the directory. When I tested it live with real URLs inserted into the code (not the ones in the attached sample, but real ones) the page is blank. It does not feedback an error, it just does nothing. Hope this additional info helps clarify the situation.

Thank you in advance.

Code:

[php]<?php

ini_set(‘display_errors’, 1);

class Redirector
{

/**
 * Server data array
 * 
 * @var array
 */
protected $_server;

/**
 * Redirection maps
 * 
 * @var array
 */
protected $_maps;

/**
 * Class constructor
 * 
 * @param  array $server Server data array
 * @param  array $maps   Redirection maps
 * @return Redirector
 */
public function __construct(array $server, array $maps)
{
    $this->_server = $server;
    $this->_maps = $maps;
}

/**
 * Get target for the specified host
 * 
 * @param  string $host Referrer host
 * @return string|null
 */
public function getTarget($host)
{
    if (array_key_exists($host, $this->_maps)) {
        return $this->_maps[$host];
    }

    return null;
}

/**
 * Return TRUE if referrer is available
 * 
 * @return bool
 */
public function hasReferer()
{
    return array_key_exists('HTTP_REFERER', $this->_server);
}

/**
 * Get current referrer if available
 * 
 * @return string
 */
public function getReferer()
{
    if ($this->hasReferer()) {
        return $this->_server['HTTP_REFERER'];
    }

    return null;
}

/**
 * Perform the redirect
 * 
 * @return bool
 */
public function run()
{
    if (!$this->hasReferer()) {
        return false;
    }

    $referer = $this->getReferer();
    $host = parse_url($referer, PHP_URL_HOST);
    $target = $this->getTarget($host);
    header('Location: ' . $target);
}

}

// initialize the redirector and perform the actual redirect
$maps = array(
www.xyz.com’ => ‘http://www.123.com’,
www.lamesite.com’ => ‘http://www.123.com’,
www.coolsite.com’ => ‘http://www.123.com’,
www.bieber.com’ => ‘http://www.boohiss.com’,
);
$redirector = new Redirector($_SERVER, $maps);
$redirector->run();

[/php]

I tested this code and it works fine. What problem are you having?

There’s nothing in this code that would redirect a specific URL. It will only redirect based on the host.

[php]
$host = parse_url($referer, PHP_URL_HOST);
[/php]

Please forgive my lack of expertise.

What I want to happen is that when a specific rererrer URL (based on the original post description) hits, that it is directed to another specific url.

I sent you a PM m@tt, so I’ll await your response whenever you have time.

Thank you for your replies in any case.

This is a help forum for people trying to learn but this is a very simple mod.

Replace the run() function with this one below. It will allow you to match query strings as well.

[php]
public function run()
{
if (!$this->hasReferer()) {
return false;
}

$referer = $this->getReferer();
$parsed = parse_url($referer);

$target = $parsed['host'];
if (isset($parsed['query'])) {
	$target .= '?' . $parsed['query']; /* append query string to target */
}
$target = $this->getTarget($target);
header('Location: ' . $target);

}
[/php]

Example:

[php]
$maps = array(
www.xyz.com’ => ‘http://www.123.com’,
www.xyz.com?tid=1’ => ‘http://www.456.com’,
www.lamesite.com’ => ‘http://www.123.com’,
www.coolsite.com’ => ‘http://www.123.com’,
www.bieber.com’ => ‘http://www.boohiss.com’,
);
[/php]

Thanks m@tt. I will tackle this in the am and let you know how I do.

Mucho gracias. :slight_smile:

m@tt, " ‘www.bieber.com’ => ‘http://www.boohiss.com’, " FUNNY!

mine from the original post of the code, but glad you got it.

Sponsor our Newsletter | Privacy Policy | Terms of Service