My web server is acting wierd. It seems like it executes scripts (PHP) twice before sending then to apache.
I've run this file:
<?php
echo '<pre>';
session_start();
$_SESSION['index']++;
echo '<br>';
print_r($_SESSION);
?>
And I know that ++ will give a notice at first, but it's just to investigate if it runs twice. Anyway, the printed session shows that the index-index increases by two each time you load the page.
The webserver is apache2, php5 installed on a debian unit.
Anyone has any ideas?
echo '<pre>'; //Headers and cookies already sent.
session_start(); //Cannot set cookie anymore, system tries again, I guess
Start session first, then output anything.
Try placing session_start(); on top
I can't thank the poster of this question enough. His session test made me realize that I had the same problem of a php script running several times.
In my script I had two PDO functions seperated from each other by an if-else-construct. One was a simple select, and one a simple insert function. But everytime I ran the script, both pdo commands got executed. PDO ended up writing rows in my table which had the entry 'public'.
So what happened? My page got send multiple times because of ELEMENTS IN THE HTML CODE THAT COULD NOT BE FOUND. In my case that was a css file which was named incorrectly. When I solved that (after 4 hours of looking at code) the problem disappeared. Also broken images for example trigger the same event.
Oke folks, found a completely insane solution to this problem. Just posting for future reference. I recently installed a HTML validator in Chrome (an extension). This seems to be the culprit. After everything has loaded, the validator seems to be re-requesting the page so it is executed twice.
Nice plugin. Not!
Took me about half a day to figure this out.
I am seeing the same behaviour... a $_POST would be present the first time the page ran, then wouldn't the second time... scoured the code to find why the page might be posting back to itself again. No avail.
After seeing user1601869's answer above, I started checking. I had some links to stylesheets I hadn't written yet, so put skeletons of those in.
It turns out that for me, the culprit was:
<link rel="shortcut icon" href="">
This was just a placeholder for an icon that was causing my page to break. I suggest that if you have the same problem, look for links in the <HEAD></HEAD> that are broken!
Related
OK, so on some of the pages on my site, I've included a foot.php file at the end so that when I make changes to it, it effects all pages on the site. On most pages, this works perfectly, but on some pages, it just cuts off, and no includes after it take effect. The weird thing is that it includes only a portion of the file on pages where this happens.
I thought it might be because of the number of includes I used, but I have pages with more that work just fine. Take for instance, this one:
http://www.kelvinshadewing.net/codeSquirrel5.php
Here, you can see the bottom gets cut off, and if you view the source code, the rest of what goes in that div is gone, yet the div itself is closed off properly. But then go here:
http://www.kelvinshadewing.net/sprTartii.php
You'll see that the full code is there, and the Disqus app is present as well. This issue has been going on since before I added Disqus, and also happened when I'd been using includes in a different way to generate global content, so it's something about those pages in particular. It does it with only my Squirrel tutorials, and nothing else. I'm totally stumped and have no idea what's causing this. I've gone over my code a dozen times, and verified that every page uses the same PHP scripts.
As for the scripts themselves, it's just this:
<?php include "foot.php";
include "disqus.php"; ?>
The problem just disappeared, so I'm ruling it as a server glitch. If anyone else is reading this, I suggest checking out Andrew's comment, because that code was nice to know.
I have a very strange problem and i don't know what to do about it. My site seems to work just fine all browsers other than internet explorer, so i've been trying to figure out why.
I've narrowed it down to the a file that I'm including in my site, this file is a php class that has a number of different functions like login getters and setters and so on.
I took all the php code out of my pages and it renders fine, so i added the php back in line by line and released that it stopped working when i used this:
require_once 'classes/Membership2.php';
Does anyone know why some php code will be messing with the style of my website.
For more detail on the matter, i have a number of divs that are centered, they all have curved edges as well as shadows. So by taking away the php i can see that IE is loading the page properly, no incompatibilities or anything like that.
Has anyone had a problem like this before? While i'm waiting for an aswesome or two i'll be removing functions and part of the code till i can narrow it down. (I would give code, but the file has a lot of lines of code.)
Thanks for the help.
Oh yeah and I'm testing on Internet Explorer 9 and every other browser is the latest version or close enough.
Okay so i've done some more digging into this, i've found that if i delete all the code in the class (All the functions) and leave just and empty class in the include file it still doesn't work. Okay, so in my view that means the functions aren't whats making this problem. So i deleted EVERYTHING, so now the include points to a blank php file. This worked and the page rendered as it should but obviously there is no functionality, i can't login or anything like that. I decided to add a constructor instead of leaving it as default, this function does nothing but return true; and it made the site mess up again.
Does this info change anything? Also i'm reiterating the fact that i do not get this error or any other browser but Internet Explorer 9 (Haven't tried any other IE version).
Thanks again for the help.
Okay, so i've solved the problem. At the start of my PHP class i have used
<!-- blah blah blah -->
forgetting that there is only PHP in this document and no HTML. So when i include the file it just outputs that into my source code and and messes things up, should have used the PHP commenting style.
Still strange that EVERY browser other than IE just ignores this and goes about its business, even the site that #blankabout suggested didn't give me any error (Although i assure thats because its part of the included PHP file and not the HTML itself).
as #fajran says to you, save both outputs with "view source code" on the browser and compare them to find the diference. To compare outputs use winmerge or similar tool. Once you now which text it generating the trouble, modify it inside the include file.
Given that your php, because it runs on the server, should never actually reach the browser, it may very well be some unterminated HTML or similar that is causing the problem. Perhaps the PHP is causing a break in the HTML that is unexpected.
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!!
EDIT: This question is solved, but I can't accept my own answer just yet.
Howdy!
So I've got a PHP/CodeIgniter webapp. I've got some custom Ajax-based tracking code inserted into my page - however obviously, for testing purposes, I don't want this code to fire when I'm running the site locally on my development computer.
In order to avoid Analytics still tracking, I'm using this in my source code:
<?php if ($_SERVER['HTTP_HOST'] != 'localhost:8888'){ ?>
_gaq.push(['_trackPageview', '/my/ajax/page']);
<?php } ?>
That does the job fine in the source code - when I'm on http://localhost:8888 (MAMP's URL) it doesn't appear. When I'm running live, it appears. Woot.
However I've found some seemingly infinite error. When you click a link that fires an Ajax event, the Firebug log repeatedly fills up and up and up with errors, if you leave it a couple of minutes it gets into the thousands (I guess it's an infinite loop). The error each time is:
_gaq is not defined
Then it references it being on localhost:8888 (line 26). Line 26 reads:
<!-- Includes for Uniform JS -->
Just a comment... I've removed that (and the subsequent Uniform JS script links beneath), same error, just goes to the next possible line. So I thought it might be the script above causing it the problem, that was Typekit. Commented out Typekit, still the same issue. Ouch.
Anybody know what's going wrong here?
*Could I just forget the PHP blocking bit and can I block my localhost in Google Analytics? The problem is I'm on a dynamic IP so changing Analytics settings every time my computer gets switched on will be a pain in the bum *.
Thanks!
Jack
EDIT: I should make it clear that EVERY reference to Google Analytics is put between the <?php ?> blocks, including the declaration at the foot of the page. So it's not like there's some Analytics JS appearing and not the rest.
Problem solved. Thanks to everyone who read this question and considered answers for me.
It was a pretty easy one:
<?php if ($_SERVER['HTTP_HOST'] == 'localhost:8888') { ?>
Turns out I'd written == by mistake, instead of != on one occurrence of the PHP block!
Oops, Thanks!
To start: I am a C++ developer who is roped into making a PHP script (for paypal IPN).
I have been incredibly frustrated witht he lack of good working samples from paypal and elsewhere.
The latest problem is that I seem to not get any of the $HTTP_POST_VARS items that I think I should be getting.
Some searches online seem to indicate that this is either deprecated or configurable, etc.
I have no idea what version of PHP is used at my host.
It seems clear that either my testing applications do not post correctly or the script is not working.
so: 2 questions:
- Does anyone have any links to working IPN scripts?
- What gives with the $HTTP_POST_VARS nonsense?
EDIT
thanks all. I'll try these suggestions out and post up my success story soon I hope.
You can check what version of PHP you are using by typing phpinfo(); into a PHP script block <?php ?> and see what it ouputs (or simply echo PHP_VERSION).
$HTTP_POST_VARS is the old way of doing things. You can use $_POST['post-var']. To examine everything beint posted, use print_r($_POST).
First thing to try is changing $HTTP_POST_VARS to $_POST. That's the new mechanism, and after some version or another $HTTP_POST_VARS stopped being a superglobal.
As chaos already wrote, just use the $_POST array instead of $HTTP_POST_VAR.
Two things i like to mention:
1. var_dump(somevar) function is very helpful in php. It displays structured information about somevar. If you not sure how is some variable or array or what ever is structured, just use this function. So this call var_dump($_POST); will display you all the current POST parameters.
2. phpinfo() function is helpful if you are interested which version and extensions is your host using. Just create a file with <?php phpinfo(); ?> in it and navigate with the browser to this file. Don't forget to remove it after this, because of the security leak.