PHP how to get the base domain/url?

后端 未结 12 2090
渐次进展
渐次进展 2020-12-04 18:43
function url(){
    if(isset($_SERVER[\'HTTPS\'])){
        $protocol = ($_SERVER[\'HTTPS\'] && $_SERVER[\'HTTPS\'] != \"off\") ? \"https\" : \"http\";
    }         


        
相关标签:
12条回答
  • 2020-12-04 19:31

    Use parse_url() like this:

    function url(){
        if(isset($_SERVER['HTTPS'])){
            $protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
        }
        else{
            $protocol = 'http';
        }
        return $protocol . "://" . parse_url($_SERVER['REQUEST_URI'], PHP_URL_HOST);
    }
    

    Here is another shorter option:

    function url(){
        $pu = parse_url($_SERVER['REQUEST_URI']);
        return $pu["scheme"] . "://" . $pu["host"];
    }
    
    0 讨论(0)
  • 2020-12-04 19:31

    Tenary Operator helps keep it short and simple.

    echo (isset($_SERVER['HTTPS']) ? 'http' : 'https' ). "://" . $_SERVER['SERVER_NAME']  ;
    
    0 讨论(0)
  • 2020-12-04 19:36

    2 lines to solve it

    $actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    $myDomain = preg_replace('/^www\./', '', parse_url($actual_link, PHP_URL_HOST));
    
    0 讨论(0)
  • 2020-12-04 19:37

    If you're using wordpress, use get_site_url:

    get_site_url()
    
    0 讨论(0)
  • 2020-12-04 19:38
    /* Get sub domain or main domain url
     * $url is $_SERVER['SERVER_NAME']
     * $index int remove subdomain if acceess from sub domain my current url is https://support.abcd.com ("support" = 7 (char))
     * $subDomain string 
     * $issecure string https or http
     * return url
     * call like echo getUrl($_SERVER['SERVER_NAME'],7,"payment",true,false);
     * out put https://payment.abcd.com
     * second call echo getUrl($_SERVER['SERVER_NAME'],7,null,true,true);
    */
    function getUrl($url,$index,$subDomain=null,$issecure=false,$www=true) {
      //$url=$_SERVER['SERVER_NAME']
      $protocol=($issecure==true) ?  "https://" : "http://";
      $url= substr($url,$index);
      $www =($www==true) ? "www": "";
      $url= empty($subDomain) ? $protocol.$url : 
      $protocol.$www.$subDomain.$url;
      return $url;
    }
    
    0 讨论(0)
  • 2020-12-04 19:48
    /**
     * Suppose, you are browsing in your localhost 
     * http://localhost/myproject/index.php?id=8
     */
    function getBaseUrl() 
    {
        // output: /myproject/index.php
        $currentPath = $_SERVER['PHP_SELF']; 
    
        // output: Array ( [dirname] => /myproject [basename] => index.php [extension] => php [filename] => index ) 
        $pathInfo = pathinfo($currentPath); 
    
        // output: localhost
        $hostName = $_SERVER['HTTP_HOST']; 
    
        // output: http://
        $protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https'?'https':'http';
    
        // return: http://localhost/myproject/
        return $protocol.'://'.$hostName.$pathInfo['dirname']."/";
    }
    
    0 讨论(0)
提交回复
热议问题