Can I redirect to another page when there is any parse error in PHP code . By setting error handler i can only redirect warning errors, notice errors but not parse errors . Please help me out. my code is:-
<?php
set_error_handler("error_handle");
function getIp(){
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
function error_handle($errno,$error_message,$error_file,$error_line,$error_context){
ob_start();
session_start();
include 'dbconnection.php';
echo $hash1=$_SESSION['Hash1'];
echo $hash3=$_SESSION['Hash3'];
$ip=getIp();
header('Location:error.php');
}
echo $test;
?>
PHP will not render code that has a parse error. Parse errors are very easy to catch by using the -l (lowercase L) option with PHP on the command-line:
$ php -l myfile.php
This will not run the code, but will simply report any parse errors.
EDIT: I do not recommend using code to detect parse errors, but just to prove Hobo wrong when he says "this can't be done", the following function (while not recommended) will redirect if a parse error is detected in an included file. Note that this solution requires that php-cli be installed on the server:
/**
* @param string $filename
* @return NULL
*/
function safeInclude($filename)
{
exec('php -l ' . $filename, $output, $parse_error);
if (!$parse_error)
{
include($filename);
}
else
{
// include another file or redirect using header()
}
}