Jump to content

php is sometimes executed twice?


hedgehog90

Recommended Posts

I have a games website called GPStudios.com.

In a previous topic (that remains unresolved) I needed help fixing a view counter on a specific page that was sometimes executing a mysql query twice.

I've since noticed that it is happening on other pages, maybe all of them.

 

At the top of the php "playgame.php", is the statement:

$updatesql = "update games set timesplayed = timesplayed+1, last_played = now() where gameid = $gameid";
mysql_query($updatesql);

 

However, when I reload the page or check on phpMyAdmin, it has sometimes incremented by 2 (possibly 3).

 

I have confirmed that no where else is calling the same query. I created a new table called "FUCK" and editted the code stated above to:

$updatesql = "update FUCK set timesplayed = timesplayed+1, last_played = now() where gameid = $gameid";
mysql_query($updatesql);

Upon loading the page, it did exactly the same thing. So I confirmed that the PHP must be being run twice.

 

I have tried it on other pages on my website such as

 

I had no luck on my other topic of the same problem, but hopefully someone might be able to tell me why or how this might be happening.

 

Just remember - it is an absolute certainty that the query is NOT being run elsewhere.

 

Thanks.

Link to comment
Share on other sites

Do you have access to your server logs?  You can check to see if the page is being requested twice, and what the referrer (if any) is.  You might dump to a log file some points within your code to be able to trace what's happening, and hopefully see why your code is being executed twice instead of just once.  Is this a single file or is this included in another file?  Any chance that you have the "include" statement multiple times?

Link to comment
Share on other sites

<?php
    session_start();
    require_once("inc/config.inc.php");
    require_once("class/functions.class.php");
    require('drawrating.php');
    $objGlobal = new globalclass();
    require_once("class/site_register.php");

    $gameid = $_GET['gameid'];
    if(!empty($gameid)){
        $updatesql = "update FUCK set timesplayed = timesplayed+1, last_played = now() where gameid = $gameid";
        mysql_query($updatesql);
    }
    $reviewlistings = $objGlobal->getgamereviews($gameid);	
    $displaytitlesql = "SELECT * FROM games WHERE gameid = $gameid";
    $displaytitle=$objGlobal->get_games($displaytitlesql);
...

 

This is all that's relevant, because there is no other function or query referencing this new table, "FUCK".

 

This is at the top of a file called playgame.php

It is not included in any other php files, just here.

 

...

 

I have just tried something new - I have uploaded a file called playgame2.php, like the table "FUCK" this is brand new, not refenced anywhere else.

 

And it still happens.

 

I have noticed though just a couple mintues ago, that when I load a game for the first time, it'll add 4.

Then, when I reload the page, it'll add 2.

 

 

 

Link to comment
Share on other sites

I have made significant progress:

 

I have made a file called playgame3.php, check it out:

http://www.gpstudios.com/playgame3.php

 

Here's the code within the file:

<?php
require_once("inc/config.inc.php");
$dbConnectID = mysql_connect(DBHOSTNAME, DBUSERNAME, DBPASSWORD);
if($dbConnectID){
	mysql_select_db(DBNAME, $dbConnectID);
}
//
function get_view_count(){
	$sql_query = "SELECT * FROM arse where gameid = 1";
	$result = mysql_query($sql_query);
	if(mysql_num_rows($result))	{
		while($row = mysql_fetch_array($result)) {
			$viewcount = $row['timesplayed'];
		}
	}
	echo "timesplayed = ".$viewcount."</br>";
}
get_view_count();
//
$gameid = 29;
if(!empty($gameid)){
	$updatesql = "update arse set timesplayed = timesplayed+1, last_played = now() where gameid = 1";
	mysql_query($updatesql);
}	
//
$final_width = 800;
$final_height = 400;
$newgamefilepath = "games/files/bunny-invasion-2.swf";
//
$playcontent = "<script type=\"text/javascript\">
document.write('<OBJECT classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0\" WIDTH=\"$final_width\" HEIGHT=\"$final_height\" align=\"middle\" id=\"FlashContent\">');
document.write('<PARAM NAME=\"movie\" VALUE=\"$newgamefilepath\">');
document.write('<PARAM NAME=\"quality\" VALUE=\"high\">');
document.write('<PARAM NAME=\"AllowScriptAccess\" VALUE=\"never\">');
document.write('<embed width=\"$final_width\" height=\"$final_height\" align=\"middle\" src=\"$newgamefilepath\" quality=\"high\" NAME=\"FlashContent\" AllowScriptAccess=\"never\" TYPE=\"application/x-shockwave-flash\" PLUGINSPAGE=\"http://www.macromedia.com/go/getflashplayer\"></embed>');
document.write('</OBJECT>');
</script>
<noscript>
<OBJECT classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0\" WIDTH=\"$final_width\" HEIGHT=\"$final_height\" align=\"middle\" id=\"FlashContent\">
<PARAM NAME=\"movie\" VALUE=\"$newgamefilepath\">
<PARAM NAME=\"quality\" VALUE=\"high\">
<PARAM NAME=\"AllowScriptAccess\" VALUE=\"never\">
<embed width=\"$final_width\" height=\"$final_height\" align=\"middle\" src=\"$newgamefilepath\" quality=\"high\" NAME=\"FlashContent\" AllowScriptAccess=\"never\" TYPE=\"application/x-shockwave-flash\" PLUGINSPAGE=\"http://www.macromedia.com/go/getflashplayer\"></embed>
</OBJECT>
</noscript>";
echo $playcontent;
?>

 

Okay, so there it is - all the code is being executed within a single page (apart from inc/config.inc.php, but that's just my database login details)

 

Run the page, the timesplayed = 150

reload the page, the timesplayed = 152

 

The problem lies within the flash content...

 

Okay, I've done all the hard work, now how do I fix this!?

Link to comment
Share on other sites

If your page is being requested twice by the client and there is nothing you can do about what is going on in the client, you would need to add code on your page to detect and ignore any page request after the first one.

 

You can use a session variable to serve as a flag that is set on the first request so that you only execute the code to increment the timesplayed column once.

Link to comment
Share on other sites

If you remove the flash element does it increment as you expect?

 

Yes.

Like i said - Flash is root of the problem.

 

If flash is calling the page in addition to the browser then that is why it's being pinged twice.  You need to locate in the Flash where it's calling the page, and either remove it or, if it has to be called, account for it in your PHP code.

Link to comment
Share on other sites

Actually, that reminds me, the flash player is its own client and maintains a separate cookie store, so if you are getting one request from the flash client and one from the browser, you won't be able to directly us a session to ignore the second request.

 

You should however be able to detect something different between the two requests and ignore the one you don't want.

Link to comment
Share on other sites

Actually, that reminds me, the flash player is its own client and maintains a separate cookie store, so if you are getting one request from the flash client and one from the browser, you won't be able to directly us a session to ignore the second request.

 

You should however be able to detect something different between the two requests and ignore the one you don't want.

 

And how would you recommend I do this?

 

Surely people must have run into this problem countless times before?

 

Thanks for the help so far, sorry for the delay in replying.

Link to comment
Share on other sites

I would start by finding out everything you can about the two requests. Add the following code to your page so that you can log all the information that the server gets with the requests -

 

<?php
$log_this = date('Y-m-d H:i:s') . '|' . microtime();
foreach($_SERVER as $key => $value){
$log_this .= "[$key]=$value";
}
$log_this .= "<br />\n";
error_log($log_this,3,'log_file.txt');
?>

 

The log_file.txt will contain all the $_SERVER variables for each request to your page.

Link to comment
Share on other sites

Blimey. I got the following report after a single page load:

 

2011-01-23 08:03:01|0.06320700 1295791381[DOCUMENT_ROOT]=/home/hedgehog/public_html[GATEWAY_INTERFACE]=CGI/1.1[HTTP_ACCEPT]=image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*[HTTP_ACCEPT_ENCODING]=gzip, deflate[HTTP_ACCEPT_LANGUAGE]=en-GB[HTTP_CONNECTION]=Keep-Alive[HTTP_COOKIE]=__utma=26257003.834224986.1287699486.1295094119.1295468194.34; __utmz=26257003.1291634664.12.2.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=gp%20studios; __qca=P0-113904449-1287699485875; phpbb3_n6bcx_u=1; phpbb3_n6bcx_k=; phpbb3_n6bcx_sid=9f35b1b309fbbb11bd17042a7d5d5ac4[HTTP_HOST]=www.gpstudios.com[HTTP_USER_AGENT]=Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; Tablet PC 2.0; OfficeLiveConnector.1.5; OfficeLivePatch.1.3; .NET4.0C; InfoPath.3)[PATH]=/bin:/usr/bin[phpRC]=/home/hedgehog[QUERY_STRING]=[REDIRECT_STATUS]=200[REMOTE_ADDR]=87.114.127.242[REMOTE_PORT]=51322[REQUEST_METHOD]=GET[REQUEST_URI]=/playgame3.php[sCRIPT_FILENAME]=/home/hedgehog/public_html/playgame3.php[sCRIPT_NAME]=/playgame3.php[sERVER_ADDR]=174.120.83.250[sERVER_ADMIN]=webmaster@gpstudios.com[sERVER_NAME]=www.gpstudios.com[sERVER_PORT]=80[sERVER_PROTOCOL]=HTTP/1.1[sERVER_SIGNATURE]=<address>Apache mod_fcgid/2.3.5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 Server at www.gpstudios.com Port 80</address>
[sERVER_SOFTWARE]=Apache mod_fcgid/2.3.5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635[uNIQUE_ID]=TTw1FK54U@IAAFiuBboAAAEG[php_SELF]=/playgame3.php[REQUEST_TIME]=1295791381[argv]=Array[argc]=0<br />
2011-01-23 08:03:07|0.30064900 1295791387[DOCUMENT_ROOT]=/home/hedgehog/public_html[GATEWAY_INTERFACE]=CGI/1.1[HTTP_ACCEPT]=*/*[HTTP_ACCEPT_ENCODING]=gzip, deflate[HTTP_ACCEPT_LANGUAGE]=en-GB[HTTP_CONNECTION]=Keep-Alive[HTTP_COOKIE]=__utma=26257003.834224986.1287699486.1295094119.1295468194.34; __utmz=26257003.1291634664.12.2.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=gp%20studios; __qca=P0-113904449-1287699485875; phpbb3_n6bcx_u=1; phpbb3_n6bcx_k=; phpbb3_n6bcx_sid=9f35b1b309fbbb11bd17042a7d5d5ac4[HTTP_HOST]=www.gpstudios.com[HTTP_REFERER]=http://x.mochiads.com/srv/1/49876907fdb169d1.swf[HTTP_USER_AGENT]=Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; Tablet PC 2.0; OfficeLiveConnector.1.5; OfficeLivePatch.1.3; .NET4.0C; InfoPath.3)[HTTP_X_FLASH_VERSION]=10,1,102,64[PATH]=/bin:/usr/bin[phpRC]=/home/hedgehog[QUERY_STRING]=[REDIRECT_STATUS]=200[REMOTE_ADDR]=87.114.127.242[REMOTE_PORT]=51322[REQUEST_METHOD]=GET[REQUEST_URI]=/playgame3.php[sCRIPT_FILENAME]=/home/hedgehog/public_html/playgame3.php[sCRIPT_NAME]=/playgame3.php[sERVER_ADDR]=174.120.83.250[sERVER_ADMIN]=webmaster@gpstudios.com[sERVER_NAME]=www.gpstudios.com[sERVER_PORT]=80[sERVER_PROTOCOL]=HTTP/1.1[sERVER_SIGNATURE]=<address>Apache mod_fcgid/2.3.5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 Server at www.gpstudios.com Port 80</address>
[sERVER_SOFTWARE]=Apache mod_fcgid/2.3.5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635[uNIQUE_ID]=TTw1G654U@IAAFiuBeAAAAEG[php_SELF]=/playgame3.php[REQUEST_TIME]=1295791387[argv]=Array[argc]=0<br />

check it out here:

http://www.gpstudios.com/log_file.txt

 

Can you make sense of this?

Thanks for your help.

Link to comment
Share on other sites

Sorry to impose...

 

Could you use the unique_id from the $_SERVER array to increment the counter?

Something like the following:

<?PHP
  $thisID = $_SERVER['UNIQUE_ID'];

  if(isSet($_SESSION['checkID'])) {
    if($_SESSION['checkID'] != $thisID) {
      // Increment Counter
    } 
  }

  $_SESSION['checkID']=$thisID;
?>

 

As this is untested, I'm not sure if it will work or not, but give it a go and tell me how it goes for you.

 

Regards, PaulRyan.

 

Link to comment
Share on other sites

The following are the differences between the two requests -

 

2011-01-23 08:03:01|0.06320700 1295791381
[HTTP_ACCEPT]=image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
[uNIQUE_ID]=TTw1FK54U@IAAFiuBboAAAEG
[REQUEST_TIME]=1295791381

2011-01-23 08:03:07|0.30064900 1295791387
[HTTP_ACCEPT]=*/*
[HTTP_REFERER]=http://x.mochiads.com/srv/1/49876907fdb169d1.swf
[HTTP_X_FLASH_VERSION]=10,1,102,64
[uNIQUE_ID]=TTw1G654U@IAAFiuBeAAAAEG
[REQUEST_TIME]=1295791387

 

The second request is apparently occurring because the .swf that you are loading on your site is sending your browser to the mochiads site and it is then causing your browser to request your page again, ~6 seconds later in this case.

 

This action is probably out of you hands, so the best you can do is detect and ignore the extra request(s). The UNIQUE_ID is, well, unique and cannot be used. There's also no guarantee that you will get exactly two requests in every case either.

 

It does appear that the request is coming from the browser, so you can probably use a $_SESSION variable to detect and ignore the extra request(s). I would use a $_SESSION['page'] variable that you use to remember the current page. If you navigate to a new page (I would use $_SERVER['REQUEST_URI'] to identify pages as this would allow you to both use GET parameters on the end of one URL to specify different games or different URLs to specify different games), the $_SESSION['page'] variable either won't exist at all (the first time you did this after opening your browser) or it will hold the previous/different page name. In either of these two cases, you would assign it the current page name and increment the timesplayed counter in the database. This will cause the counter to increment only on the first request for any specific page.

Link to comment
Share on other sites

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.