Friday, 4 October 2013

Fatal error: Call to undefined function http_redirect() : Fixed

Issue: When I was trying to call the PHP http_redirect() function, below error came up:

Fatal error: Call to undefined function http_redirect() in location.


Add the below function in your php page

/**
 * redirect to a specific URL
 * @param $url
 */
     
function redirect($url)
{
     if (!
headers_sent())
     {  
          
//If headers not sent yet... then do php redirect
          
header('Location: '.$url); exit;
     }
     else
     {                  
          
//If headers are sent... do javascript redirect...
          //if javascript disabled, do html redirect.
          
echo '<script type="text/javascript">';
          echo 
'window.location.href="'.$url.'";';
          echo 
'</script>';
          echo 
'<noscript>';
          echo 
'<meta http-equiv="refresh" content="0;url='.$url.'" />';
          echo 
'</noscript>'; exit;
     }
}  


then call the above function  to the page you wish the redirect to happen on

eg: redirect("redirect.php");