处理403错误而不进行重定向

I wanted to handle 403 error without using server side error redirection methods(i.e. without using .htaccess file). Sometimes server returns a 403 error message (forbidden access). So is it possible to have PHP script which handles this 403 error message?

For example, Before showing error page, I would like to obtain server status when it my specific php page runs, and without making a redirection, I would just like to display custom message in the that page.

Some solutions for you.

  1. Check for URL errors and make sure the actual web page is specified. Its common reason for a web site to return the 403 Forbidden error, when the URL is pointing to a directory instead of a web page. Which can be done using HttpRequest Class in PHP. You can use http_get to perform GET request. You can also Test URL here.

    <?php
    $response = http_get("URL", array("timeout"=>1), $info);
    print_r($info);
    ?>
    

    Output:

    array (
       'effective_url' => 'URL',
       'response_code' => 403,
       .
       and so on
       )
    

    What is important for you is response_code with which you can play further.

  2. Use of curl.

    function http_response($url)
    { 
        $ch = curl_init(); 
        curl_setopt($ch, CURLOPT_URL, $url); 
        curl_setopt($ch, CURLOPT_HEADER, TRUE); 
        curl_setopt($ch, CURLOPT_NOBODY, TRUE); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
        $head = curl_exec($ch); 
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
        curl_close($ch); 
    
        if(!$head) 
        {  
         return FALSE; 
        } 
    
        return $httpCode;
    }
    
    $errorcode = http_response("URL");    //if success 200 otherwise different