search
Homephp教程php手册PHP封装的一个支持HTML、JS、PHP重定向的多功能跳转函数

PHP 跳转,即重定向浏览器到指定的 URL,是一个很常见的功能。这种功能也有一些细节性的要求,比如等待多少秒以后跳转,用不用JavaScript实现跳转,等等。下面的跳转方法考虑到很多,并参数化,可以用到具体的项目当中。

<&#63;php   
/**   
 * 重定向浏览器到指定的 URL   
 *   
 * @param string $url 要重定向的 url   
 * @param int $delay 等待多少秒以后跳转   
 * @param bool $js 指示是否返回用于跳转的 JavaScript 代码   
 * @param bool $jsWrapped 指示返回 JavaScript 代码时是否使用 <mce:script type="text/javascript"><!-- 
 标签进行包装   
 * @param bool $return 指示是否返回生成的 JavaScript 代码   
 */    
function redirect($url, $delay = 0, $js = false, $jsWrapped = true, $return = false)     
{     
  $delay = (int)$delay;     
  if (!$js) {     
    if (headers_sent() || $delay > 0) {     
      echo <<<EOT     
  <html>     
  <head>     
  <meta http-equiv="refresh" content="{$delay};URL={$url}" />     
  </head>     
  </html>     
EOT;     
      exit;     
    } else {     
      header("Location: {$url}");     
      exit;     
    }     
  }     
    
  $out = '';     
  if ($jsWrapped) {     
    $out .= '<script language="JavaScript" type="text/javascript">';     
  }     
  $url = rawurlencode($url);     
  if ($delay > 0) {     
    $out .= "window.setTimeOut(function () { document.location='{$url}'; }, {$delay});";     
  } else {     
    $out .= "document.location='{$url}';";     
  }     
  if ($jsWrapped) {     
    $out .= ' 
// --></mce:script>';     
  }     
    
  if ($return) {     
    return $out;     
  }     
    
  echo $out;     
  exit;     
}    
&#63;>

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools