Hướng dẫn get root url php

Trong lập trình PHP, việc lấy đường dẫn tuyệt đối, document root hay base url sẽ là 1 chút khó khăn cho những bạn mới làm quen với PHP. Vì vậy bài viết này sẽ hướng dẫn 1 số snippets để lấy absolute path, document root và base URL.

Lấy đường dẫn tuyệt đối [absolute path]

Giả sử code hoặc script của bạn đặt trong /path/directory/, thì đoạn code snippet sau sẽ trả về kết quả như sau:

/var/www/path/example.com/httpdocs/path/directory.

Chú ý là kết quả không có dấu / ở cuối.

Snippet sẽ trả về document root mà không có script filename:

$doc_root = preg_replace["!${_SERVER['SCRIPT_NAME']}$!", '', $_SERVER['SCRIPT_FILENAME']];

Ví dụ sẽ trả về dạng /var/www/path/example.com/httpdocs.

Chú ý là kết quả không có dấu / ở cuối.

Lấy base URL của script hiện tại

Ví dụ, nếu script của bạn đặt trong /path/directory/ của website example.com, thì đoạn code scippet sẽ trả về: //example.com/path/directory.

// base directory
$base_dir = __DIR__;

// server protocol
$protocol = empty[$_SERVER['HTTPS']] ? 'http' : 'https';

// domain name
$domain = $_SERVER['SERVER_NAME'];

// base url
$base_url = preg_replace["!^${doc_root}!", '', $base_dir];

// server port
$port = $_SERVER['SERVER_PORT'];
$disp_port = [$protocol == 'http' && $port == 80 || $protocol == 'https' && $port == 443] ? '' : ":$port";

// put em all together to get the complete base URL
$url = "${protocol}://${domain}${disp_port}${base_url}";

echo $url; // = //example.com/path/directory

Chú ý là kết quả không có dấu / ở cuối.

I'm currently working on a PHP project and am looking for a way to get the URL for the root of the website; I have a configuration file at the root so I'm thinking as to using that to figure out the "base URL." I'm looking for a way to do it dynamically so I can locate the URL of the root of the website, i.e. //domain.com/my_app/. I am doing my best to avoid using relative paths and am using PHP to generate the URLs for whatever I am using. For example, I am using PHP to generate CSS code and the CSS code link to images so I would like to get an absolute URL in here instead of a relative path.

my_app/
    admin/
        resources/
            css/
                admin-css.php
            imgs/
                login.png
    resources/
        css/
            css.php
        imgs/
            my-image.png
            shared-image.png
    config.php

In my resources/css/css.php file, I am looking at getting the "base URL" so I can generate an absolute URL to the imgs folder like //domain.com/resources/imgs/my-image.png but currently I am getting //domain.com/resources/css/imgs/my-image.png since the defines below look at getting the directory of the loaded PHP file, not the included one. I would also like to share images between the folders [i.e. access the shared-image.png file from the admin folder] so getting the base URL would be ideal in generating links. The reason I an avoiding relative paths is because I have a function that creates a URL, createURL[], so I can get all the URLs working without having to hard code anything.

Chủ Đề