Die to location?

Twinkie

Banned
Messages
1,389
Reaction score
12
Points
0
Is there an easy way to exit a PHP script and show a separate document without changing the URL, something similar to a 404 error page? Like die("location: 404.html") or something like it?
 

gptsven

New Member
Messages
253
Reaction score
5
Points
0
Something like this:


PHP:
$id = 1;
$page = 'pagename';
$query = "SELECT * FROM news WHERE id=$id";

// check if the record excists.

$result = mysql_query($query); 

if ( $result === false ) 
{
  header("location:error.php?refferer=$pagename&errorcode=5")
}
else
{
  /// print page with news 
}

just make a new database that holds different errorcodes and print a suitable message depending on the errorcode. also make another sql database that stores the frequency of errors ocuring, on what page ($pagename)

never use "or die();" only if you see that your query doesnt work to get debugging results to find the error

or did you mean something else?

you can also store data in sessions and use that in error.php
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
I assume you don't want the URL to change because you're simulating an error page.

What you're describing is an internal redirect, which (as far as I can tell) isn't an option within PHP. What you can do is write no output from the script and include() (or virtual()) the error document. The standard approach would be to test at the top of the script anything that would fatally prevent the script from running. That way you don't need to worry about wasted processing or accidentally writing out data. Just to be safe, you still might want to use output buffering.

If you're including an HTTP error page, I recommend using a scripted page so it can set the appropriate Status header.

404.php:
PHP:
<?php
$statusCode=404;
$statusName='Not Found';
$title='Page does not exist...';

// simple URL correction: find the longest head section of the URL that is a valid page
if (function_exists('apache_lookup_uri')) {
	$uri = $_SERVER['REQUEST_URI'];
	do {
		$uri = dirname($uri);
		$uri_info = apache_lookup_uri($uri);
	} while ($uri_info->status >= 400 && $uri && $uri != '/');
}

$errorMessage=<<<EOF
<p>The page you requested ({$_SERVER['REQUEST_URI']}) does not exist, you can try visiting this site&apos;s home page below.<br />
  <span style="font-size:26px;"><a href="http://{$_SERVER['HTTP_HOST']}/" class="Link">http://{$_SERVER['HTTP_HOST']}/</a></span>.
   </p>
EOF;

include('err.php');
?>

err.php:
PHP:
<?php
if (! isset($errorCode)) {
    $errorCode = $_SERVER{'REDIRECT_STATUS'};
}
header("Status: $statusCode $statusName");
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
...
 
Last edited:
Top