Strange PHP Issue

Hello all,

Got a really annoying bug somewhere in some code… that doesn’t really help narrow it down however.

Basically it seems as if my page is being “refreshed” so the code is being executed twice.

Example of my issue - Note: this is a very simplified example however still produces the same result.


class Person
    private $name;
    private $age;
    public function __construct($name, $age){

        $this->name = $name;
        $this->age = $age;


    public function Save(){

        $sql = "INSERT INTO people (`name`, `age`) VALUES('{$this->name}', '{$this->age}')";
        $conn = mysql_connect("****","*****","*****");
        mysql_select_db("helios", $conn);
        $result = mysql_query($sql);
        return $result;

$person = new Person("Steve", "24");


if i load this page. I will end up with two rows inside the database, if i pop an if statement around the instantiation of the Person like this…

    $person = new Person("Steve", "24");
    $_SESSION['stop'] = "test";

then load the page, i only have one row in the DB.

I cannot for the life of me find out where or what would be sending the page twice?i have tried on multiple PC’s with different browsers.

Im trying to find out of there is a way i can check the PHP server to see where these requests are coming from? maybe some hidding header(‘location’) ? or some html refresh tags.

Really annoying been debugging for a longtime and getting no where.

Any suggestions would be appreciated.


Perhaps this:

$result = mysql_query($sql); 
        return $result; 

When you declare $result it runs the query, returning it I would assume runs it again, however the connection should be closed, either way you should do a if mysql_query, return true, else return false, and see what that does.

It may not help you at all, but it’s something to look into.

    $person = new Person("Steve", "24");
    $_SESSION['stop'] = "test";

You’re running this code inside a loop. Look at what the code above does at the first and last line:

//If NOT SET then (1st loop not set, 2nd loop IS SET)
    //New person
    $person = new Person("Steve", "24");
   //Save person

   //SET session stop - which stops the second loop
    $_SESSION['stop'] = "test";

You’ve unwittingly put your code in a loop which is looping twice. It’s easily done even when you think you haven’t (I’ve done it numerous times).

Start echoing out your variables to find where your loop is.

Thanks, however, $result will only hold the return value of mysql_query(); effectivly doing the same as return mysql_query();


Thanks but again, i dont believe that’s true?

First page load:

if session is NOT set, then go ahead and save to that database & set session.
do nothing

on this load the session is set, so it goes to the else, which is do nothing.
Second page load:
if session is NOT set, then go ahead and save to that database & set session
do nothing

Anyway, that ‘if’ was added in order to prove my point, its like that page has been reloaded. So the cause is not by that if if statement on the session.

edit: i do not get a duplicate result when using that session and the if statement to check it, which proves thats not the issue which we knew already, but thank you for the response.


Does anyone else have any idea what it could be?

If you install the Live HTTP Headers add-on in Firefox you can keep track of all requests being made to the server. If you see two requests there is a redirect / reload somewhere, if you see just one request there is some kind of loop in your code.
At least then you know where to look :slight_smile:

I see what you’re saying and what the code is supposed to do but the simple point is this: Something is making that code run twice.

Do you have any images that are link to the script (eg <img src=“script.php?parameter=here”> type of thing?)

As I’ve said already you might think you’re not looping but i’ve done it several times also 100% that i had no loop anywhere.

Code doesn’t run twice by accident.

i’ve looked through and tried to find anything that could be amiss, but its not. I have no idea why im getting duplicate entries.
The site has very little java and certainly none that reloads the page.


Would appreciate any recommendations

Scallio has just made a good suggestion to you. You’ve not even acknowledged it but you’re quick enough to shoot down other suggestions and dismiss them.

Until you go forth with Scallios idea there isn’t much we can do to help further. We need to know which side is causing the code to run twice - Server or Client.

Have you tried echoing out variables in strategic positions as i’ve suggested?

Totally missed his post. Im gonna install it now have been afk eating.

Thanks Scall

It’s a good idea that he’s put forward. Just be sure to have no other http activity going on at the same time in the same browser - just your page. If you’re seeing more than one call to the same url then its a browser problem.

You might also want to look at HttpFox too which basically does the same thing but is slightly different.

I know what the issue is… but how to fix it :o

FF 4 is sending the request twice.

I uninstalled FF4, installed 3.6 and it worked. Reinstalled FF4 and it duplicated the row again. So install 3.6 again and it worked.

So something in my code doesn’t like FF 4? or there is a bug with FF 4 ?

Not sure what to do.
edit: tried in IE also and its fine. Only getting the problem in FF4. Also tried with FF4 on other computers and had some friends test with FF4 then in IE.

I’d be very surprised if it is FF4 because in your initial post you said this:

So how come now suddenly IE is suddenly working ok?

I don’t have FF4 so i can’t offer to try but perhaps you should post your url for others to try.

I can’t seem to duplicate the issue in anything else bar FF4. Previously it was probably me in a rushing around, as I was trying to juggling cooking and coding. So scratch that. I will contact ff4 support just in case.

However it does seem unlikely that it would be FF as its not like them to make a stupid mistake like that but anything is possible.

Until i can think of anything else its the only option i got.

Thanks for helps so far, i will let you know.