8

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?

11
  • What happens if you hit it with something else, such as wget? Commented Feb 19, 2013 at 20:43
  • 1
    Do you have any apache rewrite, and 404? Check your requests to server. Commented Feb 19, 2013 at 20:44
  • 1
    Is that all you are running or is it included in something else. Commented Feb 19, 2013 at 20:44
  • Nop. Increases just one at a time! Commented Feb 19, 2013 at 20:45
  • 1
    @regilero only 1 req with wget Commented Feb 19, 2013 at 20:59

4 Answers 4

6
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

Sign up to request clarification or add additional context in comments.

5 Comments

Do all your scripts get executed twice? Or just this?
Well, first I tried with some other code, then saw that it behaived wierd, so I created this script to test things.
Did you try some really simple code without headers and session_start() ?
Well, how do I test it runs twice then?
I had the issue and the culprit was <pre> right after mysql query run making the script containing the query running twice. I deleted all <pre> and print_r() from the script until it reaches <html> and it stopped executing twice. I could do this or use ob_start(). Thank you!
3

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.

1 Comment

This solution saved me tons of hours, +1, thank you!
2

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.

1 Comment

Had the same problem.... Google Chrome showed 404's on css-files. Solved those but still script gets executed twice.
1

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!

1 Comment

Wow! Was having the same problem and this gave me the important clue. It wasn't an icon, but I had a style sheet that wasn't properly loading causing the same kind of issue. thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.