PHP跳转案例

逻辑是这样的,先正则验证是不是正确的URL,如果不是则跳转到百度。

如是正确URL则会跳转到目标站点,再做一次微信/QQ内置浏览器判断。

最后再一次判断参数是否为空,为空则跳转到百度。

案例

<?php
$url = $_REQUEST['url'];
preg_match('/http(s)?:\/\/([\w]+\.)+[\w]+(\/[\w.\/?%&=]*)?/', $url, $matches);
if ($matches[0]){	
	if(strpos($_SERVER['HTTP_USER_AGENT'],'QQ/')!== !1 ||strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false){
		echo '<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>请使用浏览器打开</title><style>.tips img{width:100%;height:auto}blockquote,body,dd,dl,dt,fieldset,figure,h1,h2,h3,h4,h5,h6,hr,html,iframe,legend,li,ol,p,pre,textarea,ul{margin:0;padding:0}h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:400}ul{list-style:none}button,input,select,textarea{margin:0}html{box-sizing:border-box}*,:after,:before{box-sizing:inherit}img,video{height:auto;max-width:100%}iframe{border:0}table{border-collapse:collapse;border-spacing:0}td,th{padding:0;text-align:left}</style></head><body><div class="tips"><img src="https://icon.qiantucdn.com/20200708/726a61fbfff02cf737223f25e6fff6202" alt=""></div></body></html>';die;
	}else{
		die(header("Location: $url"));
	}
}else{	
	die(header("Location: https://www.baidu.com/"));
}
if($url == ''){die(header("Location: https://www.baidu.com/"));}
?>
2 条回复A管理员Y会员M游客
  1. tourist LV.1
    许坏 M 沙发
    filter_var($url, FILTER_VALIDATE_URL)

    我喜欢用这个校检链接
    • tourist LV.9
      蓝优 A
      @许坏:过滤么。。每个人的喜爱不一样-.-我就毕竟喜欢正则