Jump to content

Functions being called multiple times when header is set


Recommended Posts

I have this script

<?php
header("content-type: image/png");
include '../../api/config.php';
include "../../api/api.php";
$gamercard = getInfo($_GET['gamertag']);
updateStats($gamercard['Gamertag'], "Nxe", 75);
$type = $_GET['type'];
switch ($type)
{
case "dark":
	$type = "dark.png";
break;
case "light":
	$type = "light.png";
break;
default:
	$type = "dark.png";
break;
}
$image = imagecreatefrompng("$type");  
$img = imagecreatetruecolor(imagesx($image), imagesy($image));
$avatar_l = imagecreatefrompng($gamercard["Avatarpic-l"]); 
$avatar_body = imagecreatefrompng($gamercard["Avatarpic-body"]); 
imagecopy($img, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
imagecopy($img, $avatar_l, 64, 7, 0, 0, imagesx($avatar_l), imagesy($avatar_l));
imagecopy($img, $avatar_body, 175, -40, 0, 0, imagesx($avatar_body), imagesy($avatar_body));
for($i = 1; $i <=count($gamercard["RecentGames"]); $i++)
{
$gameimg = imagecreatefromjpeg($gamercard["RecentGames"][$i]["Icon"]);
$x = ($i-1)*35 + 10;
imagecopy($img, $gameimg, $x, 77, 0, 0, imagesx($gameimg), imagesy($gameimg));
}
$ColorText = imagecolorallocate($img, 255, 255, 255);
ImageTTFText($img, 10, 0, 10, 127, $ColorText, "font.ttf", $gamercard['Gamertag']);
ImageTTFText($img, 10, 0, 135, 127, $ColorText, "font.ttf", $gamercard['Gamerscore']);
imagepng($img);  
imagedestroy($img); 
?>

 

which is made to call the updateStats() function 1 time everytime the page loads.

Heres the function (its in api.php)

function updateStats($gamertag, $type, $size)
{
$query = mysql_query("SELECT * FROM loadstats where Gamertag = '$gamertag' && Type = '$type'") or die(mysql_error());
$exists = mysql_num_rows($query);  
if ($exists == 0)
{
	mysql_query("INSERT INTO loadstats (Gamertag, Type, Loads, Data) VALUES('$gamertag', '$type', '1', '$size' )") or die(mysql_error());  
} else {
	$row = mysql_fetch_array($query);
	$newloads = $row['Loads'] + 1;
	$newdata = $row['Data'] + $size;
	mysql_query("UPDATE loadstats SET Loads = '$newloads' WHERE gamertag = '$gamertag' && Type = '$type'") or die(mysql_error());  
	mysql_query("UPDATE loadstats SET Data = '$newdata' WHERE gamertag = '$gamertag' && Type = '$type'") or die(mysql_error());  
}
}

 

The wierd thing that is happening is, When i have the Header() set in the script, the updateStats() function appears to be called twice, but when the header is commented out, it is only called once.

 

I can tell this by the change of data in the MYSQL database, the Loads value increases by 2 when the header is set, and it only creases by 1 when it isnt set.

 

 

Any insight as to why this is happening would be greatly appreciated.

The following is completely off topic, but all the logic in your updateStats function can be replaced with one single query by setting up the gamertag and Type columns as a unique composite key -

 

<?php
function updateStats($gamertag, $type, $size)
{
$query = "INSERT INTO loadstats (Gamertag, Type, Loads, Data) VALUES ('$gamertag','$type',1,$size)
	ON DUPLICATE KEY UPDATE Loads = Loads + 1, Data = Data + $size";
mysql_query($query) or die(mysql_error());
}

As to your actual problem, FF is known to request pages twice when you are using the firebug addon and also when the web page character set encoding is different from the default character set in the browser's settings.

 

You also might have some invalid html in the <img tag you are using to request that script or you might have some url rewriting or redirecting that is causing this, that only affects FF.

 

If the problem is not due to something that you have control over in the markup of your page or on your server, you will need to detect duplicate requests in the script and prevent additional calls to the updateStats function (you would still need to output the image so that the image itself will be displayed.)

 

You would typically identify something about the request that would be the same for duplicate requests, such as the $_GET parameters being the same, but would change or have a different value for that particular visitor when the request should cause the updateStats function to be called again. You would store the values in $_SESSION variables and test those session variables on each request to determine if the updateStats function should be called on any particular request. If you have multiple <img tags on one page that target that script, you would need to use arrays for the $_SESSION variables so that the code would be able to remember and test the data from the multiple sets of requests for any one visitor.

Further to the above, as to why it acts differently with and without the header(), I have not specifically seen that problem, but it's most likely due to how the browser is referencing or caching the dynamically produced image or even how you are testing (do you even have a web page with an <img tag that is referring to that dynamically produced image) and it would likely take having enough of your calling code to reproduce the problem to specifically help.

Firefox had (has?  dunno if it's been fixed) an issue where when you loaded an image directly it would request it twice.  Once to render the image in the browser, and again to use it as a favicon in the URL bar.  Could be that your seeing.  The best way to figure out if it is multiple requests is to use a tool that logs requests and see how many are made.  I prefer using Fiddler2 for this.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.