I notice that when I exit() a script, (or die() a script, for that matter), it forces the page to stop loading. Is there any workaround for this?
Stopping the script without forcing the page to quit loading
| Marston wrote: |
| I notice that when I exit() a script, (or die() a script, for that matter), it forces the page to stop loading. Is there any workaround for this? |
Instead of
| Code: |
| <?php
if ($input == '') { exit('No input. Try again.'); } echo 'rest of script</body></html>'; ?> |
| Code: |
| <?php
$exit_msg = false; if ($input == '') { $exit_msg = 'No input. Try again'; } echo 'rest of script'; if ($exit_msg !== false) { echo $exit_msg; } echo '</body></html>'; ?> |
I'm not sure if that would work though, because the script is like a registration script, and I need the script to terminate in the case of an error, but I would like the rest of the page to load.
Like, if you forget to fill something in and hit "submit", I need the script to terminate, but I also need the rest of the page to load. Exit and Die just stop the page in its tracks.
Like, if you forget to fill something in and hit "submit", I need the script to terminate, but I also need the rest of the page to load. Exit and Die just stop the page in its tracks.
Well, I guess I figured out a work around - I'll just use JavaScript to redirect to an error page. Not the greatest, but I can work on it later
. Thanks for the help anyways, hexkid.
Well my registration script will first validate all info. Then it sees if any validations threw an error, if they did, it skips the registration and displays the error. If there are no problems it does the sql query to add the user and tells them it worked.
| Marston wrote: |
| Well, I guess I figured out a work around - I'll just use JavaScript to redirect to an error page. Not the greatest, but I can work on it later |
Just be careful when using JavaScript for important things like page redirection - some browsers do not support it and some people may have it disabled, thus meaning that your website will not function properly to those people.
| ChrisCh wrote: | ||
Just be careful when using JavaScript for important things like page redirection - some browsers do not support it and some people may have it disabled, thus meaning that your website will not function properly to those people. |
lol thats nice 
I would recomend to use a function for your registration subprocess. In this case you can cancel the whole process by calling a return(false) or whatever.
I would also recommend to use ob_start() at the beginning of your script and ob_end_flush() at the end. This way you output is buffered and sent at the end of processing which will result in a cleaner behavior in some cases, and will further help you to handle errors properly.
I would also recommend to use ob_start() at the beginning of your script and ob_end_flush() at the end. This way you output is buffered and sent at the end of processing which will result in a cleaner behavior in some cases, and will further help you to handle errors properly.
| DanielXP wrote: |
| lol thats nice |
It is possible to call another function after exit() has been called, you can set it using register_shutdown_function(). Have a look here;
http://uk.php.net/manual/en/function.register-shutdown-function.php
So you could define a function which outputs the remainder of a page, presumably the ends of </div>s and footer stuff.
However, if you don't know where your script is going to end exactly when outputting the HTML then this becomes very difficult to get right.
It is more suitable for getting rid of cookies and clearing variable clutter after an error really.

http://uk.php.net/manual/en/function.register-shutdown-function.php
So you could define a function which outputs the remainder of a page, presumably the ends of </div>s and footer stuff.
However, if you don't know where your script is going to end exactly when outputting the HTML then this becomes very difficult to get right.
It is more suitable for getting rid of cookies and clearing variable clutter after an error really.
