By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,671 Members | 1,633 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,671 IT Pros & Developers. It's quick & easy.

Does PHP "cache" scripts?

P: n/a
A really simple script is driving me up the wall.

I have a very simply facility on a website to allow the user to reorder
items in a database table as she wishes by clicking a link that (in
this case) says "MOVE UP" and links to the script below.

This script snippet that does all the work is shown - the database
connection is established and after doing its work the script returns
the user to the referring page.

---------------------------------------------
$sql="select DisplayOrder from CreditType where Id=" . $credcat;
$result=mysql_query($sql);
$row=mysql_fetch_row($result);

$sql="update CreditType set DisplayOrder=" . (($row[0])-1) . " where
Id=" . $credcat;

mysql_query($sql);

$sql="update CreditType set DisplayOrder=" . ($row[0]) ." where
DisplayOrder=" . (($row[0])-1) . " and Id<>" . $credcat;

mysql_query($sql);
--------------------------------------------

On my own server this works perfectly - but when I upload it to my
client's shared server it sometimes works and sometimes doesn't. It is
intermittent.

Could PHP be returning a cached result of the script? If no, what on
earth can be happening? If yes, can I stop this from happning from the
script (I can't change any settings on the shared host).

Many thanks.
Steve

Sep 14 '06 #1
Share this Question
Share on Google+
17 Replies


P: n/a
On 14 Sep 2006 13:41:23 -0700, St***********@gmail.com wrote:
>On my own server this works perfectly - but when I upload it to my
client's shared server it sometimes works and sometimes doesn't. It is
intermittent.
How do you mean it doesn't work, exactly?
>Could PHP be returning a cached result of the script? If no, what on
earth can be happening?
PHP doesn't cache scripts. There's extensions like APC that cache the compiled
form of the script, but that doesn't (or shouldn't) have anything to with the
results.

MySQL can be configured to have a cache, but I don't know much about that.

Your browser can cache, but you can send headers that advise the browser not
to cache (look up the Pragma HTTP header).

Your ISP may have "transparent" caching proxies; ntl: are infamous for that.

It could be something much simpler; I don't see any error checking on the
mysql_query calls.

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Sep 14 '06 #2

P: n/a
Hi - thanks for the reply. The script is stripped bare - there is error
checking on the actual script and all queries work fine.

When I say it doesn't work - I just mean the user clicks on the link
and is returned almost immediately to the page as if nothing has
happened.

I have just tried it in Internet Explorer and it seems to work there -
but sometimes I have to "refresh" the page to see the effect, sometimes
it updates ok.

In firefox, sometimes it works and sometimes it doesn't. When it does,
the page updates itself to reflect the "move up" - if it doesn't work
refreshing has no effect.

If I try the link in firefox and it doesn't work and then close firefox
down and visit the same link on the same page and then try, it works
(hence a suspicion of caching or session issues).

As I say - it works perfectly in all browsers on my dedicated server.
Exactly the same script - just a different server - so I am assuming it
is a server issue.

I'm flummoxed!

Best,
Steve
Andy Hassall wrote:
On 14 Sep 2006 13:41:23 -0700, St***********@gmail.com wrote:
On my own server this works perfectly - but when I upload it to my
client's shared server it sometimes works and sometimes doesn't. It is
intermittent.

How do you mean it doesn't work, exactly?
Could PHP be returning a cached result of the script? If no, what on
earth can be happening?

PHP doesn't cache scripts. There's extensions like APC that cache the compiled
form of the script, but that doesn't (or shouldn't) have anything to with the
results.

MySQL can be configured to have a cache, but I don't know much about that.

Your browser can cache, but you can send headers that advise the browser not
to cache (look up the Pragma HTTP header).

Your ISP may have "transparent" caching proxies; ntl: are infamous for that.

It could be something much simpler; I don't see any error checking on the
mysql_query calls.

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Sep 14 '06 #3

P: n/a
St***********@gmail.com wrote:
A really simple script is driving me up the wall.

I have a very simply facility on a website to allow the user to reorder
items in a database table as she wishes by clicking a link that (in
this case) says "MOVE UP" and links to the script below.

This script snippet that does all the work is shown - the database
connection is established and after doing its work the script returns
the user to the referring page.
<snip>
On my own server this works perfectly - but when I upload it to my
client's shared server it sometimes works and sometimes doesn't. It is
intermittent.
hmmm, "client's shared server". I can read so much into that.

Go get a copy of iehttpheaders for MSIE or tamper data for firefox and find
out for yourself.
Could PHP be returning a cached result of the script?
No. Not unless you wrote it that way.
If no, what on
earth can be happening? If yes, can I stop this from happning from the
script (I can't change any settings on the shared host).
The webserver could be returning bad caching information. You may be
accessing it through an overzealous cache - you're code didn't contain any
cache instructions.

C.
Sep 14 '06 #4

P: n/a
i would compare the config on both servers, (maybe registered globals is
on on the client).

St***********@gmail.com wrote:
A really simple script is driving me up the wall.

I have a very simply facility on a website to allow the user to reorder
items in a database table as she wishes by clicking a link that (in
this case) says "MOVE UP" and links to the script below.

This script snippet that does all the work is shown - the database
connection is established and after doing its work the script returns
the user to the referring page.

---------------------------------------------
$sql="select DisplayOrder from CreditType where Id=" . $credcat;
$result=mysql_query($sql);
$row=mysql_fetch_row($result);

$sql="update CreditType set DisplayOrder=" . (($row[0])-1) . " where
Id=" . $credcat;

mysql_query($sql);

$sql="update CreditType set DisplayOrder=" . ($row[0]) ." where
DisplayOrder=" . (($row[0])-1) . " and Id<>" . $credcat;

mysql_query($sql);
--------------------------------------------

On my own server this works perfectly - but when I upload it to my
client's shared server it sometimes works and sometimes doesn't. It is
intermittent.

Could PHP be returning a cached result of the script? If no, what on
earth can be happening? If yes, can I stop this from happning from the
script (I can't change any settings on the shared host).

Many thanks.
Steve
Sep 14 '06 #5

P: n/a
NC
St***********@gmail.com wrote:
>
This script snippet that does all the work is shown - the database
connection is established and after doing its work the script returns
the user to the referring page.
....
On my own server this works perfectly - but when I upload it to my
client's shared server it sometimes works and sometimes doesn't.
It is intermittent.
Check register_globals setting on the two servers. Maybe something
writes over your GET/POST variables or vice versa...

Also, how do you handle MySQL errors? The behavior you are observing
is consistent with queries not being executed (which could happen when
your MySQL server is overloaded). The script tries to connect to the
MySQL server, gets an error, fails to execute the queries due to no
connection and redirects...
Could PHP be returning a cached result of the script?
No, but it is possible that the hosting company does. It is unlikely
though, because the problem is intermittent...

Cheers,
NC

Sep 14 '06 #6

P: n/a
St***********@gmail.com wrote:
Hi - thanks for the reply. The script is stripped bare - there is error
checking on the actual script and all queries work fine.

When I say it doesn't work - I just mean the user clicks on the link
and is returned almost immediately to the page as if nothing has
happened.

I have just tried it in Internet Explorer and it seems to work there -
but sometimes I have to "refresh" the page to see the effect, sometimes
it updates ok.

In firefox, sometimes it works and sometimes it doesn't. When it does,
the page updates itself to reflect the "move up" - if it doesn't work
refreshing has no effect.

If I try the link in firefox and it doesn't work and then close firefox
down and visit the same link on the same page and then try, it works
(hence a suspicion of caching or session issues).

As I say - it works perfectly in all browsers on my dedicated server.
Exactly the same script - just a different server - so I am assuming it
is a server issue.

I'm flummoxed!

Best,
Steve
Andy Hassall wrote:
>>On 14 Sep 2006 13:41:23 -0700, St***********@gmail.com wrote:

>>>On my own server this works perfectly - but when I upload it to my
client's shared server it sometimes works and sometimes doesn't. It is
intermittent.

How do you mean it doesn't work, exactly?

>>>Could PHP be returning a cached result of the script? If no, what on
earth can be happening?

PHP doesn't cache scripts. There's extensions like APC that cache the compiled
form of the script, but that doesn't (or shouldn't) have anything to with the
results.

MySQL can be configured to have a cache, but I don't know much about that.

Your browser can cache, but you can send headers that advise the browser not
to cache (look up the Pragma HTTP header).

Your ISP may have "transparent" caching proxies; ntl: are infamous for that.

It could be something much simpler; I don't see any error checking on the
mysql_query calls.

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

Andy,

Not necessarily a server issue. It could be anything between the server
and your system that's caching the page.

But it definitely sounds like a caching issue. Nothing in PHP or MySQL
would do this.

You might try adding a random string as a parameter, such as:
http://www.example.com/showinfo.php?n=12345

where you get the value from microtime() or similar. This should take
care of caching issues.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Sep 15 '06 #7

P: n/a

NC wrote:
Also, how do you handle MySQL errors? The behavior you are observing
is consistent with queries not being executed (which could happen when
your MySQL server is overloaded). The script tries to connect to the
MySQL server, gets an error, fails to execute the queries due to no
connection and redirects..
Thanks - there are definitely no sql errors (there is error checking on
the mysql queries - I removed it here for brevity.

Sep 15 '06 #8

P: n/a

Kim Hunter wrote:
i would compare the config on both servers, (maybe registered globals is
on on the client).
All the variables used in the script are sourced from the relevant
$_POST or $_GET. The script was designed for register_globals to be
off.

I'll check though it there are any discrepancies in the config that
might cause this problem.

Sep 15 '06 #9

P: n/a
Thanks everyone. I'll try your suggestions tonight and post back.

Steve

Sep 15 '06 #10

P: n/a
St***********@gmail.com wrote:
Kim Hunter wrote:
>i would compare the config on both servers, (maybe registered
globals is on on the client).

All the variables used in the script are sourced from the relevant
$_POST or $_GET. The script was designed for register_globals to be
off.

I'll check though it there are any discrepancies in the config that
might cause this problem.
I'm not sure but try to flush tables after all updates.

$sql="select DisplayOrder from CreditType where Id=" . $credcat;
$result=mysql_query($sql);
$row=mysql_fetch_row($result);

$sql="update CreditType set DisplayOrder=" . (($row[0])-1) . " where
Id=" . $credcat;
mysql_query($sql);

$sql="update CreditType set DisplayOrder=" . ($row[0]) ." where
DisplayOrder=" . (($row[0])-1) . " and Id<>" . $credcat;
mysql_query($sql);

# this can resolve your problem
$sql="flush tables";
mysql_query($sql);
--

Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)
Sep 15 '06 #11

P: n/a
I have added the random number to the URL and the script now works
perfectly. In Internet Explorer. The database is updated and on return
to the list page the correct order is reflected.

In Firefox it still won't work. The database doesn't get updated and
the random number in the url doesn't change or get updated.

As my client uses IE I guess it's not that big a deal - but it is
frustrating as Firefox is my preferred browser.

Any further thoughts?

Sep 16 '06 #12

P: n/a
I've just run the script in Firefox again and noticed something odd.
There are two links, thus:

MOVE UP - MOVE DOWN

When I click on MOVE UP the database isn't updated but I get this in
the address box when the script returns me to the main page:

http://www.etc.com/credits.php?r=1158400161

When I click MOVE DOWN the database isn't updated and I get this:

http://www.etc.com/credits.php?r=1158400662

if I click MOVE UP again I get

http://www.etc.com/credits.php?r=1158400161

which is the same as the first time I clicked it. The r= variable is
the unix time stamp so either firefox is caching the page or my server
is somehow going back in time! ;o)

If I leave it for a while and try again, the number is updated to a new
number (and sometimes the database is updated and the order on the
credits page is changed).

Steve

StevePBurg...@gmail.com wrote:
I have added the random number to the URL and the script now works
perfectly. In Internet Explorer. The database is updated and on return
to the list page the correct order is reflected.

In Firefox it still won't work. The database doesn't get updated and
the random number in the url doesn't change or get updated.

As my client uses IE I guess it's not that big a deal - but it is
frustrating as Firefox is my preferred browser.

Any further thoughts?
Sep 16 '06 #13

P: n/a
St***********@gmail.com wrote:
I've just run the script in Firefox again and noticed something odd.
There are two links, thus:

MOVE UP - MOVE DOWN

When I click on MOVE UP the database isn't updated but I get this in
the address box when the script returns me to the main page:

http://www.etc.com/credits.php?r=1158400161

When I click MOVE DOWN the database isn't updated and I get this:

http://www.etc.com/credits.php?r=1158400662

if I click MOVE UP again I get

http://www.etc.com/credits.php?r=1158400161

which is the same as the first time I clicked it. The r= variable is
the unix time stamp so either firefox is caching the page or my server
is somehow going back in time! ;o)

If I leave it for a while and try again, the number is updated to a new
number (and sometimes the database is updated and the order on the
credits page is changed).

Steve

StevePBurg...@gmail.com wrote:
>>I have added the random number to the URL and the script now works
perfectly. In Internet Explorer. The database is updated and on return
to the list page the correct order is reflected.

In Firefox it still won't work. The database doesn't get updated and
the random number in the url doesn't change or get updated.

As my client uses IE I guess it's not that big a deal - but it is
frustrating as Firefox is my preferred browser.

Any further thoughts?

Still a caching problem. But not related to PHP. You might get better
answers in a group such as alt.html.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Sep 16 '06 #14

P: n/a

Jerry Stuckle wrote:
Still a caching problem. But not related to PHP. You might get better
answers in a group such as alt.html.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Thanks. I'll try that.

Sep 16 '06 #15

P: n/a
St***********@gmail.com wrote:
I've just run the script in Firefox again and noticed something odd.
There are two links, thus:

MOVE UP - MOVE DOWN

When I click on MOVE UP the database isn't updated but I get this in
the address box when the script returns me to the main page:

http://www.etc.com/credits.php?r=1158400161

When I click MOVE DOWN the database isn't updated and I get this:

http://www.etc.com/credits.php?r=1158400662

if I click MOVE UP again I get

http://www.etc.com/credits.php?r=1158400161

which is the same as the first time I clicked it. The r= variable is
the unix time stamp so either firefox is caching the page or my server
is somehow going back in time! ;o)

If I leave it for a while and try again, the number is updated to a new
number (and sometimes the database is updated and the order on the
credits page is changed).
<snip>

My guess:
1. You're paying more to your hosting provider and they have some
master-slave architecture for kinda data recovery.
2. Caching issue:
a. Your ISP is caching the pages
b. As your scripting is instructing to cache (so you better post the
link & phpinfo so that someone can dig the HTTP headers and provide
suggestion) or using .html extension (some buggy proxy implementation
will flag it as static page).

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Sep 17 '06 #16

P: n/a
Hi all.

Just to "close" this, I wanted to let you know what I did that solved
it.

I typed:

about:config

into the Firefox address box and then changed the configuration
setting:

browser.cache.check_doc_frequency

to 1 (always check for new content).

The default is 3 (regularly check).

This has solved the problem.

This doesn't explain why the behaviour is different on different
servers - but it has solved the problem.

Thanks for all your input.

Steve

Sep 18 '06 #17

P: n/a
NC
St***********@gmail.com wrote:
>
I wanted to let you know what I did that solved it.

I typed:

about:config

into the Firefox address box and then changed the configuration
setting:

browser.cache.check_doc_frequency

to 1 (always check for new content).

The default is 3 (regularly check).

This has solved the problem.

This doesn't explain why the behaviour is different on different
servers - but it has solved the problem.
It does; your ISP probably has a slightly paranoid caching server on
their network...

Cheers,
NC

Sep 19 '06 #18

This discussion thread is closed

Replies have been disabled for this discussion.