douliao5942 2017-04-05 08:36
浏览 125
已采纳

php如何只允许url中的特殊参数或重定向到其他参数

I need help to write function which help me clean out any unusual ( not allow in array list) then redirect to right url

eg my original is /modules.php?aaa=111&bbb=2

the var aaa and bbb is alow using on urls ,

if some one ads other var to url incase you mess up or want to overhead the server they will redirect back to original one.. but cut all invalid param out.

eg: /modules.php?aaa=111&notgood=999&bbb=2

will redirect back to

modules.php?aaa=111&bbb=2

the &notgood=999 or any other will be strip out..

$goodvar = array("aaa","bbb");
$requesturi = $_SERVER['REQUEST_URI'];

if   /// Need help : if other query not in the list of good var will be clean out of requesturi to build NEWrequesturi
{
    header("Location: ".$NEWrequesturi,TRUE,301); exit; die(); 
}

thanks very much kind regards

  • 写回答

1条回答 默认 最新

  • douqi3195 2017-04-05 08:57
    关注
    <?php
    
    $good_var = ["aaa", "bbb"];
    $query_params = explode(".php?", $_SERVER['REQUEST_URI'])[1];
    $all_query_data = explode("&", $query_params);
    $data_set = [];
    foreach ($all_query_data as $query_value) {
        $param = explode("=", $query_value);
        $data_set[$param[0]] = $param[1];
    }
    $paased_args = array_keys($data_set);
    if (empty(array_diff($paased_args, $good_var))) {
        echo "Url is good";
        // do whatever you want when url is good
    } else {
        echo "Url tempered";
        //url has been tempered do as you wish
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3