How do I get the root url of the site?

前端 未结 5 1508
时光说笑
时光说笑 2020-12-05 13:20

Might be a trivial question, but I am looking for a way to say get the root of a site url, for example: http://localhost/some/folder/containing/something/here/or/there

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-05 14:00

    You can using Define save on define.php include for next time use on other project This is PROTOCOL DOMAIN PORT SITE_ROOT AND SITE PATH

    **
     * domain
     * ex: localhost, maskphp.com, demo.maskphp.com,...
     */
    define('DOMAIN', isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME']);
    
    /**
     * protocol
     * ex: http, https,...
     */
    define('PROTOCOL', isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] === 'on' || $_SERVER['HTTPS'] === 1)
        || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' ? 'https' : 'http');
    
    /**
     * port
     * ex: 80, 8080,...
     */
    define('PORT', $_SERVER['SERVER_PORT']);
    
    /**
     * site path
     * ex: http://localhost/maskphp/ -> /maskphp/
     */
    define('SITE_PATH', preg_replace('/index.php$/i', '', $_SERVER['PHP_SELF']));
    
    /**
     * site root
     * ex: http://maskgroup.com, http://localhost/maskphp/,...
     */
    define('SITE_ROOT', PROTOCOL . '://' . DOMAIN . (PORT === '80' ? '' : ':' . PORT) . SITE_PATH);
    

    You can debug to see result

提交回复
热议问题