dpda53918 2012-12-21 16:05
浏览 111
已采纳

PHP URL重定向 - 我这样做了吗?

I want a link on my site to redirect like such...

http://mysite.com?url=1

$offer = $_GET['url'];

if ($offer == '1'){
  header("Location: http://site1.com");
}
if ($offer == '2'){
  header("Location: http://site2.com");
}

This is functional, but is there a better way to do this rather than having 50 if statements?

  • 写回答

7条回答 默认 最新

  • dsaaqdz6223 2012-12-21 16:13
    关注

    Assuming that your sites aren't really site1.com and site2.com (I'd be impressed if they were), this is probably a good way to approach the problem:

    $sites = array(
        '1' => 'http://www.google.com/',
        '2' => 'http://www.bing.com/'
    );
    
    if (isset($sites[$_GET['url']])) {
        header('Location: ' . $sites[$_GET['url']]);
        die();
    } else {
        die('Sorry, this does not exist.');
    }
    

    This would of course also allow you to do things such as accepting non-numeric values in ?url=.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题