Zend: 500 error instead of 404 - php

Problem with 404 errors in Zend Framework 1. http://www.url.nl/path/wrong returns a 404 error (because the /path part exists). But when the first path is wrong (the root), it returns a 500 error. So http://url.nl/wrong returns a 500. Anybody who experienced the same issue? This is my error handler:
switch ($errors->type) {
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
// 404 error -- controller or action not found
$this->getResponse()->setHttpResponseCode(404);
$this->view->message = 'Page not found '.$role;
if (!$role) $this->_helper->layout->setLayout ( 'pages/404' );
else $this->_helper->layout->setLayout ( '404' );
break;
default:
// application error
$this->getResponse()->setHttpResponseCode(500);
$this->view->message = 'Application error';
break;
}
if ($errors->exception instanceof Zend_Acl_Exception) {
// send needed headers...
// prepare log message...
// render info: resource_not_found.phtml
$this->_helper->viewRenderer('error-no-access');
$this->_helper->layout->setLayout ( '403' );
}
EDIT:
The ACL manager was the problem, contained some problems in redirects etc. Most of the pages where redirected to the back-end and restricted because a normal user was trying to reach the page.

/*if (!$role) $this->_helper->layout->setLayout ( 'pages/404' );
else $this->_helper->layout->setLayout ( '404' );
break;
default:
// application error
$this->getResponse()->setHttpResponseCode(500);
$this->view->message = 'Application error';
break;*/
Try commenting the above section (like I did). This will tell you the actual problem at your file location(at your url). Once you know the problem, you can solve it as well.
Hope it helps.

Related

Redirect Query - Header not redirecting with switch

I'm trying to redirect my links, using a php file but when I click the links nothing happens or I get a 500 error.
I've tried redirecting websites such as https://test1.org, https://test2.org and https://test3.org using a php file with switch.
<?php
$page = $_GET['page'];
switch($_GET['page']) {
case '1':
header('https://test1.org');
break;
case '2':
header('https://test2.org');
break;
case '3':
header('https://test3.org');
break;
default:
echo "Hello";
}
Anyone know whats wrong?
I'm wanting it to redirect when someone clicks the link, example redirect.php?page=1, redirect.php?page=2 or redirect.php?page=3...etc
Use "Location:" in your header() function call to redirect the page to the specified URL, like so:
header('Location: https://test1.org');

PHP error notices. Should they never be ignored?

I'm researching how to better organize my website content with PHP, and I had a question regarding unimportant error notices.
<?php
switch ($_GET['filename']) {
case 'home':
require('src/home.php');
break;
case 'quiz';
require('src/quiz.php');
break;
default:
if ($_GET['filename'] == '') {
include('src/home.php');
} else {
header('HTTP/1.0 404 Not Found');
include('src/page_not_found.php');
}
break;
}
?>
For example here; it's obviously telling me that it's getting undefined when I try to get the filename in the URL parameter. In this context, it's empty, and I'm doing this on purpose to check if there's something in there and if it should be interpreted as one of my other files.
I'm aware that you can add "error_reporting(E_ERROR | E_PARSE);" at the start of the line to hide the notice and the website will work just fine like that, but I was wondering if this is something I should always "fix"?
I was thinking of doing an if condition before the switch case:
if ($_GET['filename'] == ""){
include('src/home.php');
}
But that will throw me a notice as well, since what I am checking is undefined and will trigger the error notice regardless. What should I do?
Tim Lewis answered my question, thank you by the way!; Instead of hoping that a file is there, you can instead use isset().
So, instead of what I made, I would do something like this, to first check if the content is set before doing anything else:
<?php
if (!isset($_GET['filename'])){
include('src/home.php');
} else {
switch ($_GET['filename']) {
case 'home':
require('src/home.php');
break;
case 'quiz';
require('src/quiz.php');
break;
default:
header('HTTP/1.0 404 Not Found');
include('src/page_not_found.php');
break;
}
}
?>

How to tell the browser to display his default error page?

In order to include the right file and display an error page if an error occurs, I have the following code (very simplified) :
$page = 'examplePage.php';
$page404 = '404.php';
if (file_exists($page))
{
require($page);
}
else if (file_exists($page404))
{
require($page404);
}
else
{
// Tell the browser to display his default page
}
?>
To summarize :
If I have the file, I include it.
If I don't have the file, i include the error file.
What if the error file does not exist too ?
I would like it to be rendered as the default error page of the browser.
I already achieved this with Internet Explorer by sending an empty content with the HTTP error.
The problem is that the other browsers don't act the same, they all display a blank page.
Is there any way to tell browsers to display their own error page ? (not only 404, but all errors : 304, 500 etc)
Thank you.
Edit : I forgot to tell you that I have the complete control on the headers I send and on the content sent in response.
Edit 2 : here is some code
// possible paths to retrieve the file
$possiblePaths = array(
$urlPath,
D_ROOT.$urlPath,
D_PAGES.$urlPath.'.php',
D_PAGES.$urlPath.'/index.php',
$urlPath.'.php'
);
foreach ($possiblePaths as $possiblePath)
if (file_exists($possiblePath) && !is_dir($possiblePath))
{
if (!is_readable($possiblePath))
{
Response::setCode(403); // calls the header(403)
self::$filePath = self::getErrorPage(403);
}
else
self::$filePath = $possiblePath;
break;
}
if (self::$filePath === null) // no file found => 404
{
Response::setCode(404); // call the header(404)
self::$filePath = self::getErrorPage(404);
}
public static function _getErrorPage($code)
{
if (is_readable(D_ERRORS.$code.'.php')) // D_ERRORS is the error directory, it contains files like 404.php, 403.php etc
return D_ERRORS.$code.'.php';
else
{
/*-------------------------------------------------*/
/* Here i go if the error file is not found either */
/*-------------------------------------------------*/
if ($code >= 400)
Response::$dieResponse = true; // removes all output, only leaves the http header
return null;
}
}
?>
And here is when I print the content :
<?php
if (self::$dieResponse)
{
self::$headers = array(); // no more headers
self::$content = ''; // no more response
}
http_response_code(self::$code); // HTTP code
foreach (self::$headers as $key => $value)
header($key.': '.implode(';', $value)); // sends all headers
echo self::$content;
?>
Edit : here are some screenshots to explain what I want.
This is what i've got in IE :
This is exactly what i want.
Now, in all the other browsers, I've got a blank page. I don't want a blank page.
I want, for example, Chrome to display this :
Default error pages
Web Browsers shows default error pages if content is blank, eg. create a empty PHP file (error.php) and put this:
<?php
$status = http_response_code();
switch ($status) {
case 404:
case 500:
exit;//terminate script execution
break;
...
}
In .htaccess put:
ErrorDocument 400 /error.php
ErrorDocument 500 /error.php
Custom error pages
Using HTTP status
You can use http_response_code() for GET current HTTP status, .htaccess file content:
ErrorDocument 400 /error.php
ErrorDocument 401 /error.php
ErrorDocument 403 /error.php
ErrorDocument 404 /error.php
ErrorDocument 500 /error.php
ErrorDocument 503 /error.php
Page error.php:
<?php
$status = http_response_code();
switch ($status) {
case '400':
echo 'Custom error 400';
break;
case '404':
echo 'Custom error 404';
break;
...
}
Using GET param
ErrorDocument 400 /error.php?status=400
ErrorDocument 401 /error.php?status=401
ErrorDocument 403 /error.php?status=403
ErrorDocument 404 /error.php?status=404
ErrorDocument 500 /error.php?status=500
ErrorDocument 503 /error.php?status=503
Page error.php:
<?php
$status = empty($_GET['status']) ? NULL : $_GET['status'];
switch ($status) {
case '400':
echo 'Custom error 400';
break;
case '404':
echo 'Custom error 404';
break;
...
}
Related: How to enable mod_rewrite for Apache 2.2
If you need to have it display its default 404 page, before any output, do this:
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
See here: http://www.php.net/manual/en/function.header.php
So, for your code, you could modify it to:
$page = 'examplePage.php';
$page404 = '404.php';
if (file_exists($page))
{
require($page);
}
else if (file_exists($page404))
{
require($page404);
}
else
{
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
}
?>
Note the following warning that header stuff has to be done before any other output:
Remember that header() must be called before any actual output is
sent, either by normal HTML tags, blank lines in a file, or from PHP.
It is a very common error to read code with include, or require,
functions, or another file access function, and have spaces or empty
lines that are output before header() is called. The same problem
exists when using a single PHP/HTML file.
I asked similar question before a while ago
Access apache errordocument directive from PHP
Upshot was either redirect the user to a generic 404 page (so the address changes) Header("Location: $uri_404"); or curl your own 404 page and echo it, like so:
Header('Status: 404 Not Found');
$uri_404 = 'http://'
. $_SERVER['HTTP_HOST']
. ($_SERVER['HTTP_PORT'] ? (':' . $_SERVER['HTTP_PORT']) : '')
. '/was-nowhere-to-be-seen';
$curl_req = curl_init($uri);
curl_setopt($curl_req, CURLOPT_MUTE, true);
$body = curl_exec($curl_req);
print $body;
curl_close($curl_req);
Code credit to #RoUS
Maybe you could try:
header('HTTP/1.0 404 Not Found');

403 error on passing parameters

I have following function in a php file :
function bc_output($vars) {
switch($_GET['a']){
case 'addip':
bc_add_licenses($vars, trim($_GET['current_ip']));
break;
case 'addiprequest':
if(isset($_POST['submitaddip'])) {
display_guidance_text();
bc_add_licenses_request($vars, $_POST);
} else {
display_guidance_text();
bc_get_licenses($vars, '4');
}
break;
default:
display_guidance_text();
bc_get_licenses($vars);
break;
}
}
Now when i go to url such as http://website.com/addon.php?module=b&a=addip it works fine, although for second case i.e http://website.com/addon.php?module=b&a=addiprequest browser throws 403 error..
Can anyone guide me where to proceed from here, i have echoed etc in the function but always 403 error.
Please comment if you need more info.

Zend 301 Redirects - some mysteriously not working?

I have a problem whereby some redirects are not working and some are. I am using a crude way of doing a redirect, which is catching the url in the errorAction of the ErrorController and then having a few if statements and then doing the redirects as follows:
public function errorAction()
{
$request = basename($this->getRequest()->getRequestUri());
$this->getHelper('redirector')->setCode(301);
if ($request == 'rand.html')
$this->_redirect('/services/currency/rand');
if ($request == 'dollar.html')
$this->_redirect('/services/currency/dollar');
if ($request == 'zim-dollar-currency.html')
$this->_redirect('/services/currency/zim');
//About 20 of these If statements
$errors = $this->_getParam('error_handler');
//load appearance stuff
if (!$errors) {
$this->view->message = 'You have reached the error page';
return;
}
switch ($errors->type) {
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
// 404 error -- controller or action not found
$this->getResponse()->setHttpResponseCode(404);
$priority = Zend_Log::NOTICE;
$this->view->message = 'Page not found';
$this->view->headTitle()->prepend('Page Not Found');
break;
default:
// application error
$this->getResponse()->setHttpResponseCode(500);
$priority = Zend_Log::CRIT;
$this->view->message = 'Application error';
$this->view->headTitle()->prepend('Application error');
break;
}
$this->view->request = $errors->request;
}
So, some will work and some won't, like: zim-dollar-currency...
Not sure why this is. I have even checked things like windows line endings etc. Nothing...
Try use switch construction with default action

Categories