<?php
ob_start();
/**
* (C) Jaroslaw Miazga
* http://the-portal.pl
*/
class RedirectionDetector
{
private $url;
private $redirectTo;
/**
* Sets url of checking page
*
* @param $url string
*
*/
public function setUrl($url)
{
$this->url = $url;
}
/**
* Gets all headers
*
* @return $headers string
*
*/
public function getHeaders()
{
$ch = curl_init($this->url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_exec($ch);
curl_close($ch);
$headers = ob_get_clean();
return $headers;
}
/**
* Looking for redirection in meta tags
*
* @return bool true|false
*
*/
public function checkMetaTags()
{
$open = @file_get_contents($this->url);
if(eregi('<META HTTP-EQUIV="Refresh" CONTENT="(.*);URL=(.*)">', $open))
{
//if exists
return true;
}
else
{
//if not
return false;
}
}
/**
* Gets page where is redirection
*
* @return string
*
*/
public function getRedirectedPage()
{
return $this->redirectTo;
}
/**
* Looking for hidden redirection such as header('Location: xxxxx');
*
* @return bool true|false
*
*/
public function checkHidden()
{
preg_match_all('%Location: http://(.*)%', $this->getHeaders(), $match);
$found = $match[0][0];
$found = str_replace('Location: ', '', $found);
$this->redirectTo = $found;
$input = $this->url;
$host1 = parse_url($found);
$host2 = parse_url($input);
$host_a = str_replace('www.', '', $host1['host']);
$host_b = $host2['host'];
if($host_a == $host_b)
{
$this->redirectTo = null;
}
elseif(!empty($host_a))
{
$this->redirectTo = $found;
}
if($host_a == $host_b)
{
return false;
}
elseif(!empty($host_a))
{
return true;
}
}
/**
* Looking for all types of redirection
*
* @return bool true|false
*
*/
public function checkAll()
{
if($this->checkHidden() || $this->checkMetaTags())
{
return true;
}
else
{
return false;
}
}
}
?>