juriaan79 Posted December 23, 2009 Share Posted December 23, 2009 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/186143-function-page-reloaded-after-header-redirect/ Share on other sites More sharing options...
ChemicalBliss Posted December 23, 2009 Share Posted December 23, 2009 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- Quote Link to comment https://forums.phpfreaks.com/topic/186143-function-page-reloaded-after-header-redirect/#findComment-983056 Share on other sites More sharing options...
laffin Posted December 23, 2009 Share Posted December 23, 2009 I would add more debug information to the error reporting read some about the debug_backtrace, this should provide more than enough information. Quote Link to comment https://forums.phpfreaks.com/topic/186143-function-page-reloaded-after-header-redirect/#findComment-983064 Share on other sites More sharing options...
juriaan79 Posted December 23, 2009 Author Share Posted December 23, 2009 @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? Quote Link to comment https://forums.phpfreaks.com/topic/186143-function-page-reloaded-after-header-redirect/#findComment-983353 Share on other sites More sharing options...
laffin Posted December 23, 2009 Share Posted December 23, 2009 Check yer apache log files, if its getting refreshed, it will show the entry twice, its probably the browser. you may also want to check yer php log files. Quote Link to comment https://forums.phpfreaks.com/topic/186143-function-page-reloaded-after-header-redirect/#findComment-983365 Share on other sites More sharing options...
juriaan79 Posted December 24, 2009 Author Share Posted December 24, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/186143-function-page-reloaded-after-header-redirect/#findComment-983368 Share on other sites More sharing options...
juriaan79 Posted December 24, 2009 Author Share Posted December 24, 2009 @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? Quote Link to comment https://forums.phpfreaks.com/topic/186143-function-page-reloaded-after-header-redirect/#findComment-983373 Share on other sites More sharing options...
laffin Posted December 24, 2009 Share Posted December 24, 2009 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(); } Quote Link to comment https://forums.phpfreaks.com/topic/186143-function-page-reloaded-after-header-redirect/#findComment-983463 Share on other sites More sharing options...
juriaan79 Posted December 24, 2009 Author Share Posted December 24, 2009 Hey this is now working fine. It's still not a very satisfying solution, but for now it's working. Adjusting the way the header is redirecting with cache control etc won't work I assume? Quote Link to comment https://forums.phpfreaks.com/topic/186143-function-page-reloaded-after-header-redirect/#findComment-983560 Share on other sites More sharing options...
juriaan79 Posted January 4, 2010 Author Share Posted January 4, 2010 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... Quote Link to comment https://forums.phpfreaks.com/topic/186143-function-page-reloaded-after-header-redirect/#findComment-988039 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.