Double GET request in text file? - php

I have a file "host.php" which gives plain text. When I try to run it in browser, once per few refreshes one variable increases by 2, not 1. I tried placing session variable on end, to check if its script error (double var increment) or a http error. Looks like it increases the same way. Also checked Chrome Network debugger. Found one error (with browser trying to load favicon.ico), fixed it and the problem still occurs.
I have seen this question:
What can cause a double page request? which had the same problem, but it wasn't solution for me. I don't use ads or html elements - its plain text.
The result:
The page sometimes loads twice. There is no network errors, like empty src. And its plain text, not html.
The question:
How to fix it? How to prevent page loading twice?
PS: The page never uses POST requests, only get.

I'm not sure is it the correct answer, so I won;t yet mark it as correct.
When I tried the script in Chrome on other device (in Incognito mode), everything worked well (or I just didn't saw...). On my device I'm also using Chrome.
As Incognito mode uses different cookies probably even for each tab (?), it might be fault of some external cookies running (like in linked question, ads could import some). Chrome debugger now only shows 1 cookie, which is PHPSESSID from my site, it looks like its not that.
Afterall, I pressume that its user's device/browser/extensions fault. I also tried disabling Adblock, but that didn't solve the problem.

Related

(PHP) MySQL creates two rows instead of one in Chrome

I've already looked through other questions but couldn't find an answer to this specific problem since it seems its all about the Chrome browser.
I stripped down my code to debug this problem and the only thing I'm executing now is loading my database and doing an INSERT.
However it always inserts two rows instead of one row.
Then I tested this with Firefox and there are no problems. I also disabled all my Chrome Extensions and it's still unchanged. I can't figure out what else might be causing this. Like I said, it works in other browsers.
there is some discussion about chrome making multiple requests here
the gist seems to be that there are some things that could cause chrome to make an additional request, including (but not limited to):
link tags with an empty href property <link rel="shortcut" href="">
image, iframe, or scripts tags with an empty or '#' src property <img src="#">
chrome fetching the favicon
do any of those apply?
regardless of what is causing the double GET, a good solution would be to be to follow standard web development best practices: don't change application state as a result of a GET call. Require a POST.
Server side code (e.g. PHP script) does not depend of your browser. So it couldn't behave differently in different browsers.
But yes, in some cases you may have your code executed two times for single page hit.
Lets assume you have a code that inserts new row every time your page has been visited (for example: old-school visits counter). When using Google Chrome you may be surprised, cause you'll get two inserted rows per single page hit. That happens because Google Chrome makes second request to page just for getting favicon.
Other possibility is an weird condition in your code. Eg. if it is chrome, do something else/more

Browser printing binary data with a HTTP header in the middle

The last 2-3 days some of our users have been reporting this error. Random pages on our site will output raw binary data. None of the developers can duplicate the issue which comes and goes randomly.
All users reporting the issue have so far been using IE11. The server is Apache 2.4.16 with PHP 5.3.29. There are NO errors being logged by PHP or by Apache related to the issue.
Oddly the HTTP header is embedded in the middle of the data. I can't even fathom a reason that would ever happen. One would expect that if the browser was having issues rendering the content, then it wouldn't be making further requests for more resources to the server and there wouldn't be another HTTP header to show.
We really don't know where to start with this one, we can't tell if it's server, php code, or browser related. Is anyone aware of bugs in Apache or IE that would cause this?
Attached is a screenshot one of the users sent.
I found the bug. The PHP code was generating a 302 redirect, but then also outputting it's normal page content. In certain high load conditions outputting the page could take 3-5 seconds. For some still undetermined reason the output from the first (redirected) request was being munged onto the beginning of the new request before that request's headers, turning the entire thing into garbage.
If anyone knows why THAT behavior - the content of one terminated request being prepended onto a new active request - is happening feel free to answer the question so I can fully mark it solved.

PHP Javascript page loads twice

I have written this small site, with registration and everything, and I got to a point I think I am not too sure what is happening.
It first started as the DB reporting to me that the user I am trying to write into the DB has a duplicate entry (where it should be unique), which really puzzled me, how can it be that I have duplicate ? Well. It took me three days to realize that the page is being somehow called twice !
I put a
$_SESSION['one']=0;
and a
$_SESSION['two']=0;
in the topmost and bottommost parts of the page accordingly.
then I changed them both to ++, so I can check how many times they have been passed through.
I used the verification link from the emaill the site has sent, and tested their values.
Strangely enough, 'one' would equal 2 and 'two' would equal 1...
This explains exactly why all worked registration-wise, but I got all those errors about those duplicates.
Though, I used firebug to trace any redirections, but couldn't see anything...
It shows the page has 12 GETs and a POST.
I was hoping to bump into a redirect and debug accordingly, but alas, or maybe I don't know how to use firebug to trace these redirects...
I would appreciate any suggestion
Thanks in advance!
Like I said in the comments, here is the answer that worked for me and Ted:
What I do to fix it is change my doctype to html 5 and then validate the page using w3c validator, this problem was only occurring for my in firefox using firebug.
Do you have some <img> or <script> with src=""? Or maybe some <link> stylesheet with empty href?
Is firebug showing you, in net tab, that your site is called twice?
This kind of issues usually happen when you have a fatal or otherwise unignorable error that forces PHP to terminate you script early, in the midst of processing a request.
Check you error log for details of happening.
It is Firebug that is causing the page to load twice. Apparently changing the doctype to html5
as:
instead of html4 has overridden that bug.
When you are going to deploy your site, go back to html 4, and of course, always keep the server safe from such bugs. Use DB constraints, validation and escaping when needed.
Hope this helps, and Big thanks to #jeffreydev!!

How can I prevent content from unauthorized views (php/js)

How can I prevent (unauthorized) people from reading a message on a website (e.g. by looking in the browser cache for the text/images)?**
It's a PUBLIC (!) site (means: no logins here!)
But:
the (secret) message is only shown for a certain time.
the message might be shown only if a passwort is given.
Problems:
In Opera for example page(=page contents/text) could be indexed by the browser and searched.
One idea was to create an image with the message ... but: Also images - even when a header "no cache" is send could be retrieved from FireFox's cache.
Also: Recreating the message from single characters as image does not work (at least I think so at the moment). I tried this method, but it makes output quite slow (writing this: I notice that I do not need to create the images at runtime, but could create images (of single letters) in advance and display/refer to them not by real, but pseudo random names in the HTML )
I also had the idea to output a encoded message (ROT13) (in HTML) but use JS .onload to decode the message immediately. Problem: If this code is in the HTML it could be recovered from the cache later on. At least if someone searches through the (Opera) cache the person would probably not think of entering search terms encoded.
Programming language is PHP.
You can't. What if someone takes a screenshot of this?
You could add the secret code to the page with javascript, after the page is loaded. You'd want to retrieve the secret code via AJAX, then write it to the page - that way, the code isn't cached in the HTML part of the source, and it isn't sitting in the javascript within the page's source code.
Content piped in with AJAX is pretty ephemeral, it won't be cached or otherwise recorded.
Since I don't know anything about your HTML or what (if any) javascript framework you might be using, I can't give you a code sample, but you should be able to work with the concept.
Realistically if it is sent to the client and displayed on screen then you can not prevent the message from being saved or stored on the client machine. Whatever you do to prevent that save could still be bypassed by a simple screenshot.
If you are not concerned about the person the message is targeted at saving said message then I think your best course of action would be to use Flash with Flash doing a call to the server to retrieve the message and display it. Another option may be to use javascript to perform some form of call (AJAX) to the server which then sends back the message and you alter the DOM to display the message. I don't think that would be cached but unless you use SSL it could be stored by intermediate proxies.

Strange PHP output corruption

I'm having a very strange output corruption on one of my PHP sites. Sometimes, a piece of HTML code is displayed, rather than the tags being interpreted. It looks like some characters are missing, messing up the tags. See the example below: the second line should just be a link to c1, but due to some reason part of the target URL is shown.
alt text http://trappist.elis.ugent.be/~wheirman/atuin/tmp/phpstrangeness.png
The problem is temporary, a refresh usually solves it. This can happen on different parts of the page (although often in the same location). Only Safari seems affected (but I'm suspecting Firefox just masks the problem due to more tolerant parsing). It happens on both my development server as the live one, they both have slightly different settings (output buffering, chunked transfer), although the probability of it happening seems to vary.
Anyone ever seen something like this??
EDIT
When I "View Source" in Safari on this page, I get the following HTML:
<tr class="odd">
<td>73</td>
<td>c1</td>
<td></td>
<td><img src='/images/dot_blue.png' class="altTooltip" alt="inactive: no account" /> </td>
I can't see anything wrong with this, so either Safari has reloaded the page when I asked it for the source, or I'm not looking hard enough...
Well, here's my shot in the dark.
The break occurs in the word "office", after the fi character combination. I would bet that the fi ligature is -- somehow -- causing trouble.
How exactly? Since that HTML code doesn't contain the ligature character, this might be a bug in Safari. Especially since it occurs randomly. Could you try to rename that file, and see if the problem goes away?
Having valid HTML might also help in avoiding this problem, because it makes parsing easier.
When you select a piece of HTML and view source, what you get is not 100% what is there. For instance, all of you &'s are &, which probably means you selected the offending text and viewed the selection's source.
If you are still having the issue, trying viewing the whole source code without selecting anything, and then using ctrl + f to find the spot in the code, and try and give us a larger sample, not just the offending row, but a correct row, and in a larger context.
For instance, when using tables, a mislaid <td> can have very weird consequences, this doesn't look like that type of issue, I am just saying that we need context to be able to help.
There's also the issue that some browsers, in order to view source, actually resubmit the page and get a second copy of it. I have a feeling that it is the code that's outputting the text, so look and see if you are using something like
<?= $someVar ?>
and make sure it's not like this:
'?>> xxx
So, no selecty, and bigger sample please. And we'd prolly need something from the code that outputs the errored HTML...
I finally found the problem (using Web Inspector): TinyMCE has been inserting tags (which it uses to load language files and extension modules), at seemingly random places inside my own HTML. The result was that, in the case visible from my screenshot, something like bar.php">foobar.
Since I'm also using jQuery on the same page, my guess is it was ultimately cause by jQuery's modifications to the DOM and TinyMCE's additions happening at the same time which resulted in some sort of race condition (caused by a bug that only appears to exhibit itself in Safari).
I am now using the jQuery build of TinyMCE, and all has been well since...
Thanks to everyone for the help!

Categories