Server Caching PHP executions

Status
Not open for further replies.

talk2azs

Member
Messages
103
Reaction score
5
Points
18
OK. So here is the problem.
Last week I posted a thread of a possible security risk on x10hosting.com
The good news is that it was determined to NOT be a security risk after
my thread was placed into the admin area for further investigation.
The problem turns out to be that x10hosting severs are now caching page/executed
content after a php script runs. I was told the server keeps the cache for
2 to 3 seconds. I found it to take longer than that, and sometimes the cache does not clear at all.
But since my post, I found the server cache is now wreaking havoc on another one of my scripts.
This time it has to do with my web page hit counter. In my code, I also have it
e-mail me the first time it gets a hit from an IP address. If it gets hit again
from the same IP address within 5 minutes, the counter will continue to increase
but not send me an e-mail. It is set to only log that hit in a text file. It
was designed that way so that x10hosting servers do not e-mail me with many
e-mails should a remote visitor hit refresh on their web browse. The problem
is that the servers cache retains the variable information. And when another PC/mobile
browser hits the page, The IP address is logged as the last visitor from the
last device.
So I dumbed down some code to try different things to see if I could cure
my problem. No solution yet! Here is what I found.
The object is to take the page count and display it as a graphic within a web
page in a png file format. That part is working!
Here is the html line I add into my html code:
<img src= "{x10hosting server path}/count.php" alt="counter"/>

Here is the PHP code inside count.php
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$filename = 'cache.txt';
$handle = fopen($filename, 'r');
$contents = fread($handle, filesize($filename));
fclose($handle);
$handle = fopen($filename, 'w');
fwrite($handle, $contents + 1 );
fclose($handle);
// Create a 500*30 image and display the IP address and urrent counter value.
$im = imagecreate(500, 30);
// White background and black text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 0);
// Write the string at the top left
imagestring($im, 5, 0, 0, 'Connecting IP:' . $ip . ' Page Counter:' . $contents , $textcolor);
// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
=======
TEST #1
=======
Lets say PC/device #1 has IP address a.b.c.d and the current page counter is set at 301.
PC/device #1 web browser shows the graphic as "Connecting IP:a.b.c.d Page Counter:300"
THIS IS CORRECT!!!
Now when going to PC/device #2 with an IP address of w.x.y.z and access the page calling
the image, it displays the INCORRECT information,
"Connecting IP:a.b.c.d Page Counter:300"
the same as what was displayed on PC/device#1.
Now when I refresh PC/device #2, it now shows,
"Connecting IP:w.x.y.z Page Counter:301" THE CORRECT IP ADDRESS AND COUNT!!!
Now it gets even better! When I go back to PC/device #1 and hit the page again, it displays,
"Connecting IP:w.x.y.z Page Counter:301", THE WRONG IP ADDRESS AND COUNT!!!
Then when I refresh PC device #1, it displays:
"Connecting IP:a.b.c.d Page Counter:302", THE CORRECT INFORMATION!!!
Now when I continue to do a refresh on PC/device#1, the count will increment by 1
after multiple refresh clicks. even with caching turned OFF!
================================
TEST #2....A partial solution!!!
================================
Here is another interesting finding:
If I simply access http://{x10hosting server path}/count.php from the web browser, CACHING TURNED OFF,
it will display the proper information. When I hit refresh, it takes multiple refreshs
to increment the count by 1.
Now I found that if you add an argument to the end of the line such as ?a=1,
http://{x10hosting server path}/count.php?a=1
The counter incrments by 1. Lets say the counter is at 395 now!
Then I go back to the line and change it to ?a=2,
http://{x10hosting server path}/count.php?a=2
The counter incremnts to 396 right away and no refresh needed.
Then I set a=3, and the counter immediately incrments to 397, again right away and no refresh needed!
Now if I set a back to 1, ?a=1
http://{x10hosting server path}/count.php?a=1
The image that comes back shows the counter back at 395, THE CACHED INFO ON THE PHP SERVER!
I hit refresh multiple times and the counter eventually goes to 398!
Keep in mind that my web browsers caching is turned OFF!
Also keep in mind that the last IP address that hits the code is showing on another PC connecting through a
completely different public IP address on a completely different network, and when a refresh is done,
the correct IP address then shows!
The problem here is that I am unable to change the argument value on the third party web server's page!
It does not support php, so I am unable to create a random and/or incrementing number on the page.
The link must remain static. And I not able to transfer the web page to x10hosting's server!

=========
SIDE NOTE!!!
=========
I also edited the code to just echo the text information instead of returning to the browser as text and
NOT an image for testing purposes. I got the same results.
===========
QUESTION!!!
===========
Does anybody know how to fix or work around this problem? I am at my whit's end with this issue!
My only recourse for now to resolve the problem is,
I had to re-instate my old web host account and move my counter code to their system.
Their system works fine and does NOT cache the previous excutions! Data is updated on every hit and
NO refresh is needed!

Any help would be greatly appreciated!
 

talk2azs

Member
Messages
103
Reaction score
5
Points
18
The Varnish cache server was spoken of in the reply back from X10hosting. I will try that header fix in the .htaccess and hope it won't break any of my other scripts. ;)

But if that ends up not working, other fix attempts and suggestions would be great appreciated also.
 

AngusThermopyle

Active Member
Messages
319
Reaction score
52
Points
28
Post your final code here inside tags ...
PHP:
// php code here

Make sure code is not called twice somehow.

A link to a test page URL would be nice.
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
The user agent may be issuing a HEAD request to revalidate; you may find yourself having to intercept $_SERVER['REQUEST_METHOD'] to do nothing on 'HEAD', depending on how your code is structured (and how the web server is handling HEAD requests).
 

talk2azs

Member
Messages
103
Reaction score
5
Points
18
OK. Apparently Mozilla Firefox calls count.php script 2 times if you just enter the web address http://talk2azs.x10host.com/_test/count.php. But when I call the script within the <img....> html code in page.htm, http://talk2azs.x10host.com/_test/page.htm, the count is working just fine and incrementing by 1 in all browsers.

So appending that header into the .htaccess file seems to work like a charm. Also, so far, none of my other scripts on the server have broken.

Thanks so much for helping with this issue.
 
Status
Not open for further replies.
Top