ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  Custom Error Pages with PHP and Apache
Subject:   Redirect Class
Date:   2003-04-26 23:11:52
From:   anonymous2
This might be useful to people getting started with this.


<?php


define('DIR_SLASH', '/');

/**
* Redirect Class
* Author : Cameron Green
*
* History :
* 20030427 camerongreen at wildmail dot com
* started work
*
* Simple class to work with custom error pages
*
* To set up redirects in Apache :
* ErrorDocument 404 /error.php
*
* Where error.php is a file in your documentroot which includes and
* instantiates this class and does something useful with it
*/
class Redirect {

/**
* query_string
*
* Holds redirect query string, parsed into
* key valu pairs
*
* @var array $query_string
* @access private
*/
var $query_string;

/**
* url_parts
*
* Hash of 'file' - requested file, and 'dirs' - array of requested directories
*
* @var array $url_parts
* @access private
*/
var $url_parts;



/**
* Constructor
*
* @access public
*/
function Redirect() {
}



/**
* get status
*
* returns REDIRECT_STATUS
*
* @access public
* @return mixed $status string or false if not set
*/
function get_status() {
return (isset($_SERVER['REDIRECT_STATUS']) ? $_SERVER['REDIRECT_STATUS'] : FALSE);
}



/**
* get url
*
* returns REDIRECT_URL
*
* @access public
* @return mixed $url string or false if not set
*/
function get_url() {
return (isset($_SERVER['REDIRECT_URL']) ? $_SERVER['REDIRECT_URL'] : FALSE);
}



/**
* get request method
*
* returns REDIRECT_REQUEST_METHOD
*
* @access public
* @return mixed $request_method string or false if not set
*/
function get_request_method() {
return (isset($_SERVER['REDIRECT_REQUEST_METHOD']) ? $_SERVER['REDIRECT_REQUEST_METHOD'] : FALSE);
}



/**
* get error notes
*
* returns REDIRECT_ERROR_NOTES
*
* @access public
* @return mixed $error_notes string or false if not set
*/
function get_error_notes() {
return (isset($_SERVER['REDIRECT_ERROR_NOTES']) ? $_SERVER['REDIRECT_ERROR_NOTES'] : FALSE);
}



/**
* get raw query string
*
* returns REDIRECT_QUERY_STRING
*
* @access public
* @return mixed $query_string string or false if not set
*/
function get_raw_query_string() {
return (isset($_SERVER['REDIRECT_QUERY_STRING']) ? $_SERVER['REDIRECT_QUERY_STRING'] : FALSE);
}



/**
* Get query string
*
* @access private
* @return mixed $query_string array or false if not set
*/
function get_query_string() {
// if its already set, don't do it again
if (!is_array($this->query_string)) {
if ($query_string = $this->get_raw_query_string()) {
$this->query_string = $this->parse_query_string($query_string);
}
else {
return (FALSE);
}
}


return ($this->query_string);
}



/**
* Get url
*
* @access private
* @return array $url
*/
function get_url_parts() {
// make sure var exists
if (isset($_SERVER['REDIRECT_URL'])) {
// if its already set, don't do it again
if (!is_array($this->url_parts)) {
$this->parse_url($_SERVER['REDIRECT_URL']);
}
}


return ($this->url_parts);
}



/**
* To String
*
* Returns all parameters in string suitable for output to pre
*
* @access public
* @return string $values array as nested string
*/
function to_string() {
$url = $this->get_url_parts();
$output =
"REDIRECT_URL =" . implode($url['dirs'], DIR_SLASH) . (empty($url['file']) ? '' : DIR_SLASH . $url['file']) . "\n" .
"REDIRECT_STATUS =" . $this->get_status() . "\n" .
"REDIRECT_ERROR_NOTES =" . $this->get_error_notes() . "\n" .
"REDIRECT_REQUEST_METHOD =" . $this->get_request_method() . "\n" .
"REDIRECT_QUERY_STRING =" . $this->make_query_string($this->get_query_string()) . "\n";


return ($output);
}



/**
* Make Query String
*
* Takes an associative array and makes a query
* string out of it, encoding values
*
* @param array $assoc_array
* @access public
* @return string $query
*/
function make_query_string($assoc_array) {
if (is_array($assoc_array)) {
$query_array = array();

foreach ($assoc_array as $key => $value) {
$query_array[] = $key . "=" . urlencode($value);
}

return (implode($query_array, "&"));
}


return (FALSE);
}



/**
* parse_query_string
*
* I know there is parse_str, but that puts things into
* global scope which is insane for most things
*
* @param string $query
* @access private
* @return array $query_elements
*/
function parse_query_string($query) {
$return_val = array();

foreach (explode('&', $query) as $key => $value) {
list($attribute, $attribute_value) = explode('=', $value);
$return_val[$attribute] = $attribute_value;
}

return($return_val);
}



/**
* parse_url
*
* takes the passed in url and sets the url_parts
* instance var to the directories and file_name requested
*
* @param string $url
* @access private
*/
function parse_url($url) {
$this->url_parts = array();


$parts = explode(DIR_SLASH, $url);


$potential_file = array_pop($parts);


// the only way we can presume the user has
// requested a file, is if there is a dot in this field
if (strpos($potential_file, '.') === FALSE) {
// put it back on
array_push($parts, $potential_file);
}
else {
$this->url_parts['file'] = $potential_file;
}


$this->url_parts['dirs'] = array_filter($parts, 'not_empty');
}
}



/**
* not empty
*
* call back function to remove empty values from array
*
* @param string $var
* @access public
* @return bool $not_empty
*/
function not_empty($var) {
return (!empty($var));
}

?>