PHP 多 URL 重定向器与数组


PHP Multi URL Redirector with Array

例如,我试图创建一个php来重定向具有特定ID的多个url

mysite.com/url.php?id=1
id=1  will automatically redirect to www.google.com
mysite.com/url.php?id=2
id = 2  will automatically redirect to www.bing.com
successively ...

我认为数组可以简化它

我会感谢你的回答。谢谢

如果要

使用静态数组,请使用以下代码:

<?php
    $sites = array(
        1 => 'http://domain.tld';
        2 => 'http://anotherdomain.tld';
    );
    if (isset($_GET['id']) && array_key_exists($_GET['id']), $sites) {
        header('location: ' . $sites[$_GET['id']]);
    } else {
        // 404?
    }
?>

你可以使用这样的东西:

<?php

    $id = isset($_GET['id']) ? $_GET['id'] : '';
    $id = (int)(trim($id));

    switch($id){
        case 1:
            header("Location: http://www.google.com/");exit;
            break;
        case 2:
            header("Location: http://www.bing.com/");exit;
            break;
        default:
            header("Location: http://www.default.com/");exit;
            break;
    }
?>