Changes to code won't display from potential cache issue - php

There's an issue I'm experiencing that seems related to caching, but I'm not 100% positive.
I'm working with MAMP to render PHP inside of HTML. When I write some lines of code and go to the appropriate page, my code is manifest. However, if I make small changes to the code, I'm not able to get it to change with a refresh.
More than that, I'm finding that hard refreshes with and without cache clearing aren't solving the issue as well.
Here's a concrete example. Within PHP I declare a couple of numerical variables. I then set an if statement so that if the second variable is higher than the first, text is displayed on the page. When I change the value of the variables so the opposite would be true, the text continues to show on the page, even though the if statement is false.
I refresh, hard refresh, clear the cache, and even close the browser window. It often takes several combinations of these to finally register my change.
I thought the issue might have been with MAMP and so I tried it with XAMPP, but the results were the same. This makes sense, as they both use an Apache server (provided the issue is server related).
This is becoming very bothersome and I'm not sure where the issue lies. Any idea what might be the problem here?

Related

Refreshing an entire html/php page smoothly

Case: A php page with SQL needs to be refreshed every 30 seconds, but due to hardware limitations, there is a 0.5-2 second blank screen. Values do need to be retrieved from the database on every refresh, but the idea is to use a kind of seamless transitional effect, but where the transition is from one page to itself. 15 years ago or so, I experimented with what is described here: https://www.hongkiat.com/blog/meta-tag-hidden-features/ and I need something similar, though I am not even entirely sure that it would work. The page is very light and does not contain any images, so the load time is not the issue.
So, in short: How do I make it "impossible" to see that the page is refreshed, though values might have changed (or at least without the page going momentarily blank)?
I have tried numerous approaches with js and ajax functions to take care of the reloading instead of <meta http-equiv="refresh">, but none of them seem to be able to do what I want.
As suggested by #ivar and $usman-afzal, the issue was resolved using Ajax. Having no experience with Ajax/jQuery whatsoever, it took a while to get all the different values right and put them in their correct positions, but I succeeded, so thanks for the tip.

Random session data loss in PHP

Here is the problem we have been facing for the past few weeks.
1/ Our setup
PHP 5.4 + MySQL
2 dedicated servers, load-balanced
Sessions are replicated between the 2 servers using memcached
3 applications running on these servers :
One custom-developped application, using default php session settings
Another custom-developped application, using different session settings (cookie name, path)
One Wordpress CMS
2/ The problem
The problem occurs on our first application.
Some of our users reported that they sometimes get disconnected after a few minutes (when the session is setup to last 3 hours). It can happen to them several time in the same day, then no disconnection for a few days, but the problem always comes back.
So far the fraction of users impacted is small, but I would like to solve this before it "spreads" to other users.
The problem seems to occur in different places of the application, though we have identified 3 scenarii where most of the errors occur :
Some involve submitting a form ($_SESSION variable is modified)
Other simply involve opening a popup page, with no modification of the session data
We have tried to reproduce the different scenarii described by the users : sometimes we have been able to, but most of the time we don't have any problem, which makes it hard to debug.
Other notes :
The problem is recent, this application had been running for years without any problem.
It doesn't seem to be related to our server load, because the problem still occured during the summer break when our trafic was low
It only affects one session/users at a time: all the other users logged in at the same time don't experience this problem
The problem occured on all the different browsers (IE, Firefox, Chrome)
3/ Technical analysis
When a disconnect occurs, the user is redirected to a page "Your session has expired or you don't have the right to view". When this page is loaded, we get a technical email with a dump of the $_SESSION variable.
When a session expires the normal way, the email we get shows that the $_SESSION variable is empty (normal behavior).
When an unexpected disconnect occurs, what is interesting is that the $_SESSION is not entirely empty : out of the ~20 elements the array contained, only one is left (always the same).
So this would mean the session is not expired, but not enough data is left to "identify" the user, hence the "no rights" page displayed. As a confirmation when this occurs, we can check in memcached that this session still holds some data.
These are the potential problem causes we have identified so far, and what we have done to rule them out :
Memcached indicates between 70 et 80% freespace, so we don't think it is the problem.
We removed Memcached and went back to using a NFS shared directory for session files: the problem actually got worse. This would point to an applicative bug, because NFS being slower to write data, session loss would occur more often.
We have browsed all the different forums (including SO) talking about PHP session data loss, and reviewed our code accordingly. The code base is big, but we have used automated tools and scripts to avoid missing a file.
session_start() is called at the beginning of each page.
exit() is called after each header("Location...")
register_globals is Off
We have tested the possible interractions between our 2 other applications and the problematic one, though they don't share any code, database or session handling. Nothing identified there.
We have analyzed our access logs around the times of the disconnections, to check for behavior patterns : no luck here either.
So we have no idea what causes this problem, as it seems to occur randomly, so my questions are :
The problem could come from our code: did we miss anything to check ? This solutions seems unlikely as the code works most of the time for all our users, but I am still considering it.
The problem could come from another application/process that would "empty" part of the session variable array. We have also reviewed the code from the other applications, but didn't find anything that could cause this.
And if another process is doing this, why would it only empty some sessions and not all of them ?
Thanks for your help.
I don't think you'll get a definitive answer to your question. There are too many probable causes and you haven't shown any code.
Still, my guess is that you have memcached.sess_locking turned Off, or if you have a custom session implementation - that it doesn't implement locking at all.
Eventually, this leads to a race condition between two simultaneous HTTP requests.
My guess is based on the often seen bad advice to turn off locks or free them as soon as possible, in order to achieve higher performance.
If this problem "suddenly" occurred, check what has changed. Did you do any work on the application? If so check committed code (you talked about automated tools so I expect there to be a repository which would allow for accurate finding of code changes).
Did you change anything on the server? Like upgrade software, upgrade/change hardware, make changes to the other two applications ?
One thing that popped to mind, did you check the drives you use for caching? It could be a corrupted part of the file system. Which would explain the random user part.
I couple of things I always to is:
Try to determine the moment of first occurrence as accurate as possible. At my work this occasionally triggers someone saying "oh yeah that might have to do with when I changed/updated/created this or that" so this might help. On the other hand it can sometimes takes days, weeks or more before something gets noticed so start expanding that time-frame if nothing comes up.
You have already a couple of scenario, find the common factor in these. If they don't share any code, stop looking there. If they DO share code search there. Of course sharing (part of) it here might allow us to help you search.
Do an organised search. I usually do the main application check when I am the one working most on the application (or even better when I created it). A colleague will check surrounding applications that might have influence on it. In your case those 2 other applications. Finally our sysadmin will check for newly installed or updated software on the server(s) and he will also check with our network guys if anything changed hardware wise or network related (for other people this could be the hosting provider).
It could be as simple as a WordPress plugin that uses sessions and calls either session_name() or session_id() with a different value, overlapping your custom applications with default session settings.
Since WordPress itself does not use sessions, plugins are often written from the perspective of having free rein with sessions. I just did a search on a WordPress test site and found sessions used in a gallery plugin, a plugin for putting a background image on the page, a shopping cart plugin, and a plugin I was writing that needed to carry an uploaded file from one admin page to another.

Why is my PHP Script not working when JavaScript is switched on? (but only in certain browsers)

I'm new to PHP (and web development in general) and have come across something that to me seems really, really bizarre!
Background
I am currently designing and developing an online enrolment form for my employer, a training company. The form consists of 3 pages - pages 1 and 2 are for data input whilst page 3 is a summary page with only one input, a box to check that the customer agrees to our T&Cs. Page 3 also includes a box where calculations are made as to the price of the selected training course based on choices made earlier in the form.
The form displayed on page 2, and some of the text on page 3, changes depending on a choice made on page one - that is, whether they are paying for the course themselves or their employer is funding it. If they choose employer, they get one form, if they choose self funding they get a different one. However, both of these forms are contained within a single .php file, using session variables to decide which one should be displayed.
I am using sessions to transfer the data between the pages. Each page has been made sticky using session variables. Real-time validation is carried out using javascript on each individual page, then a final PHP validation check is run on the whole thing when the customer tries to submit the final page. Javascript is also used on page one to calculate the price of the course in real time so the customer can see how much he/she will be paying before proceeding further.
The Problem
I find this really strange:
In Firefox, everything works perfectly
In Internet Explorer, when JavaScript is turned off, everything is fine. But when JS is switched on, the PHP validation on the final page seems to go haywire, thus making it impossible to submit the form.
In Chrome, again when JS is switched off, everything works, but when switched on, I can't even get past the first page. Instead of loading the correct form for page 2, I just get a blank screen.
Can anyone help? This is my first major project and I've been working on it for weeks, coming up against all sorts of problems and tearing my hair out but managing to solve them. Now I'm completely stumped, when I'm almost within touching distance of completion!!
I can't post the code here as it's 3 separate documents and very complicated, but if you want to have a look at the form itself go to:
http://testing.xenongroupadmin.com/testing/enrolment.php
I hope someone can help! Thanks very much!
You are most likely getting errors on the page2.php,
See error reporting to display and debug those:
http://ca.php.net/manual/en/function.error-reporting.php
When I have validation errors in non-Firefox browsers, it's usually because I have some error-reporting code in my JavaScript. Just something like
console.log(testValue);
can bring IE to its knees. If you have any code like that, remove it before testing in other browsers.
Also make sure that all variable declarations are done with "var", and that you're using semicolons at the end of each line (the spec should really enforce this, but I guess it doesn't, and browsers handle it differently).
I've only just seen all these answers, so apologies for not responding sooner.
I managed to fix the problem - turns out it was all to do with JS, no problems with my PHP at all. There were two issues. Firstly, a few syntax errors (which it took forever to find). Secondly, a lot of my HTML elements had id's and classes with identical names (e.g. id = "example" class = "example") which seemed to be throwing the javascript a bit.
Anyway, thanks for all the responses, and also apologies for not being very clear on some aspects - 'going haywire' was a pretty poor way of explaining the problem!!
Cheers

PHP page stops loading halfway, using ODBC, datasource, MSSQL

We have a page that is mostly static with a few PHP includes, each of which pull data from our MSSQL database.
There is a very strange issue where pages will randomly stop rendering. The problem is sporadic and not always visible. Sometimes the pages load correctly, sometimes they stop before reaching the end of the file.
The page in question where you can see the problem is at
Dev: http://author.www.purdue.edu/discoverypark/climate/
Prod: http://www.purdue.edu/discoverypark/climate/index.php
If you refresh the page repeatedly you will hopefully be able to see the issue. The problem only exists on pages that include calls to our database, but again the pages load completely normally most of the time; only sometimes it will stop outputting the page. It has broken inside of normal html as well as before and inside php blocks.
The issue seems to almost be worse in the production environment; the only difference between the two would be the datasource connection to the DB.
Are there any known issues of this with PHP, ODBC, and MSSQL? It is obviously tied to the calls to the database, which are all stored procedures. Could it be an issue with the datasource?
Any input would be appreciated.
I consistently see this in "View Source" when it dies:
<div class="wrap">
OVPR
<img alt=">" src=".
I would guess that your image caching or image URL generating or image handling is probably broken somewhere, and it's aborting for lack of an image.
The > INSIDE the alt value is also not kosher. That needs to be escaped with http://php.net/htmlentities
It might "work" but it won't validate, and a page that doesn't validate is just plain broken.
The DB connection differences between, say, localhost in DEV and separate boxes in PROD is probably changing the timing / frequency of the issue, but is almost for sure a red herring...
Though if a DB call to look up the OVPR image is doing a die()...
For sure, though, if you don't have 10 lines of error handling around every call to odbc_* or mssq_* in your database code, then you've done it wrong, and need to add that.
PS
It should be trivial to switch from ODBC to mssql_* or sybase_* driver, or PDO::* and eliminate at least one possible contender, if none of the above work out. I say again, though, that the DB is 99% for sure a red herring, and you've done something that will be obvious, dare I say silly, once you trace it through to the real cause...
Make sure there isn't a die or exit in the code anywhere
Edit -- If there is, remove this, and view the error
Have you checked normal debugging methods? What does the code look like - specifically, the error handling around your ODBC calls? You don't have a top level return or a misplaced die(), do you?
When I see the page in it's not-rendering state it seems to be because the page is clearly incomplete and it's XHTML.
I see it normally die here -
OVPR
Try bumping up your error reporting level so that you can see any warnings, errors, infos that might be suppressed at the server level.
http://php.net/manual/en/function.error-reporting.php
// Report all PHP errors
<?php error_reporting(-1); ?>

TYPO3 protected sites access to GET variables

I am writing an TYPO3 Extension and everything is working fine right now. I Access the GET Variables via
t3lib_div::_GET('rid');
This does work on the testsite I added my Extension to, but if I add it on another subsite of the same page which is in an access-restricted area this does not work. I use var_dump to look at the GET vars, and on the normal site it works, on the restricted I dont get anything (not even NULL!) Just no output and the logic also does not take it. How do i fix that, or ist there another way to access the GET variables in that case?
I guess that happens because within the first request the output of your extensions is stored within the cache. And the second output is just generated out of the cache (instead of regeneration within your extension). To avoid that you could just make your Extensions not cacheable (USER_INT) or use cHash to show that cache-entries are related to more input values than just the simple page-url...
cHash is explained in the the mysteries of cHash article and I guess you'll find enough information regarding USER vs. USER_INT Objects with google ;)
I have no clue why, but seems to be some kind of caching issue. I always cleared the Typo3 cache so it was not directly a problem with that, but if i set the "nocache" flag for the site the plugin is on, everything works fine. So actually it has nothing to do with the access thing, but I do not understand why this doesnt work without nocache.

Categories