-4

在 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/

我怎么能那样做?

4

4 回答 4

1

试试这个:

<?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();
?>
于 2013-08-26T09:16:30.703 回答
0

当你执行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']])
于 2013-08-26T09:20:40.597 回答
0

你可以这样做。

<?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.
}
?>
于 2013-08-26T09:20:49.620 回答
0

尝试这样的事情

请注意,如果查询字符串上的 ?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]}");
于 2013-08-26T09:17:47.047 回答