Tim Dietrich

Custom Software Developer

Home Services Blog About Contact

PHP: Custom 404 Error Handlers And GET / POST Variables

When PHP is being used to handle 404 errors, variables in the GET and POST scopes are not set properly. This is a documented bug going as far back as 2006.

Here's some code that I wrote that sets the GET variables:

$t = explode ( '?', $_SERVER['REQUEST_URI'] );
if ( ( is_array ( $t ) ) and ( count ( $t ) == 2 ) ) {
	$url_vars = explode ( '&', $t[1] );
	foreach ( $url_vars as $url_var ) {
		$url_var_parts = explode ( '=', $url_var );
		$_GET[ $url_var_parts[0] ] = urldecode ( $url_var_parts[1] );
	}
}

The code uses $_SERVER['REQUEST_URI'] (which does contain the URL query string) to create the GET variables.

Unfortunately, I have not yet found a way to set the POST variables.