Jump to content

PHP compiling error redirect


joe92

Recommended Posts

Hi there,

 

Does anybody know of a way to make php execute a redirect if there was a compiling error? Specifically for the means of redirecting during file uploads. Most likely to a script which would wait 5 seconds with a nice error message and redirect back.

 

I had a look at set_error_handler but it specifically says:

If errors occur before the script is executed (e.g. on file uploads) the custom error handler cannot be called since it is not registered at that time.

 

So that's a no go it seems.

 

If there is no way to do this with PHP, is there an FTP program that will upload the file you want with a ghost name during transfer, and then rename it and overwrite a conflicting file once the file has been 100% uploaded? That would also get around the problem I think.

 

Thanks for any help,

Joe

Link to comment
Share on other sites

A function from php.net towards the bottom of the set_error_handler documentation that detects PHP fatal errors:

 

function shutdown(){
        $isError = false;
        if ($error = error_get_last()){
            switch($error['type']){
                case E_ERROR:
                case E_CORE_ERROR:
                case E_COMPILE_ERROR:
                case E_USER_ERROR:
                    $isError = true;
                    break;
            }
        }

        if ($isError){
            echo "Script execution halted ({$error['message']})";
        } else {
            echo "Script completed";
        }
    }

 

this checks for several different fatal errors. You can adjust to your needs.

Link to comment
Share on other sites

Thanks for finding that. I've altered it slightly to suit my needs, and only redirect if I'm not in a test environment. Thought I'd post it here since it's actually quite handy. It captures the type of error as a number and redirects to an error script sending type in the GET. In the error php that number is then retrieved and placed into a table in the mysql database along with a time stamp. If there's a new one when I log in, then I am notified of it:

 

<?php
//Report all errors
error_reporting(E_ALL);

/** Define the shutdown error handling to redirect a user to a nice page if a fatal error is detected **/
function shutdown()
{
	$sandboxing = false;
	//check if in test environment
	if(strstr($_SERVER['HTTP_HOST'],'127.0.0.1'))
		{
			$sandboxing = true;
		}
	$isError = false;
	if ($error = error_get_last())
		{
			switch($error['type'])
				{
					//http://www.php.net/manual/en/errorfunc.constants.php
					//parsing errors
					case E_PARSE:
					$isError = true;
					$errNum  = 1;
					break;

					//fatal errors
					case E_ERROR:
					$isError = true;
					$errNum  = 2;
					break;

					//core php fatal erros
					case E_CORE_ERROR:
					$isError = true;
					$errNum  = 3;
					break;

					//compiling errors
					case E_COMPILE_ERROR:
					$isError = true;
					$errNum  = 4;
					break;

					//a user generated error, when you call trigger_error();
					case E_USER_ERROR:
					$isError = true;
					$errNum  = 5;
					break;
				}
		}
	//error detected, redirect user to error page, but only on actual site
	if($isError == true && $sandboxing == false)
		{
			header("location:http://www.mysite.com/error.php?$errNum");
			exit();
		}
}

//make sure the function is called upon reciept of an error
set_error_handler('shutdown');
register_shutdown_function('shutdown');

 

This means I can define errors anywhere in the script, and be told if and when they happen. I like this feature. Might likely add in further details like location...

 

Cheers,

Joe

Link to comment
Share on other sites

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.