Returning an HTTP error
Create site9 by copying site8.
- /cms
- ...
- site8
- site9
In this chapter, we are going to program HTTP error responses.
To test the result online, enter http://www.frasq.org/cms/site9 in the address bar of your navigator. Try an invalid URL in English and another one in French to display the error pages Not Found and Non trouvé.
Create the folder error in the folder actions then the file notfound.php in the folder actions/error with the following content:
- /cms/site9
- actions
- error
- notfound.php
- error
- actions
- function notfound($lang) {
- head('title', translate('http_not_found:title', $lang));
- head('robots', 'noindex, nofollow');
- $contact=true;
- $banner = build('banner', $lang, compact('contact'));
- $contact_page=url('contact', $lang);
- $content = view('error/notfound', $lang, compact('contact_page'));
- $output = layout('standard', compact('banner', 'content'));
- header('HTTP/1.1 404 Not Found');
- return $output;
- }
The action notfound
returns the error HTTP/1.1 404 Not Found
followed by a document which displays an error message with a link to the contact page.
Add the title of the page to the file includes/strings.inc.
In English in the array 'en':
- 'http_not_found:title' => 'Not Found',
In French in the array 'fr':
- 'http_not_found:title' => 'Non trouvé',
Create a folder error in the folders views/en and views/fr. Add the view of the error page in the folders views/en/error for the English version and views/fr/error for the version in French.
- /cms/site9
- views
- en
- error
- notfound.phtml
- error
- fr
- error
- notfound.phtml
- error
- en
- views
- <h3>URL not found</h3>
- <p>The requested URL was not found on this server.
- If you entered the URL manually please check your input and try again.
- If you think this is a server error, please <a href="<?php echo $contact_page; ?>">contact us</a>.</p>
- <h3>URL non trouvée</h3>
- <p>L'URL demandée n'a pas été trouvée sur ce serveur.
- Si vous avez tapé l'URL à la main, veuillez vérifier la saisie et réessayer.
- Si vous pensez qu'il s'agit d'une erreur du serveur, merci de <a href="<?php echo $contact_page; ?>">nous contacter</a>.</p>
To capture addressing errors, modify the line in the dispatch
function in the file library/engine.php which directly returns an HTTP/1.0 404 Not Found
document:
- $action=$args=false;
- $r = route($path, $lang);
- if (!$r) {
- $action='error/notfound';
- }
If a path cannot be routed, $action
is set to error/notfound
.
Enter http://localhost/cms/site9/en/nowhere then http://localhost/cms/site9/fr/nullepart in the address bar of your navigator to test the error page in English and in French.
To return an HTTP error if the file with the code for an action is missing, modify the run
function in the file library/engine.php:
- $file = ACTIONS_DIR.DIRECTORY_SEPARATOR.$action.'.php';
- if (!is_file($file)) {
- $action = 'error/internalerror';
- $file = ACTIONS_DIR.DIRECTORY_SEPARATOR.$action.'.php';
- $args = false;
- }
- require_once $file;
Add the file internalerror.php in the folder actions/error with the following content:
- function internalerror($lang) {
- head('title', translate('http_internal_error:title', $lang));
- head('robots', 'noindex, nofollow');
- $contact=true;
- $banner = build('banner', $lang, compact('contact'));
- $contact_page=url('contact', $lang);
- $content = view('error/internalerror', $lang, compact('contact_page'));
- $output = layout('standard', compact('header', 'banner', 'content'));
- header('HTTP/1.1 500 Internal Error');
- return $output;
- }
Add the title of the page in the file includes/strings.inc, in English and in French:
- 'http_internal_error:title' => 'Internal Error',
- 'http_internal_error:title' => 'Erreur interne',
Add the views internalerror.phtml in English and in French in the folders views/en/error and views/fr/error:
- <h3>Internal error</h3>
- <p>The server encountered an internal error and was unable to complete your request.
- If you can describe the problem, please <a href="<?php echo $contact_page; ?>">contact us</a>.</p>
- <h3>Erreur interne</h3>
- <p>Le serveur a rencontré une erreur interne et n'a pas pu faire aboutir votre requête.
- Si vous pouvez décrire le problème, merci de <a href="<?php echo $contact_page; ?>">nous contacter</a>.</p>
Lock the access to the site during a maintenance period, add the global parameters $closing_time
and $opening_time
in the configuration file includes/config.inc:
- global $closing_time, $opening_time;
- $closing_time=0; // mktime(13, 0);
- $opening_time=0; // $closing_time+30*60;
$closing_time
gives the closing time of the service.
$opening_time
can indicate when the service will be available again.
Use the PHP function mktime
to set $closing_time
.
Add a number of second to $closing_time
to adjust $opening_time
or leave this parameter at 0 if how long the interruption will be is indeterminate.
Modify the function dispatch
in the file library/engine.php to take the maintenance mode into account:
- global $closing_time, $opening_time;
Gives access to the global parameters $closing_time
and $opening_time
.
- $action=$args=false;
- if ($closing_time and $closing_time <= time()) {
- $action='error/serviceunavailable';
- $args=array($closing_time, $opening_time);
- }
- else {
- $r = route($path, $lang);
- if (!$r) {
- $action='error/notfound';
- }
- else {
- list($action, $args) = $r;
- }
- }
Triggers the action error/serviceunavailable
with the parameters $closing_time
and $opening_time
if $closing_time
is later than the current time. Notice that $opening_time
isn't evaluated.
Add the file serviceunavailable.php in the folder actions/error with the following content:
- function serviceunavailable($lang, $closing_time=false, $opening_time=false) {
- head('title', translate('http_service_unavailable:title', $lang));
- head('robots', 'noindex, nofollow');
- $contact=true;
- $banner = build('banner', $lang, compact('contact'));
- $content = view('error/serviceunavailable', $lang, compact('closing_time', 'opening_time'));
- $output = layout('standard', compact('header', 'banner', 'content'));
- header('HTTP/1.1 503 Service Unavailable');
- return $output;
- }
serviceunavailable
passes the parameters $closing_time
and $opening_time
to the view.
Add the title of the page in the file includes/strings.inc, in English and in French:
- 'http_service_unavailable:title' => 'Service unavailable',
- 'http_service_unavailable:title' => 'Service indisponible',
Add the views serviceunavailable.phtml in English and in French in the folders views/en/error and views/fr/error:
- <?php
- /**
- *
- * @copyright 2010 frasq.org
- * @version 1
- * @link http://www.frasq.org
- */
- ?>
- <h3>Service unavailable</h3>
- <p>The site is under maintenance<?php if (isset($closing_time) and $closing_time): ?> since <?php echo date('H\hi', $closing_time); ?><?php if (isset($opening_time) and $opening_time): ?>. Back to normal scheduled at <b><?php echo date('H\hi', $opening_time); ?></b><?php endif; ?><?php endif; ?>.
- Please accept our apologizes for any inconvenience caused during renovations.
- Thank you for trying again later.</p>
- <h3>Service indisponible</h3>
- <p>Le site est en maintenance<?php if (isset($closing_time) and $closing_time): ?> depuis <?php echo date('H\hi', $closing_time); ?><?php if (isset($opening_time) and $opening_time): ?>. Retour à la normale prévu à <b><?php echo date('H\hi', $opening_time); ?></b><?php endif; ?><?php endif; ?>.
- Veuillez nous excuser pour les désagréments causés par les travaux.
- Merci de réessayer plus tard.</p>
Set $closing_time
in includes/config.inc to a time in the past and $opening_time
to a time in the future. Enter http://localhost/cms/site9/en then http://localhost/cms/site9/fr in the address bar of your navigator to test the locking of the site in English and in French. Try with $opening_time
at 0. Reset $closing_time
to 0 to unlock the site.
One last effort to install a specific page for the other HTTP error codes that the site could be led to return.
Copy all the following files in the folder actions/error:
- $content = view('error/badrequest', $lang, compact('contact_page'));
- $output = layout('standard', compact('header', 'banner', 'content'));
- header('HTTP/1.1 400 Bad Request');
- $content = view('error/unauthorized', $lang, compact('contact_page'));
- $output = layout('standard', compact('header', 'banner', 'content'));
- header('HTTP/1.1 401 Unauthorized');
- $content = view('error/forbidden', $lang, compact('contact_page'));
- $output = layout('standard', compact('header', 'banner', 'content'));
- header('HTTP/1.1 403 Forbidden');
- $content = view('error/notimplemented', $lang, compact('contact_page'));
- $output = layout('standard', compact('header', 'banner', 'content'));
- header('HTTP/1.1 501 Not Implemented');
Define the titles of the pages in includes/strings.inc, in English and in French:
- 'http_bad_request:title' => 'Bad Request',
- 'http_unauthorized:title' => 'Unauthorized',
- 'http_forbidden:title' => 'Forbidden',
- 'http_not_found:title' => 'Not Found',
- 'http_internal_error:title' => 'Internal Error',
- 'http_not_implemented:title' => 'No Implemented;',
- 'http_service_unavailable:title' => 'Service unavailable',
- 'http_bad_request:title' => 'Mauvaise requête',
- 'http_unauthorized:title' => 'Non autorisé',
- 'http_forbidden:title' => 'Interdit',
- 'http_not_found:title' => 'Non trouvé',
- 'http_internal_error:title' => 'Erreur interne',
- 'http_not_implemented:title' => 'Non implémenté',
- 'http_service_unavailable:title' => 'Service indisponible',
Copy all the following views in views/en/error for the English versions and in views/fr/error for the versions in French:
- <h3>Bad request</h3>
- <p>The service has received a malformed or incomplete request which could not be interpreted.
- If you think this is a server error, please <a href="<?php echo $contact_page; ?>">contact us</a>.</p>
- <h3>Requête erronée</h3>
- <p>Le service a reçu une requête mal formulée ou incomplète qui n'a pas pu être interprétée.
- Si vous pensez qu'il s'agit d'une erreur du serveur, merci de <a href="<?php echo $contact_page; ?>">nous contacter</a>.</p>
- <h3>Authentication required</h3>
- <p>This server could not verify that you are authorized to access the requested object.
- If you think this is a server error, please <a href="<?php echo $contact_page; ?>">contact us</a>.</p>
- <h3>Autorisation requise</h3>
- <p>Ce serveur n'a pas été en mesure de vérifier que vous avez l'autorisation d'accéder à l'objet demandé.
- Si vous pensez qu'il s'agit d'une erreur du serveur, merci de <a href="<?php echo $contact_page; ?>">nous contacter</a>.</p>
- <h3>Access forbidden</h3>
- <p>You don't have permission to access the requested object.
- If you think this is a server error, please <a href="<?php echo $contact_page; ?>">contact us</a>.</p>
- <h3>Accès interdit</h3>
- <p>Vous n'avez pas le droit d'accéder à l'objet demandé.
- Si vous pensez qu'il s'agit d'une erreur du serveur, merci de <a href="<?php echo $contact_page; ?>">nous contacter</a>.</p>
- <h3>Action not implemented</h3>
- <p>The service is not able to run the requested action.
- If you think this is a server error, please <a href="<?php echo $contact_page; ?>">contact us</a>.</p>
- <h3>Action non implémentée</h3>
- <p>Le service n'est pas en mesure d'effectuer l'action demandée.
- Si vous pensez qu'il s'agit d'une erreur du serveur, merci de <a href="<?php echo $contact_page; ?>">nous contacter</a>.</p>
Comments