yeah

搜索

计数器

62820

链接

URL重定向

Dec 24, 2009 12:32:32 AM | Comments(14) | Category:PHP学习 | Tags:

 

<?php
 
        /**
         * URL重定向
         *
         * @param string $url
         * @param int $time
         * @param boolean $js
         * @param string $msg
        public function redirect($url,$time=0, $js = false, $msg='')
        {
                //多行URL地址支持
                $url = str_replace(array("\n", "\r"), '', $url); //如果为多行地址,去除\n \r
                if (!headers_sent()) { //判断header头是否已经发送
                        // redirect
                        if(0===$time) {
                                header("Location: ".$url);
                        }else {
                                header("refresh:{$time};url={$url}");
                                echo($msg);
                        }
                        exit();
                } else if(!$js) { //header头已经发送,并且采用非js方式跳转
                        $str    = "<meta http-equiv='Refresh' content='{$time};URL={$url}'>";
                        if($time!=0)
                                $str   .=   $msg;
                        exit($str);
                } else { //采用js方式跳转
                        $str = "<script type=\"text/javascript\">
                        alert('" . $msg . "');
                        setTimeout(\"location='" . $url . "';\", " . $time . ");";
                }
        }
 

?>