dpsx99068
dpsx99068
2018-04-13 14:39

检测URL是否等于值并执行操作

I want to make action if the current url only equals to this: https://www.example.co.il/index.php?id=1000&2222

$url = 'https://www.example.co.il/index.php?id=1000';
        if(strpos($url,'&2222')) 
          {
        // Do something 
           echo "2222";
        }
        else
        {
        // Do Nothing
        }
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • duanpin5168 duanpin5168 3年前

    USE THIS

    // Assign your parameters here for restricted access
    $valid_url = new stdClass();
    $valid_url->scheme = 'https';
    $valid_url->host = 'www.example.co.il';  
    $valid_url->ids  = array(1000,2222);
    
    $url = 'https://www.example.co.il/index.php?id=1000&2222';         
    $urlinfo = parse_url($url); // pass url here
    $ids = [];
    parse_str(str_replace('&', '&id1=', $urlinfo['query']), $ids);
    
    if($urlinfo['scheme'] == $valid_url->scheme && $urlinfo['host'] == $valid_url->host && count(array_intersect($valid_url->ids, $ids)) == count($valid_url->ids)){
        echo 'valid';
        // Do something 
    }else{
        echo 'in valid';
        // error page
    }
    
    点赞 评论 复制链接分享
  • dongzhuo5185 dongzhuo5185 3年前

    You're able to read the parameters in the URL using the $_GET object. It lists the keys and values in the querystring, i.e. in your example,

    https://www.example.co.il/index.php?id=1000
    

    if you use:

    print $_GET['id'];
    

    you'll see 100.

    so you could simply check for the existence of the key 2222:

    if (isset($_GET['2222'])) { /** do something **/ }
    

    bear in mind, this is only the case if you're actually reading a URL the script is running on.

    your method of searching for a string within the URL is appropriate if you simply want to match a value in a string, whether its a URL or not.

    点赞 评论 复制链接分享
  • dongqian9567 dongqian9567 3年前

    To exactly do what you are asked, try this

     //actual link (http or https)
    $actualUrl = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    $url        = 'https://www.example.co.il/index.php?id=1000';
    if($actualUrl === $url) {
        //do something
    }
    

    But if you just want to retrieve the id :

    $id = $_GET('id');
    //return 1000 in your case
    
    点赞 评论 复制链接分享

相关推荐