blank.gif
triangle.gif Du er her: /  Forsiden  /  Kildekoden  /  Basic  /  Redirect   Login nu   Login
blank.gif
 ««« Se kilde koden
blank.gif
triangle.gif Basic  Base Component Db Dto Form Form-elements Jquery Layout Menu Menu-fisheye Mvc Tab Table Template Util
blank.gif
blank.gif
 
arrow-headline.gif Index
 
  Tilbage

Navn : Redirect.php


Sample code, tutorial

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();
    ?>

Parent html

Sådan vises komponenten Redirect klassen

No html code

PHP source code

Den fulde PHP kildekode for Redirect klassen

<?
/**
* @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.10
* @since 22-feb-2007
*/

/**
* 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:
*   $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 Redirect() {
    }
    
    
/**
     * 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
     */
    
function go($url='',$params='',$time='') {
        
$html = '';
        
$theTime = $time!=''?$time:REDIRECT_TIME;
        
$theUrl = $url!=''?$url:$_SERVER['PHP_SELF'];
        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(__FILE__,__LINE__,$msg);
            }
            if (
defined('DEBUG_LEVEL') && 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
            
header("Location: $theUrl");
            
//header("Refresh: $theTime;url=$theUrl");
            
$html .= TEXT_REDIRECT_UPDATING_PLEASE_WAIT;
            
$msg = "Redirect.go() Time: $theTime Url: $theUrl";
            if (
defined('DEBUG_LEVEL') && 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";
            }
        }
        
$html .= "</head></html>\r\n";
        return
$html;
    }
    
    
/**
     * Get the html code
     * @return String The html code
     */
    
function getHtml() {
        return
"No html code";
    }
}
?>

HTML source code

Den fulde HTML kildekode for Redirect klassen

<?
No html code
?>

Class methods

Her er 'klasse metoderne' for Redirect klassen:

  • redirect
  • go
  • gethtml

Object vars

Her er 'objekt variable' for Redirect klassen:


 
triangle.gif

danmark

Germany

England

France

Italy

Norge

Sverige

USA


 
blank.gif