作者:日月小明空间_785 | 来源:互联网 | 2020-09-07 10:34
php使用header函数进行页面跳转的方法:首先创建一个php文件;然后使用“header”函数,代码为“Header(Location:$url)”;接着运行该php文件,实现页面的跳转。
3.header后的PHP代码还会被执行。例如,将浏览器重定向到php1.cn
1、php跳转代码一句话式:
2、php跳转代码if判断式:
代码如下:
if($_COOKIE["u_type"]){
header('location:register.php');
} else{
setCOOKIE('u_type','1','86400*360');//设置COOKIE长期有效
header('location:zc.html');
注:保存为zc.php,当用户访问zc.php时,判断一个COOKIE是否存在,如果存(www.php1.cn)在就跳转到register.php,如果不存在则创建COOKIE然后跳转到zc.html.
URL重定向函数
function redirect($url, $time=0, $msg=”) {
//多行URL地址支持
$url = str_replace(array(“n”, “r”), ”, $url);
if ( empty($msg) )
$msg = “系统将在{$time}秒之后自动跳转到{$url}!”;
if (!headers_sent()) {
// redirect
if (0 === $time) {
header(‘Location: ‘ . $url);
} else {
header(“refresh:{$time};url={$url}”);
echo($msg);
}
exit();
} else {
$str = “”;
if ($time != 0)
$str .= $msg;
exit($str);
}
}
上面的不能返回404状态,如果是页面跳转之后返回404状态代码我们可如下操作
function getref()
{
$url = @$_SERVER['HTTP_REFERER'];
if( !empty( $url ) )
{
if( !strstr($url ,'jb51.net' ) && !strstr($url,'jb51.net'))
{
@header("http/1.1 404 not found");
@header("status: 404 not found");
include("404.html");//跳转到某一个页面,推荐使用这种方法
exit();
}
}
else
{
@header("http/1.1 404 not found");
@header("status: 404 not found");
include("404.html");//跳转到某一个页面,推荐使用这种方法
exit();
}
}
如果要做301也差不多