homepage Welcome to WebmasterWorld Guest from 54.166.100.8
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

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




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

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 -----------------
<?php
$path = str_replace('..','',$_SERVER['QUERY_STRING']);
$fullpath = 'http://AdServerDomain.com/nserver/SITE=TEST/AREA=Front/pageid=$pageid';
header('Content-type:image/gif');
$data = file_get_contents($fullpath);
echo $data;
?>
----------------- image.php -----------------



----------------- index.php -----------------
<html>
<head>
</head>
<body>
<?
function generateRandomString($length = 15, $letters = '1234567890qwertyuiopasdfghjklzxcvbnm')
{
$s = '';
$lettersLength = strlen($letters)-1;
for($i = 0 ; $i < $length ; $i++)
{
$s .= $letters[rand(0,$lettersLength)];
}
return $s;
}
$pageid=generateRandomString();

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);
fclose($sock);

if (preg_match('/^Location: (.+?)$/m', $response, $matches)){
if ( substr($matches[1], 0, 1) == "/" )
return $url_parts['scheme'] . "://" . $url_parts['host'] . trim($matches[1]);
else
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";
?>


</body>
</html>
----------------- index.php -----------------

 

g1smd




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

The index.php file should NOT begin with:

<html>
<head>
</head>
<body>


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.

The_Hat




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

Thanks.
Same outcome.
Any other ideas?

The_Hat




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

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?..

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved