PHP detect currently website url http or https

I have design a template for many websites

however each site may use http or https

I try to use php to detect http or https

if I use HTTP_X_FORWARDED_PROTO to detect, is this correct?

1 answer

  • answered 2018-01-17 05:59 Ravi Shrimali

    Try it. I hope it will work for you. If Any change in code, most welcome

    <?php
        $uri = $_SERVER['REQUEST_URI'];
        echo $uri; // Outputs: URI
        echo "<br/>";
    
        $protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
    
       $url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
       echo $url; // Outputs: Full URL
       echo "<br/>";
    
       $query = $_SERVER['QUERY_STRING'];
       echo $query; // Outputs: Query String
    ?>