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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?