[英]PHP Header Redirect, If Variable Then Redirect
I want to use one URL to redirect users to various outgoing URLs. 我想使用一个URL将用户重定向到各种传出URL。 For example http://example.com/out.php?ofr=2
where ofr
will refer to the appropriate URL the user should be redirected to. 例如, http://example.com/out.php?ofr=2
ofr
其中ofr
指向用户应重定向到的适当URL。
I have the following php code for out.php
我有以下php代码用于out.php
Is this acceptable, or is there a more efficient way to accomplish this (assuming there were 10 or so different URLs in the below script)? 这是可以接受的,还是有一种更有效的方法来完成此操作(假设以下脚本中有10个左右不同的URL)?
<?php
$ofr = $_GET['ofr'];
if ($ofr == 1) {
header('location: http://google.com');
}
elseif ($ofr == 2) {
header('location: http://yahoo.com');
}
else {
header('location: http://msn.com');
}
?>
Edit: Looking at switch statements as suggested, I believe it would look like: 编辑:按照建议查看switch语句,我相信它看起来像:
$ofr = $_GET['ofr'];
switch ($ofr){
case 1: header('location: http://example_1.com');
break;
case 2: header('location: http://example_2.com');
break;
default: header('location: http://example_2.com');
break;
}
Does that look correct? 看起来正确吗? Thanks! 谢谢!
First of all I suggest to make a redirect function like so: 首先,我建议制作一个重定向函数,如下所示:
function redirect($url)
{
$baseUri=_URL_;
if(headers_sent())
{
$string = '<script type="text/javascript">';
$string .= 'window.location = "' . $baseUri.$url . '"';
$string .= '</script>';
echo $string;
}
else
{
if (isset($_SERVER['HTTP_REFERER']) AND ($url == $_SERVER['HTTP_REFERER']))
header('Location: '.$_SERVER['HTTP_REFERER']);
else
header('Location: '.$baseUri.$url);
}
exit;
}
then in a file called redirectFiles.php make array of the urls that you want to redirect: 然后在一个名为redirectFiles.php的文件中,制作要重定向的网址的数组:
$redirecUrls = [
'location: http://example_1.com',
'location: http://example_2.com',
'location: http://example_3.com',
]
then make a function to do the redirecting: 然后制作一个函数进行重定向:
function redirectUrls($index){
if(isset($redirecUrls[ $index])
return redirect($redirecUrls[ $index])
return false;
}
After that you can do something like this: 之后,您可以执行以下操作:
$ofr = $_GET['ofr'];
if($ofr!='')
redirectUrls($ofr)
Try something like this: 尝试这样的事情:
<?php
$ofr=$_GET['ofr'];
$url_array=array([1]=>http://google.com [2]=>http://yahoo.com);
foreach($url_array as $key=>$value)
{
if($ofr==$key)
header('location: ".$value."');
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.