Get current URL/URI without some of $_GET variables

后端 未结 15 1923
被撕碎了的回忆
被撕碎了的回忆 2021-01-30 09:10

How, in Yii, to get the current page\'s URL. For example:

http://www.yoursite.com/your_yii_application/?lg=pl&id=15

but excluding the

15条回答
  •  被撕碎了的回忆
    2021-01-30 09:18

    I don't know about doing it in Yii, but you could just do this, and it should work anywhere (largely lifted from my answer here):

    // Get HTTP/HTTPS (the possible values for this vary from server to server)
    $myUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] && !in_array(strtolower($_SERVER['HTTPS']),array('off','no'))) ? 'https' : 'http';
    // Get domain portion
    $myUrl .= '://'.$_SERVER['HTTP_HOST'];
    // Get path to script
    $myUrl .= $_SERVER['REQUEST_URI'];
    // Add path info, if any
    if (!empty($_SERVER['PATH_INFO'])) $myUrl .= $_SERVER['PATH_INFO'];
    
    $get = $_GET; // Create a copy of $_GET
    unset($get['lg']); // Unset whatever you don't want
    if (count($get)) { // Only add a query string if there's anything left
      $myUrl .= '?'.http_build_query($get);
    }
    
    echo $myUrl;
    

    Alternatively, you could pass the result of one of the Yii methods into parse_url(), and manipulate the result to re-build what you want.

提交回复
热议问题