Header php从列表中获取方法


Header php Get method from list

在test.php中,我有以下代码:

<php?
header ('Location: $link ');
$link = $_GET["id"]
 ss1 = 'http://drrd.com';
 ss2 = 'http://drrd2.com';
 ss3 = 'http://drrd3.com';
 ss4 = 'http://drrd4.com';
 ss5 = 'http://drrd5.com';
 ss6 = 'http://drrd6.com';
 ss7 = 'http://drrd7.com';
?>

我什么时候转到test.php?id=ss3页面应该将我重定向到http://drrd3.com/

我怎么能那样做?

试试这个:

<?php
ob_start();
$links = array(
    'ss1' => 'http://drrd.com',
    'ss2' => 'http://drrd2.com',
    'ss3' => 'http://drrd3.com',
    'ss4' => 'http://drrd4.com',
    'ss5' => 'http://drrd5.com',
    'ss6' => 'http://drrd6.com',
    'ss7' => 'http://drrd7.com'
);
$id = $_GET['id'] ? ($links[$_GET['id']] ? $_GET['id']: 'ss1') : 'ss1';
header ('Location:'.$links[$id]);
ob_flush();
?>

当您执行header('Location: $link')时,它会尝试将您重定向到NULL,因此首先您应该设置$link变量。

试试这个

$links = array(
               0=>'Fist Link',
               1=>'Second Link',
               //other links
         );
if (isset($_GET['id']) && array_key_exists($_GET['id'],$links))
    header("Location: ".$links[$_GET['id']])

您可以这样做。

<?php
$links = Array(
 'ss1' => 'http://drrd.com',
 'ss2' => 'http://drrd2.com',
 'ss3' => 'http://drrd3.com',
 'ss4' => 'http://drrd4.com',
 'ss5' => 'http://drrd5.com',
 'ss6' => 'http://drrd6.com',
 'ss7' => 'http://drrd7.com');
if (in_array($_GET['id'], array_keys($links)))
{
  $location = 'Location: '.$links[$_GET['id']];
  header($location);
}
else
{
  // If the id is not valid, you can write an error message here.
}
?>

试试这样的

注意,如果?querystring上的id与数组关键字不匹配

<?php
    $locations = array(
        'ss1' => 'http://drrd.com',
        'ss2' => 'http://drrd2.com',
        'ss3' => 'http://drrd3.com',
        'ss4' => 'http://drrd4.com',
        'ss5' => 'http://drrd5.com',
        'ss6' => 'http://drrd6.com',
        'ss7' => 'http://drrd7.com'
    );
    $link = $_GET['id'];
    header ("Location: {$locations[$link]}");