Outputting referer_uri in 404 error page
I have done a lot of searching and it doesn't seem like there was a way to provide a template suggestion for the core 404 error page, so I have been tinkering with the CustomError module, and can not get it to consistently output the referring page in my code. This is not limited to this module, since if I add it to a node with the PHP filter turned on, it behaves the same way. When I go to a bad URL, the page outputs but the URL is not displayed. If I then flush the Drupal cache, the referring URL is output. If I refresh again, it is not displayed.
In a static environment, I was able to accomplish this with an SSI include:
<!--#echo var="HTTP_HOST" --><!--#echo var="REQUEST_URI" -->
Here is the code I am using in Drupal:
<?php print referer_uri(); ?>
Thanks.

Not sure I follow. In order
Not sure I follow. In order to specify a custom error page, you can specify one under admin/settings/error-reporting, say call it '404'
In your theme's template.php file, you can do something quick and dirty like this to theme it (with clean-urls)
<?php
function phptemplate_preprocess_page(&$vars) {
$parts = "";
$vars['tabs2'] = menu_secondary_local_tasks();
if (module_exists('path')) {
$alias = drupal_get_path_alias($_GET['q']);
if ($alias != $_GET['q']) {
$suggestions = array();
$template_filename = 'page';
$parts = explode('/', $alias);
foreach ($parts as $path_part) {
$template_filename = $template_filename . '-' . $path_part;
$suggestions[] = $template_filename;
}
}
if (drupal_is_front_page()) {
$vars['template_file'] = 'page-front';
}
if(arg(0) == '404'){
$vars['template_file'] = 'page-404';
}
$vars['template_files'] = $suggestions;
}
// Hook into color.module
if (module_exists('color')) {
_color_page_alter($vars);
}
}
?>
Then just create a template file with your specific customisations, page-404.tpl.php and refresh the cache.
Ok, I've got that part down,
Ok, I've got that part down, where Drupal will use my custom page-404.tpl.php template to theme that node. I am still unable to get the referrer URL to display consistently using:
<php print referer_uri(); ?>Hmm, I'm not having an issue
Hmm, I'm not having an issue trying to recreate the same thing.
Have you tried prining $_SERVER['HTTP_REFERER'] directly? Referer_uri is just a wrapper for it.
Ok, I duplicated the
Ok, I duplicated the problem.
I'm trying to figure out why $_SERVER['HTTP_REFERER'] isn't populated for the error page. Will return if I find out
Ok, it works fine, but only
Ok, it works fine, but only as long as the user-agent has a valid entry for it to use.
Ie, I get nothing if I haven't visited a page begin with. If I manually enter a link into the address bar, it won't. This make sense when we consider the definition:
The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.Personally I'd do away with this, and if you're not against a bit of Javascript, investigate the document.referrer method.
Thanks for all your help. I
Thanks for all your help. I misunderstood referer_uri. What I was looking for was request_uri. I am getting the output I am looking for using this code:
<?php print "http://" . $_SERVER["HTTP_HOST"] . request_uri(); ?>Does $_SERVER["HTTP_HOST"]; need to be passed through a sanitizing function, or is there a better way to write this?
Ideally yes, you'd be wanting
Ideally yes, you'd be wanting to replace it with drupal_valid_http_host (http://api.drupal.org/api/function/drupal_valid_http_host)
<?phpprint "http://" . drupal_valid_http_host($_SERVER["HTTP_HOST"]). request_uri();
?>
It looks like
It looks like drupal_valid_http_host is boolean and is only printing true or false. I was able to use check_url instead:
<?php print "http://" . check_url($_SERVER["HTTP_HOST"]) . request_uri(); ?>Thanks for all the help!
Urk, it is. Sorry about that,
Urk, it is. Sorry about that, not with it today :)