PHP seems to be caching my code - php

I'm currently testing a web application and kept noticing my changes weren't being updated after each save of the PHP file.
As far as I'm aware, I'm using no current PHP caching solution. I'm running PHP 5.5.3 with a fresh copy of CodeIgniter.
Here's what happened:
I noticed PHP didn't seem to be loading the latest code changes as I made them
After noticing the problem, I output <?php echo time();?> into all my view files
Now, the time updates on every page load - indicating PHP is processing each page's code
But still sometimes the page code updates don't take place until 3-5 page loads later
I've tried disabling and empty my browser cache multiple times
Am I right in thinking this is a PHP issue rather than a browser one, given the fact that the time() output is being updated?

PHP doesn't cache anything.
If time() is being updated, you know that there isn't a traditional cache issue.
It's possible you are dealing with filesystem caching if you are using something like NFS. That would explain the 3-5 second delay.

Well that was frustrating. Turns out that MAMP now installed and enables, by default, Zend OpCache - see Stop caching for PHP 5.5.3 in MAMP

Related

Suspected Caching Issue with CakePhp 4

I'm recently getting back into programming after taking a break for quite some time. I'm currently trying to rebuild my CakePhp 2.X apps for CakePhp 4.X. I'm developing in a local environment using Bitnami WAMP stack.
The issue is that development is very slow because changes do not seem to be taking effect immediately. I have tried disabling all caching by using
Cache::disable();
I've tried placing this line in various places
config/boostrap.php
config/app.php
config/app_local.php
src/application.php
How this is impacting me: I'll make an update, for instance, to a model table file or controller file. I'll go refresh my site to preview the change and either there is no update or there might be an error. To fix the error, I try to undo the changes I made. I go back to my browser and hard refresh the page. I continue to see the same error for 10+ minutes. This often leads me to wanting to undo previous steps but I know that those previous steps didn't cause the issue and it was only the most recent change that caused it. It's making it difficult to keep track of what changes are causing issues and what solutions are working. Even something as simple as updating my navigation element (templates/elements/nav.php) to add a new link does not show on the page when I refresh. I've also tried clearing my browser cache (I use Chrome).
Did you try to clear all keys? You can do it with:
// Will clear all keys.
Cache::clear();
Cache::disable() should work also.
You can also delete the contents of /tmp/cache/ if the caching is set on File.
Maybe the problem is Bitnami WAMP itself. Try to disable the server cache:
https://docs.bitnami.com/installer/infrastructure/wamp/administration/disable-cache/
If you are developing on top of an AMP Stack or customizing any
Bitnami Stack, your files (like JavaScript files) may be cached by the
server and even you modify them your changes will not appear to be
applied.
In order to disable the cache in the server and let the files be
served each time, disable PageSpeed for Apache and OPCache for PHP,
enabled by default

Apache or PHP keeps the HTML output in some kind of cache for a couple of minutes?

I recently ditched XAMPP on my Windows 10 machine and re-installed Apache (2.4), PHP 7 and MySQL manually (I followed the instructions given here in order to be able to switch between PHP versions easily).
Everything works fine, except that now when I make a change in a PHP file and hit Refresh in the browser, the change often doesn't appear immediately in the browser. No matter how hard I hit F5 (or Ctrl+F5), I still get the non-modified source code, and I have to wait a couple of minutes before those change are finally visible to the browser.
Needless to say, it's quite annoying when developing. And it didn't happen when I was using XAMPP.
So there seems to be some kind of cache somewhere, but I can't find where it is. I don't know if it's Apache or PHP, although I suspect it might be PHP, because the CSS or JS files are not affected by this problem (as far as I can tell).
Any idea what's causing this behavior and how to disable it?
EDIT: I did some more testing.
I created the simplest PHP file possible. Just:
<?php
echo 'test1';
I can confirm that the problem occurs even in this simple case (changing "test1" to "test2": the browser still shows "test1" for a while).
Opening the same page in another browser still shows the outdated code (test1 instead of test2).
Clearing the browser cache doesn't help.
So the problem doesn't seem to happen on the client side.
However, if I do the same test with an HTML file instead of a PHP file, then the problem doesn't occur. Any change done to that file is visible immediately in the browser (of course I'm still accessing this file via Apache, so http://localhost/some-path/test.html)
So the problem seems to affect only PHP files.
It seems the problem was caused by the OPCache module, which I had to enable in order to work on another (drupal 8) project.
In php.ini, the following line:
; How often (in seconds) to check file timestamps for changes to the shared
; memory storage allocation. ("1" means validate once per second, but only
; once per request. "0" means always validate)
opcache.revalidate_freq=60
Changing 60 to 1 (and restarting Apache) basically solved the problem.

MAMP + dreamweaver + delay issue

I am very very new at PHP programming.
I am making up some code to test my learning but I just discovered a very annoying problem.
I have no idea about the cause, so following I'll describe my situation and I'll leave to you the "honor" to find the problem :)
I use dreamweaver to write my PHP code.
Once I made all the changes, I save the file and refresh the browser page to see the result.
Well, here is the problem. For like 30seconds nothing changes, after this time delay the page actually refreshes. There seems to be a delay between when I save the modified file in dreamweaver and when the browser (Chrome in my case) sees the new file.
I tried different browsers and I also verified that the file is physically changed...I have no clue.
It has nothing to do with Dreamweaver.
You have to disable OPCache
Some software such as MAMP turns on OPCache by default in the php configuration file (php.ini), you can disable it this way
opcache.revalidate_freq=0

PHP 5.2 Unexplainable - File stops executing properly

I have multiple virtual hosts running PHP 5.2.17. Periodically a script will seemingly randomly stop working properly. The script will silently fail and the browser will attempt to download the file. The only solution I've been able to find is ridiculous.
I have to go through all the files executed for the request in question. In each file I make an arbitrary change such as a blank line or extra space and save the file. I refresh after every save and eventually I find the file causing the issue and all is well again until the next occurrence.
My only hunch is that it has something to do with the function_exists function. It seems to be a common denominator in messed up files and that function is pretty uncommon. I can't seem to find anything to back up my guess and I can't find any evidence of it myself.
You have an issue with some sort of PHP optimizer/cacher/accelerators (Zend Optimizer, eAccelerator, APC, XCache, IonCube, etc.)
That is the only time that changing the file by that small amount would make a difference.
Normally every time you access a PHP file it is converted into bytecode and then processed by PHP.
An accelerator (among other things) saves the bytecode the first time you access the file to speed things up, and would only refresh the bytecode cache if it thinks the file has changed.

Problems with APC on publish

We've recently enabled APC on our servers, and occasionally when we publish new code or changes we discover that the source files that were changed start throwing errors that aren't reflected in the code, usually parse errors describing a token that doesn't exist. We have verified this by running php -l on the files the error logs say are affected. Usually a republish fixes the problem. We're using PHP 5.2.0 and APC 3.01.9. My question is, has anyone else experienced this problem, or does anyone recognize what our problem is? If so, how did you fix it or how could we fix it?
Edit: I should probably add in some details about our publishing process. The content is being pushed to the production servers via rsync from a staging server. We enabled apc.stat_ctime because it said this helps things run smoother with rsync. apc.write_lock is on by default and we haven't disabled it. Ditto for apc.file_update_protection.
Sounds like a part-published file is being read and cached as broken. apc.file_update_protection is designed to help stop this.
in php.ini: apc.file_update_protection integer
apc.file_update_protection setting
puts a delay on caching brand new
files. The default is 2 seconds which
means that if the modification
timestamp (mtime) on a file shows that
it is less than 2 seconds old when it
is accessed, it will not be cached.
The unfortunate person who accessed
this half-written file will still see
weirdness, but at least it won't
persist.
Following the question being edited: One reason I don't see these kinds of problems is that I push a whole new copy of the site (with SVN export). Only after that is fully completed does it become visable to Apache/Mod_php (see my answer How to get started deploying PHP applications from a subversion repository? )
The other thing that may happen of course, is that if you are updating in place, you may be updating files that depend on others that have not yet been uploaded. Rsync can only guarantee atomic updates for individual files, not the entire collection that is being changed/uploaded. Another reason I think to upload the site en-mass, and only then put into use.
It sounds like APC isn't preforming or getting the correct file stat info. You could check it to make sure the APC configuration apc.stat is set correctly. Another thing you could do it force the cache to clear with apc_clear_cache() when you publish new code.
Never saw that before, even if i'm a huge user of APC.
Maybe try to trigger a script that empty the APC opcode everytime you send new code on the server ?
When you get a file with a parse error, back it up, then repubish. Take that same file that now works and do a diff against the file with the parse error.
ctime means creation time. You will want to manually flush your entire cache every time you do updates.
You can easily do this, by putting the apc.php script somewhere on your server. This script gives you cache statistics, and will allow you to drop the cache altogether.
The script comes with APC.
Hopet his helps,
Evert
This is probably happening because there's a mismatch between your code, and the cached versions of the code.
For example, APC has a cached version of User.php, but you made changes to User.php or to the data that User uses. The cached version is still running even after your deploy, because it hasn't expired yet.
If you clear your APC cache entries when you deploy, this issue should disappear.

Categories