Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Get redirect url and file get contents should match but they don't



11:27 pm on Feb 9, 2011 (gmt 0)

10+ Year Member

Sorry about that subject line.. but it does relate what the question is about.

I have an ad serving platform that serves up ads on page load from an assortment of different ads that are eligible to appear depending upon the values I pass to it in the url.

I have circumstances that make it necessary for me to query the ad Server through php for a matched set of both the ad image and the intended click url of the ad.. and I'm pretty close to it, but right now what I get is an ad image and the click url of the PREVIOUS advertisement.. it doesn't seem to matter when I refresh the page.. the URL that I get is steadily one page refresh behind what the URL should be for the image that is showing.. Below is the code I'm running.. two files, image.php is referenced in index.php.

Anyone out there see anything in my logic that would cause such a delay?

------------------ image.php -----------------
$path = str_replace('..','',$_SERVER['QUERY_STRING']);
$fullpath = 'http://AdServerDomain.com/nserver/SITE=TEST/AREA=Front/pageid=$pageid';
$data = file_get_contents($fullpath);
echo $data;
----------------- image.php -----------------

----------------- index.php -----------------
function generateRandomString($length = 15, $letters = '1234567890qwertyuiopasdfghjklzxcvbnm')
$s = '';
$lettersLength = strlen($letters)-1;
for($i = 0 ; $i < $length ; $i++)
$s .= $letters[rand(0,$lettersLength)];
return $s;

function get_redirect_url($url){
$redirect_url = null;

$url_parts = @parse_url($url);
if (!$url_parts) return false;
if (!isset($url_parts['host'])) return false; //can't process relative URLs
if (!isset($url_parts['path'])) $url_parts['path'] = '/';

$sock = fsockopen($url_parts['host'], (isset($url_parts['port']) ? (int)$url_parts['port'] : 80), $errno, $errstr, 30);
if (!$sock) return false;

$request = "HEAD " . $url_parts['path'] . (isset($url_parts['query']) ? '?'.$url_parts['query'] : '') . " HTTP/1.1\r\n";
$request .= 'Host: ' . $url_parts['host'] . "\r\n";
$request .= "Connection: Close\r\n\r\n";
fwrite($sock, $request);
$response = '';
while(!feof($sock)) $response .= fread($sock, 8192);

if (preg_match('/^Location: (.+?)$/m', $response, $matches)){
if ( substr($matches[1], 0, 1) == "/" )
return $url_parts['scheme'] . "://" . $url_parts['host'] . trim($matches[1]);
return trim($matches[1]);

} else {
return false;

<img src="image.php?pageid=<? echo $pageid ;?>" />

$clickUrl = get_redirect_url('http://AdServerDomain.com/ClickURL/SITE=TEST/AREA=Front/pageid=$pageid');
echo "$clickUrl";

----------------- index.php -----------------


11:39 pm on Feb 9, 2011 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

The index.php file should NOT begin with:


Those should be moved a long way down the script, appearing immediately before the first place you output any HTML content.

This then allows the "logic" part of your script to send extra HTTP headers and other information before the beginning of the HTML page.


3:13 pm on Feb 10, 2011 (gmt 0)

10+ Year Member

Same outcome.
Any other ideas?


4:34 pm on Feb 14, 2011 (gmt 0)

10+ Year Member

Here's what I did this morning.. I took the piece that gets the url and split it into a separate php page. Leaving just the image stuff in index.php.. hitting index2.php then gets the corresponding url.. and get this IT WORKS.. if I refresh index.php and then refresh index2, i get the image in the first one and the right url in the second.. reliably.. if I don't refresh the first I can refresh and refresh number 2 and won't get anything different.. until i refresh the first one..

So it seems as though the request for the url needs to be separate somehow from the request for the image.

I have no idea why, but someone here does.. Can anyone help me?

Does having everything within a single file make just one request to the ad server? and the adserver waits for the second request for the url.. which makes the all-in-one solution always pulling the previous url?..

Featured Threads

Hot Threads This Week

Hot Threads This Month