Sådan benyttes komponenten Redirect klassen
Først skal du inkludere den fil der beskriver komponenten, som en klasse fil
<? require_once(HTML_PACKAGE_PATH.'/Redirect.php'); ?>
Dernæst kan du enten benytte komponenten som et taglib (statiske metoder):
<? Redirect::display($param1, $param2, $param3, ...); ?>
eller du kan lave en instance af komponenten og benytte metoderne direkte:
<? $object = new Redirect($param1, $param2, $param3, ...); print $object->getHtml(); ?>
Sådan vises komponenten Redirect klassen
Den fulde PHP kildekode for Redirect klassen
<?php/** * @package basic * @see HTML_BASIC_UTIL_PATH.'/Redirect.php' * @copyright (c) http://Finn-Rasmussen.com * @license http://Finn-Rasmussen.com/license/ myPHP License conditions * @author http://Finn-Rasmussen.com * @version 1.11 * @since 27-nov-2009 *//** * The required files */if (defined('HTML_BASE_PAGE_PATH')) { require_once(HTML_BASE_PAGE_PATH.'/Meta.php'); require_once(HTML_BASE_UTIL_PATH.'/Htmlspecialchars.php');}if (defined('HTML_LOG_UTIL_PATH')) { require_once(HTML_LOG_UTIL_PATH.'/Log.php');}if (defined('HTML_BASE_UTIL_PATH')) { require_once(HTML_BASE_UTIL_PATH.'/Link.php');}/** * Redirect to the specified url. * If the headers are already sent, * try to use the html meta HttpEquip refresh tag * Note: It is your responsibility to get the html, print it and then exit * <code> * Usage: * $url = "http://finnrasmussen.dk"; * $param = array(REQUEST_COMMAND=>''); // Start all over * $urlencode = false; * $params = Params::get($param, $urlencode, __FILE__, __LINE__); * $href = Server::getPhPSelf(); * $time = "10"; * * $redirect = new Redirect(); * $html = $redirect->go($url, $params, $time); * print $html; // Print the Redirect message text * exit; // Skip further data processing * Or * $html = Redirect::go($url, $params, $time); * print $html; // Print the Redirect message text * exit; // Skip further data processing * </code> * @package basic */class Redirect { /** * Constructor */ function __construct() { } /** * Go to the specified url. * The url and the params are checked for any '?' characters * @param String $url The url to redirect to * @param String $params The params to append, no leading '?' or '&' * @param String $time The time to redirect in seconds * @return String the html */ public static function go($url='', $params='', $time='') { $html = ''; $theUrl = $url != '' ? $url : $_SERVER['PHP_SELF']; $theTime = $time != '' ? $time : REDIRECT_TIME; if ($params != '') { // Does the url already contain a '?' if (strpos( $theUrl,'?')!==false) { $theUrl .= '&'.$params; } else { // Does the params already contain a '?' if (strpos( $params,'?')!==false) { $theUrl .= $params; // already a '?' } else { $theUrl .= '?'.$params; } } } $filename = ''; $lineno = ''; $html .= "<html><head>\r\n";//die($html); if (headers_sent($filename, $lineno)) { /** * If the url does not coantain 'http: //' * Then assume that the $url is a relative path then set $url = httphost + $url */ if (strpos( $theUrl,'http:/'.'/')===false) { // Does the url already contain a '/' if (strpos( $theUrl,'/')!==false) { // Ignore } else { $theUrl = '/'.$theUrl; } $theUrl = 'http:/'.'/'.$_SERVER['HTTP_HOST'].$theUrl; } $msg = @TEXT_REDIRECT_HEADERS_ARE_SENT." see $filename, $lineno"; if (defined('HTML_LOG_UTIL_PATH')) { Log::info($msg, __FILE__, __LINE__); } if (defined('DEBUG_LEVEL_SHOW_INFO') && DEBUG_LEVEL & DEBUG_LEVEL_SHOW_INFO) { $html .= "<!-- Redirect::go($url, $params) $msg -->\r\n"; } if (defined('HTML_BASE_PAGE_PATH')) { $link = new Link(@LINK_TEXT_CLICK_HERE, $theUrl,'',@LINK_TITLE_CLICK_HERE); $meta = new Meta(); $meta->setHttpEquiv(META_HTTP_EQUIV_REFRESH, "$theTime;URL=$theUrl", META_SHOW_HTTP_EQUIV_EXPIRES); $refresh = $meta->getHttpEquiv(META_HTTP_EQUIV_REFRESH, META_SHOW_HTTP_EQUIV_EXPIRES); /** * Work-around for the headers_sent() * <meta HttpEquiv="refresh" content="$time;URL=$url" /> */ $html .= "$refresh\r\n"; $html .= $link->getHtml()."<br />\r\n"; } else { $html .= "<!-- No Base package -->\r\n"; $html .= '<meta http-equiv="refresh" content="'.$theTime.';URL='.$theUrl.'" ></meta>'."\r\n"; $html .= '<a class="" href="'.$theUrl.'" title="'.@LINK_TITLE_CLICK_HERE.'">'.@LINK_TEXT_CLICK_HERE."</a><br />\r\n"; } $html .= "<!-- $msg -->\r\n"; } else { // 2008-03-23 CMS if ($time == '') { header("Location: $theUrl"); } else { header("Refresh: $theTime;url=$theUrl"); if (defined('DEBUG_LEVEL_SHOW_INFO') && DEBUG_LEVEL & DEBUG_LEVEL_SHOW_INFO) { $html .= @TEXT_REDIRECT_UPDATING_PLEASE_WAIT; $msg = "Redirect.go() Time: $theTime Url: $theUrl"; if (defined('DEBUG_LEVEL_SHOW_INFO') && DEBUG_LEVEL & DEBUG_LEVEL_SHOW_INFO) { $html .= "header(Refresh: $theTime;url=$theUrl)<br />\r\n"; $html .= "$url<br />\r\n"; foreach ($_POST as $key=>$value) { if ($key == 'password' || (defined('SELECT_LOGIN_PASSWORD') && $key == SELECT_LOGIN_PASSWORD) || (defined('REQUEST_CMS_PASSWORD') && $key == REQUEST_CMS_PASSWORD)) { $html .= "$key=********<br />\r\n"; } else { $html .= "$key=".Htmlspecialchars::encode(stripslashes($value))."<br />\r\n"; } } $html .= "<br />$msg\r\n"; } else { $html .= "<br /><!-- $msg -->\r\n"; } } } } Message::display(); $html .= Cache::content(); $html .= "</head></html>\r\n"; return $html; } /** * Get the html code * @return String The html code */ function getHtml() { return "No html code"; }}?>
Den fulde HTML kildekode for Redirect klassen
<? No html code ?>
Her er 'klasse metoderne' for Redirect klassen:
Her er 'objekt variable' for Redirect klassen: