Jump to content

Recommended Posts

If an error occurs in my script, e.g. caused by a query, then my error handling function reportError is called. Within this function a record is stored in the database regarding information concerning the error and the system administrator is sent an email with details on the error. After this, the user is redirected to an error page where he/she receives some info on what went wrong.

This is all working fine, the only problem being that it seems as if the function is called / executed twice. Both the INSERT query and the email is performed / sent twice. When I halt the script with an exit after these actions then there's only query executed and one email sent. Also if I remove the header redirect at the end of the reportError function, there's also only one record stored in the DB and one email sent. I guess this must be happening because of the header redirect, does anyone have a clue of what's going wrong?

 

Here's the code:

 

<?php

$result = mysql_query($query, $_my_db) or reportError(SYSTEM_ERR_DB_SELECT_FAIL, $query, mysql_error($_my_db));

 

function reportError($errCode, $query, $errMsg)

{

    INSERT error record in error table

    Send email to admin about error

 

    // Error is processed redirect user to dedicated error page with information for the user

    header ('Location: '.URL_BASE.'/error/'.$errCode);

    exit;

}

?>

Sounds as if the page your redirecting also has the same error or something.

 

Echo the Redirect string, make sure it points to the right file.

Make sure that file is not causing the problem, empty it (backup first), so u get a blank page on redirect, if u get 1 email then it's the file.

 

-CB-

@Laffin: I'm only showing a subset of the data that is being stored, but thank you for the tip I wasn't aware of that type of functionality!

 

It's still not working, the problem is not in the script itself. If I put the INSERT query on any of my webpages and perform a header redirect as displayed above, then for some reason the query gets executed twice. It's as if the page is refreshed without me noticing. I'm completely in the dark here. Does anyone have a clue?

Ok, I'll check the logs, I dont think it's a refresh that's causing the problem though because if I setup the code like this:

if(!isset($_SESSION['a']))

{

  INSERT query here

  $_SESSION['a'] = 1;

}

 

Then you would expect that even on a refresh of the page the INSERT query only gets executed once. If I enter the URL of the file that's inserting the record in the error table in the browser the script is executed without any problems and the record is only inserted once. So it really is the header redirect that is causing these problems. I'll look for the logs.

@Laffin,

In the current setup I made things a bit more simple. I have a file seterror (with /4 which is the error code that was parsed to seterror) from where the redirecting is taking place. In another file test1.php (where seterror is redirecting to) I added the insert query. If I execute seterror then test1.php is redirected to and executing twice (two records are inserted). From the logfiles I got the following info:

 

127.0.0.1 - - [24/Dec/2009:01:11:46 +0100] "GET /include/system/seterror/4 HTTP/1.1" 302 -

127.0.0.1 - - [24/Dec/2009:01:11:46 +0100] "GET /include/system/test1.php HTTP/1.1" 200 -

127.0.0.1 - - [24/Dec/2009:01:11:47 +0100] "GET /include/system/seterror/4 HTTP/1.1" 302 -

127.0.0.1 - - [24/Dec/2009:01:11:47 +0100] "GET /include/system/test1.php HTTP/1.1" 200 -

 

Apparently after loading test1.php has finished the initial script from where redirecting was initiated is called a second time and kind of bouncing back to the test1.php file again where the query is executed for the second time. But why is this happening, I thought that if you do a header redirect the page is redirected permanently. Do you have any idea what is causing the problem?

The 302 status code - page has moved redirect.

Which browser tries to handle, so this does sound like a browser issue.

You may consider using your own redirect method

 

function redirect($url,$timeout=1)
{
   if(empty($url)) $url=URL_BASE;
   echo "<meta http-equiv="refresh" content=\"{$timeout};{$url}\" />";
  die();
}

  • 2 weeks later...

OK, so recently I ran into the same problem and as it turns out the scripting was not the problem (the above privided solution was a workaround though) but having YSlow as an add-on in Firefox seemed to cause unpredictable page reloads by the server. After inactivating YSlow as an add-on I did not experience any problems anymore...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.