Hãy xem
function url_origin[ $s, $use_forwarded_host = false ]
{
$ssl = [ ! empty[ $s['HTTPS'] ] && $s['HTTPS'] == 'on' ];
$sp = strtolower[ $s['SERVER_PROTOCOL'] ];
$protocol = substr[ $sp, 0, strpos[ $sp, '/' ] ] . [ [ $ssl ] ? 's' : '' ];
$port = $s['SERVER_PORT'];
$port = [ [ ! $ssl && $port=='80' ] || [ $ssl && $port=='443' ] ] ? '' : ':'.$port;
$host = [ $use_forwarded_host && isset[ $s['HTTP_X_FORWARDED_HOST'] ] ] ? $s['HTTP_X_FORWARDED_HOST'] : [ isset[ $s['HTTP_HOST'] ] ? $s['HTTP_HOST'] : null ];
$host = isset[ $host ] ? $host : $s['SERVER_NAME'] . $port;
return $protocol . '://' . $host;
}
function full_url[ $s, $use_forwarded_host = false ]
{
return url_origin[ $s, $use_forwarded_host ] . $s['REQUEST_URI'];
}
$absolute_url = full_url[ $_SERVER ];
echo $absolute_url;
3, tức là.$actual_link = "//$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
[Lưu ý rằng cú pháp chuỗi được trích dẫn kép là hoàn toàn chính xác]
Nếu bạn muốn hỗ trợ cả HTTP và HTTPS, bạn có thể sử dụng
$actual_link = [isset[$_SERVER['HTTPS']] && $_SERVER['HTTPS'] === 'on' ? "https" : "http"] . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
Lưu ý của biên tập viên: Sử dụng mã này có ý nghĩa bảo mật. Máy khách có thể đặt http_host và request_uri thành bất kỳ giá trị tùy ý nào mà nó muốn. using this code has security implications. The client can set HTTP_HOST and REQUEST_URI to any arbitrary value it wants.
Đã trả lời ngày 20 tháng 7 năm 2011 lúc 21:33Jul 20, 2011 at 21:33
' . $escaped_url . '';
Dưới đây là một số chi tiết về các vấn đề và trường hợp cạnh của //example.com/path/ Định dạng
Phiên bản đầy đủ
function url_origin[ $s, $use_forwarded_host = false ]
{
$ssl = [ ! empty[ $s['HTTPS'] ] && $s['HTTPS'] == 'on' ];
$sp = strtolower[ $s['SERVER_PROTOCOL'] ];
$protocol = substr[ $sp, 0, strpos[ $sp, '/' ] ] . [ [ $ssl ] ? 's' : '' ];
$port = $s['SERVER_PORT'];
$port = [ [ ! $ssl && $port=='80' ] || [ $ssl && $port=='443' ] ] ? '' : ':'.$port;
$host = [ $use_forwarded_host && isset[ $s['HTTP_X_FORWARDED_HOST'] ] ] ? $s['HTTP_X_FORWARDED_HOST'] : [ isset[ $s['HTTP_HOST'] ] ? $s['HTTP_HOST'] : null ];
$host = isset[ $host ] ? $host : $s['SERVER_NAME'] . $port;
return $protocol . '://' . $host;
}
function full_url[ $s, $use_forwarded_host = false ]
{
return url_origin[ $s, $use_forwarded_host ] . $s['REQUEST_URI'];
}
$absolute_url = full_url[ $_SERVER ];
echo $absolute_url;
Đây là phiên bản sửa đổi rất nhiều của //snipplr.com/view.php?codeview&id=2734 [không còn tồn tại]
Cấu trúc URL:
scheme://username:password@domain:port/path?query_string#fragment_idusername:password@domain:port/path?query_string#fragment_id
Các phần in đậm sẽ không được bao gồm bởi chức năng
Notes:
- Hàm này không bao gồm
4 từ URL đầy đủ hoặc đoạn [băm].function url_origin[ $s, $use_forwarded_host = false ] { $ssl = [ ! empty[ $s['HTTPS'] ] && $s['HTTPS'] == 'on' ]; $sp = strtolower[ $s['SERVER_PROTOCOL'] ]; $protocol = substr[ $sp, 0, strpos[ $sp, '/' ] ] . [ [ $ssl ] ? 's' : '' ]; $port = $s['SERVER_PORT']; $port = [ [ ! $ssl && $port=='80' ] || [ $ssl && $port=='443' ] ] ? '' : ':'.$port; $host = [ $use_forwarded_host && isset[ $s['HTTP_X_FORWARDED_HOST'] ] ] ? $s['HTTP_X_FORWARDED_HOST'] : [ isset[ $s['HTTP_HOST'] ] ? $s['HTTP_HOST'] : null ]; $host = isset[ $host ] ? $host : $s['SERVER_NAME'] . $port; return $protocol . '://' . $host; } function full_url[ $s, $use_forwarded_host = false ] { return url_origin[ $s, $use_forwarded_host ] . $s['REQUEST_URI']; } $absolute_url = full_url[ $_SERVER ]; echo $absolute_url;
- Nó sẽ không hiển thị cổng 80 mặc định cho HTTP và cổng 443 cho HTTPS.
- Chỉ được thử nghiệm với các sơ đồ HTTP và HTTPS.
5 không được máy khách [trình duyệt] gửi đến máy chủ và sẽ không được thêm vào URL đầy đủ.function url_origin[ $s, $use_forwarded_host = false ] { $ssl = [ ! empty[ $s['HTTPS'] ] && $s['HTTPS'] == 'on' ]; $sp = strtolower[ $s['SERVER_PROTOCOL'] ]; $protocol = substr[ $sp, 0, strpos[ $sp, '/' ] ] . [ [ $ssl ] ? 's' : '' ]; $port = $s['SERVER_PORT']; $port = [ [ ! $ssl && $port=='80' ] || [ $ssl && $port=='443' ] ] ? '' : ':'.$port; $host = [ $use_forwarded_host && isset[ $s['HTTP_X_FORWARDED_HOST'] ] ] ? $s['HTTP_X_FORWARDED_HOST'] : [ isset[ $s['HTTP_HOST'] ] ? $s['HTTP_HOST'] : null ]; $host = isset[ $host ] ? $host : $s['SERVER_NAME'] . $port; return $protocol . '://' . $host; } function full_url[ $s, $use_forwarded_host = false ] { return url_origin[ $s, $use_forwarded_host ] . $s['REQUEST_URI']; } $absolute_url = full_url[ $_SERVER ]; echo $absolute_url;
6 sẽ chỉ chứafunction url_origin[ $s, $use_forwarded_host = false ] { $ssl = [ ! empty[ $s['HTTPS'] ] && $s['HTTPS'] == 'on' ]; $sp = strtolower[ $s['SERVER_PROTOCOL'] ]; $protocol = substr[ $sp, 0, strpos[ $sp, '/' ] ] . [ [ $ssl ] ? 's' : '' ]; $port = $s['SERVER_PORT']; $port = [ [ ! $ssl && $port=='80' ] || [ $ssl && $port=='443' ] ] ? '' : ':'.$port; $host = [ $use_forwarded_host && isset[ $s['HTTP_X_FORWARDED_HOST'] ] ] ? $s['HTTP_X_FORWARDED_HOST'] : [ isset[ $s['HTTP_HOST'] ] ? $s['HTTP_HOST'] : null ]; $host = isset[ $host ] ? $host : $s['SERVER_NAME'] . $port; return $protocol . '://' . $host; } function full_url[ $s, $use_forwarded_host = false ] { return url_origin[ $s, $use_forwarded_host ] . $s['REQUEST_URI']; } $absolute_url = full_url[ $_SERVER ]; echo $absolute_url;
7 cho một URL nhưfunction url_origin[ $s, $use_forwarded_host = false ] { $ssl = [ ! empty[ $s['HTTPS'] ] && $s['HTTPS'] == 'on' ]; $sp = strtolower[ $s['SERVER_PROTOCOL'] ]; $protocol = substr[ $sp, 0, strpos[ $sp, '/' ] ] . [ [ $ssl ] ? 's' : '' ]; $port = $s['SERVER_PORT']; $port = [ [ ! $ssl && $port=='80' ] || [ $ssl && $port=='443' ] ] ? '' : ':'.$port; $host = [ $use_forwarded_host && isset[ $s['HTTP_X_FORWARDED_HOST'] ] ] ? $s['HTTP_X_FORWARDED_HOST'] : [ isset[ $s['HTTP_HOST'] ] ? $s['HTTP_HOST'] : null ]; $host = isset[ $host ] ? $host : $s['SERVER_NAME'] . $port; return $protocol . '://' . $host; } function full_url[ $s, $use_forwarded_host = false ] { return url_origin[ $s, $use_forwarded_host ] . $s['REQUEST_URI']; } $absolute_url = full_url[ $_SERVER ]; echo $absolute_url;
8.function url_origin[ $s, $use_forwarded_host = false ] { $ssl = [ ! empty[ $s['HTTPS'] ] && $s['HTTPS'] == 'on' ]; $sp = strtolower[ $s['SERVER_PROTOCOL'] ]; $protocol = substr[ $sp, 0, strpos[ $sp, '/' ] ] . [ [ $ssl ] ? 's' : '' ]; $port = $s['SERVER_PORT']; $port = [ [ ! $ssl && $port=='80' ] || [ $ssl && $port=='443' ] ] ? '' : ':'.$port; $host = [ $use_forwarded_host && isset[ $s['HTTP_X_FORWARDED_HOST'] ] ] ? $s['HTTP_X_FORWARDED_HOST'] : [ isset[ $s['HTTP_HOST'] ] ? $s['HTTP_HOST'] : null ]; $host = isset[ $host ] ? $host : $s['SERVER_NAME'] . $port; return $protocol . '://' . $host; } function full_url[ $s, $use_forwarded_host = false ] { return url_origin[ $s, $use_forwarded_host ] . $s['REQUEST_URI']; } $absolute_url = full_url[ $_SERVER ]; echo $absolute_url;
- Một số CMS và môi trường sẽ viết lại
3 và trả vềfunction url_origin[ $s, $use_forwarded_host = false ] { $ssl = [ ! empty[ $s['HTTPS'] ] && $s['HTTPS'] == 'on' ]; $sp = strtolower[ $s['SERVER_PROTOCOL'] ]; $protocol = substr[ $sp, 0, strpos[ $sp, '/' ] ] . [ [ $ssl ] ? 's' : '' ]; $port = $s['SERVER_PORT']; $port = [ [ ! $ssl && $port=='80' ] || [ $ssl && $port=='443' ] ] ? '' : ':'.$port; $host = [ $use_forwarded_host && isset[ $s['HTTP_X_FORWARDED_HOST'] ] ] ? $s['HTTP_X_FORWARDED_HOST'] : [ isset[ $s['HTTP_HOST'] ] ? $s['HTTP_HOST'] : null ]; $host = isset[ $host ] ? $host : $s['SERVER_NAME'] . $port; return $protocol . '://' . $host; } function full_url[ $s, $use_forwarded_host = false ] { return url_origin[ $s, $use_forwarded_host ] . $s['REQUEST_URI']; } $absolute_url = full_url[ $_SERVER ]; echo $absolute_url;
0 cho một URL như// ======= PATHINFO ====== // $x = pathinfo[$url]; $x['dirname'] 🡺 //example.com/subFolder $x['basename'] 🡺 myfile.php?var=blabla#555 // Unsecure! $x['extension'] 🡺 php?var=blabla#555 // Unsecure! $x['filename'] 🡺 myfile // ======= PARSE_URL ====== // $x = parse_url[$url]; $x['scheme'] 🡺 https $x['host'] 🡺 example.com $x['path'] 🡺 /subFolder/myfile.php $x['query'] 🡺 var=blabla $x['fragment'] 🡺 555 //=================================================== // //========== Self-defined SERVER variables ========== // //=================================================== // $_SERVER["DOCUMENT_ROOT"] 🡺 /home/user/public_html $_SERVER["SERVER_ADDR"] 🡺 143.34.112.23 $_SERVER["SERVER_PORT"] 🡺 80 [or 443, etc..] $_SERVER["REQUEST_SCHEME"] 🡺 https //similar: $_SERVER["SERVER_PROTOCOL"] $_SERVER['HTTP_HOST'] 🡺 example.com [or with WWW] //similar: $_SERVER["SERVER_NAME"] $_SERVER["REQUEST_URI"] 🡺 /subFolder/myfile.php?var=blabla $_SERVER["QUERY_STRING"] 🡺 var=blabla __FILE__ 🡺 /home/user/public_html/subFolder/myfile.php __DIR__ 🡺 /home/user/public_html/subFolder //same: dirname[__FILE__] $_SERVER["REQUEST_URI"] 🡺 /subFolder/myfile.php?var=blabla parse_url[$_SERVER["REQUEST_URI"], PHP_URL_PATH]🡺 /subFolder/myfile.php $_SERVER["PHP_SELF"] 🡺 /subFolder/myfile.php // ==================================================================// //if "myfile.php" is included in "PARENTFILE.php" , and you visit "PARENTFILE.PHP?abc": $_SERVER["SCRIPT_FILENAME"]🡺 /home/user/public_html/parentfile.php $_SERVER["PHP_SELF"] 🡺 /parentfile.php $_SERVER["REQUEST_URI"] 🡺 /parentfile.php?var=blabla __FILE__ 🡺 /home/user/public_html/subFolder/myfile.php // =================================================== // // ================= handy variables ================= // // =================================================== // // If the site uses $HTTP_or_HTTPS = [[!empty[$_SERVER['HTTPS']] && $_SERVER['HTTPS']!=='off'] || $_SERVER['SERVER_PORT']==443] ? '//':'//' ]; //in some cases, you need to add this condition too: if ['https'==$_SERVER['HTTP_X_FORWARDED_PROTO']] ... // To trim values to filename, i.e. basename[$url] 🡺 myfile.php // Excellent solution to find origin $debug_files = debug_backtrace[]; $caller_file = count[$debug_files] ? $debug_files[count[$debug_files] - 1]['file'] : __FILE__;
8, sử dụngfunction url_origin[ $s, $use_forwarded_host = false ] { $ssl = [ ! empty[ $s['HTTPS'] ] && $s['HTTPS'] == 'on' ]; $sp = strtolower[ $s['SERVER_PROTOCOL'] ]; $protocol = substr[ $sp, 0, strpos[ $sp, '/' ] ] . [ [ $ssl ] ? 's' : '' ]; $port = $s['SERVER_PORT']; $port = [ [ ! $ssl && $port=='80' ] || [ $ssl && $port=='443' ] ] ? '' : ':'.$port; $host = [ $use_forwarded_host && isset[ $s['HTTP_X_FORWARDED_HOST'] ] ] ? $s['HTTP_X_FORWARDED_HOST'] : [ isset[ $s['HTTP_HOST'] ] ? $s['HTTP_HOST'] : null ]; $host = isset[ $host ] ? $host : $s['SERVER_NAME'] . $port; return $protocol . '://' . $host; } function full_url[ $s, $use_forwarded_host = false ] { return url_origin[ $s, $use_forwarded_host ] . $s['REQUEST_URI']; } $absolute_url = full_url[ $_SERVER ]; echo $absolute_url;
2 trong trường hợp này.// ======= PATHINFO ====== // $x = pathinfo[$url]; $x['dirname'] 🡺 //example.com/subFolder $x['basename'] 🡺 myfile.php?var=blabla#555 // Unsecure! $x['extension'] 🡺 php?var=blabla#555 // Unsecure! $x['filename'] 🡺 myfile // ======= PARSE_URL ====== // $x = parse_url[$url]; $x['scheme'] 🡺 https $x['host'] 🡺 example.com $x['path'] 🡺 /subFolder/myfile.php $x['query'] 🡺 var=blabla $x['fragment'] 🡺 555 //=================================================== // //========== Self-defined SERVER variables ========== // //=================================================== // $_SERVER["DOCUMENT_ROOT"] 🡺 /home/user/public_html $_SERVER["SERVER_ADDR"] 🡺 143.34.112.23 $_SERVER["SERVER_PORT"] 🡺 80 [or 443, etc..] $_SERVER["REQUEST_SCHEME"] 🡺 https //similar: $_SERVER["SERVER_PROTOCOL"] $_SERVER['HTTP_HOST'] 🡺 example.com [or with WWW] //similar: $_SERVER["SERVER_NAME"] $_SERVER["REQUEST_URI"] 🡺 /subFolder/myfile.php?var=blabla $_SERVER["QUERY_STRING"] 🡺 var=blabla __FILE__ 🡺 /home/user/public_html/subFolder/myfile.php __DIR__ 🡺 /home/user/public_html/subFolder //same: dirname[__FILE__] $_SERVER["REQUEST_URI"] 🡺 /subFolder/myfile.php?var=blabla parse_url[$_SERVER["REQUEST_URI"], PHP_URL_PATH]🡺 /subFolder/myfile.php $_SERVER["PHP_SELF"] 🡺 /subFolder/myfile.php // ==================================================================// //if "myfile.php" is included in "PARENTFILE.php" , and you visit "PARENTFILE.PHP?abc": $_SERVER["SCRIPT_FILENAME"]🡺 /home/user/public_html/parentfile.php $_SERVER["PHP_SELF"] 🡺 /parentfile.php $_SERVER["REQUEST_URI"] 🡺 /parentfile.php?var=blabla __FILE__ 🡺 /home/user/public_html/subFolder/myfile.php // =================================================== // // ================= handy variables ================= // // =================================================== // // If the site uses $HTTP_or_HTTPS = [[!empty[$_SERVER['HTTPS']] && $_SERVER['HTTPS']!=='off'] || $_SERVER['SERVER_PORT']==443] ? '//':'//' ]; //in some cases, you need to add this condition too: if ['https'==$_SERVER['HTTP_X_FORWARDED_PROTO']] ... // To trim values to filename, i.e. basename[$url] 🡺 myfile.php // Excellent solution to find origin $debug_files = debug_backtrace[]; $caller_file = count[$debug_files] ? $debug_files[count[$debug_files] - 1]['file'] : __FILE__;
- Hãy nhớ rằng một URI =
3, nhưng do sử dụng phổ biến, URL bây giờ có nghĩa là cả URI và URL.// ======= PATHINFO ====== // $x = pathinfo[$url]; $x['dirname'] 🡺 //example.com/subFolder $x['basename'] 🡺 myfile.php?var=blabla#555 // Unsecure! $x['extension'] 🡺 php?var=blabla#555 // Unsecure! $x['filename'] 🡺 myfile // ======= PARSE_URL ====== // $x = parse_url[$url]; $x['scheme'] 🡺 https $x['host'] 🡺 example.com $x['path'] 🡺 /subFolder/myfile.php $x['query'] 🡺 var=blabla $x['fragment'] 🡺 555 //=================================================== // //========== Self-defined SERVER variables ========== // //=================================================== // $_SERVER["DOCUMENT_ROOT"] 🡺 /home/user/public_html $_SERVER["SERVER_ADDR"] 🡺 143.34.112.23 $_SERVER["SERVER_PORT"] 🡺 80 [or 443, etc..] $_SERVER["REQUEST_SCHEME"] 🡺 https //similar: $_SERVER["SERVER_PROTOCOL"] $_SERVER['HTTP_HOST'] 🡺 example.com [or with WWW] //similar: $_SERVER["SERVER_NAME"] $_SERVER["REQUEST_URI"] 🡺 /subFolder/myfile.php?var=blabla $_SERVER["QUERY_STRING"] 🡺 var=blabla __FILE__ 🡺 /home/user/public_html/subFolder/myfile.php __DIR__ 🡺 /home/user/public_html/subFolder //same: dirname[__FILE__] $_SERVER["REQUEST_URI"] 🡺 /subFolder/myfile.php?var=blabla parse_url[$_SERVER["REQUEST_URI"], PHP_URL_PATH]🡺 /subFolder/myfile.php $_SERVER["PHP_SELF"] 🡺 /subFolder/myfile.php // ==================================================================// //if "myfile.php" is included in "PARENTFILE.php" , and you visit "PARENTFILE.PHP?abc": $_SERVER["SCRIPT_FILENAME"]🡺 /home/user/public_html/parentfile.php $_SERVER["PHP_SELF"] 🡺 /parentfile.php $_SERVER["REQUEST_URI"] 🡺 /parentfile.php?var=blabla __FILE__ 🡺 /home/user/public_html/subFolder/myfile.php // =================================================== // // ================= handy variables ================= // // =================================================== // // If the site uses $HTTP_or_HTTPS = [[!empty[$_SERVER['HTTPS']] && $_SERVER['HTTPS']!=='off'] || $_SERVER['SERVER_PORT']==443] ? '//':'//' ]; //in some cases, you need to add this condition too: if ['https'==$_SERVER['HTTP_X_FORWARDED_PROTO']] ... // To trim values to filename, i.e. basename[$url] 🡺 myfile.php // Excellent solution to find origin $debug_files = debug_backtrace[]; $caller_file = count[$debug_files] ? $debug_files[count[$debug_files] - 1]['file'] : __FILE__;
- Bạn nên xóa
4 nếu bạn không có kế hoạch sử dụng proxy hoặc người cân bằng.// ======= PATHINFO ====== // $x = pathinfo[$url]; $x['dirname'] 🡺 //example.com/subFolder $x['basename'] 🡺 myfile.php?var=blabla#555 // Unsecure! $x['extension'] 🡺 php?var=blabla#555 // Unsecure! $x['filename'] 🡺 myfile // ======= PARSE_URL ====== // $x = parse_url[$url]; $x['scheme'] 🡺 https $x['host'] 🡺 example.com $x['path'] 🡺 /subFolder/myfile.php $x['query'] 🡺 var=blabla $x['fragment'] 🡺 555 //=================================================== // //========== Self-defined SERVER variables ========== // //=================================================== // $_SERVER["DOCUMENT_ROOT"] 🡺 /home/user/public_html $_SERVER["SERVER_ADDR"] 🡺 143.34.112.23 $_SERVER["SERVER_PORT"] 🡺 80 [or 443, etc..] $_SERVER["REQUEST_SCHEME"] 🡺 https //similar: $_SERVER["SERVER_PROTOCOL"] $_SERVER['HTTP_HOST'] 🡺 example.com [or with WWW] //similar: $_SERVER["SERVER_NAME"] $_SERVER["REQUEST_URI"] 🡺 /subFolder/myfile.php?var=blabla $_SERVER["QUERY_STRING"] 🡺 var=blabla __FILE__ 🡺 /home/user/public_html/subFolder/myfile.php __DIR__ 🡺 /home/user/public_html/subFolder //same: dirname[__FILE__] $_SERVER["REQUEST_URI"] 🡺 /subFolder/myfile.php?var=blabla parse_url[$_SERVER["REQUEST_URI"], PHP_URL_PATH]🡺 /subFolder/myfile.php $_SERVER["PHP_SELF"] 🡺 /subFolder/myfile.php // ==================================================================// //if "myfile.php" is included in "PARENTFILE.php" , and you visit "PARENTFILE.PHP?abc": $_SERVER["SCRIPT_FILENAME"]🡺 /home/user/public_html/parentfile.php $_SERVER["PHP_SELF"] 🡺 /parentfile.php $_SERVER["REQUEST_URI"] 🡺 /parentfile.php?var=blabla __FILE__ 🡺 /home/user/public_html/subFolder/myfile.php // =================================================== // // ================= handy variables ================= // // =================================================== // // If the site uses $HTTP_or_HTTPS = [[!empty[$_SERVER['HTTPS']] && $_SERVER['HTTPS']!=='off'] || $_SERVER['SERVER_PORT']==443] ? '//':'//' ]; //in some cases, you need to add this condition too: if ['https'==$_SERVER['HTTP_X_FORWARDED_PROTO']] ... // To trim values to filename, i.e. basename[$url] 🡺 myfile.php // Excellent solution to find origin $debug_files = debug_backtrace[]; $caller_file = count[$debug_files] ? $debug_files[count[$debug_files] - 1]['file'] : __FILE__;
- Thông số kỹ thuật nói rằng tiêu đề
5 phải chứa số cổng trừ khi đó là số mặc định.// ======= PATHINFO ====== // $x = pathinfo[$url]; $x['dirname'] 🡺 //example.com/subFolder $x['basename'] 🡺 myfile.php?var=blabla#555 // Unsecure! $x['extension'] 🡺 php?var=blabla#555 // Unsecure! $x['filename'] 🡺 myfile // ======= PARSE_URL ====== // $x = parse_url[$url]; $x['scheme'] 🡺 https $x['host'] 🡺 example.com $x['path'] 🡺 /subFolder/myfile.php $x['query'] 🡺 var=blabla $x['fragment'] 🡺 555 //=================================================== // //========== Self-defined SERVER variables ========== // //=================================================== // $_SERVER["DOCUMENT_ROOT"] 🡺 /home/user/public_html $_SERVER["SERVER_ADDR"] 🡺 143.34.112.23 $_SERVER["SERVER_PORT"] 🡺 80 [or 443, etc..] $_SERVER["REQUEST_SCHEME"] 🡺 https //similar: $_SERVER["SERVER_PROTOCOL"] $_SERVER['HTTP_HOST'] 🡺 example.com [or with WWW] //similar: $_SERVER["SERVER_NAME"] $_SERVER["REQUEST_URI"] 🡺 /subFolder/myfile.php?var=blabla $_SERVER["QUERY_STRING"] 🡺 var=blabla __FILE__ 🡺 /home/user/public_html/subFolder/myfile.php __DIR__ 🡺 /home/user/public_html/subFolder //same: dirname[__FILE__] $_SERVER["REQUEST_URI"] 🡺 /subFolder/myfile.php?var=blabla parse_url[$_SERVER["REQUEST_URI"], PHP_URL_PATH]🡺 /subFolder/myfile.php $_SERVER["PHP_SELF"] 🡺 /subFolder/myfile.php // ==================================================================// //if "myfile.php" is included in "PARENTFILE.php" , and you visit "PARENTFILE.PHP?abc": $_SERVER["SCRIPT_FILENAME"]🡺 /home/user/public_html/parentfile.php $_SERVER["PHP_SELF"] 🡺 /parentfile.php $_SERVER["REQUEST_URI"] 🡺 /parentfile.php?var=blabla __FILE__ 🡺 /home/user/public_html/subFolder/myfile.php // =================================================== // // ================= handy variables ================= // // =================================================== // // If the site uses $HTTP_or_HTTPS = [[!empty[$_SERVER['HTTPS']] && $_SERVER['HTTPS']!=='off'] || $_SERVER['SERVER_PORT']==443] ? '//':'//' ]; //in some cases, you need to add this condition too: if ['https'==$_SERVER['HTTP_X_FORWARDED_PROTO']] ... // To trim values to filename, i.e. basename[$url] 🡺 myfile.php // Excellent solution to find origin $debug_files = debug_backtrace[]; $caller_file = count[$debug_files] ? $debug_files[count[$debug_files] - 1]['file'] : __FILE__;
Các biến được kiểm soát của máy khách [trình duyệt]:
3. Bất kỳ ký tự không được hỗ trợ nào được mã hóa bởi trình duyệt trước khi chúng được gửi.function url_origin[ $s, $use_forwarded_host = false ] { $ssl = [ ! empty[ $s['HTTPS'] ] && $s['HTTPS'] == 'on' ]; $sp = strtolower[ $s['SERVER_PROTOCOL'] ]; $protocol = substr[ $sp, 0, strpos[ $sp, '/' ] ] . [ [ $ssl ] ? 's' : '' ]; $port = $s['SERVER_PORT']; $port = [ [ ! $ssl && $port=='80' ] || [ $ssl && $port=='443' ] ] ? '' : ':'.$port; $host = [ $use_forwarded_host && isset[ $s['HTTP_X_FORWARDED_HOST'] ] ] ? $s['HTTP_X_FORWARDED_HOST'] : [ isset[ $s['HTTP_HOST'] ] ? $s['HTTP_HOST'] : null ]; $host = isset[ $host ] ? $host : $s['SERVER_NAME'] . $port; return $protocol . '://' . $host; } function full_url[ $s, $use_forwarded_host = false ] { return url_origin[ $s, $use_forwarded_host ] . $s['REQUEST_URI']; } $absolute_url = full_url[ $_SERVER ]; echo $absolute_url;
7 và không phải lúc nào cũng có sẵn theo các bình luận trong hướng dẫn sử dụng PHP: //php.net/manual/en/reserved.variables.php// ======= PATHINFO ====== // $x = pathinfo[$url]; $x['dirname'] 🡺 //example.com/subFolder $x['basename'] 🡺 myfile.php?var=blabla#555 // Unsecure! $x['extension'] 🡺 php?var=blabla#555 // Unsecure! $x['filename'] 🡺 myfile // ======= PARSE_URL ====== // $x = parse_url[$url]; $x['scheme'] 🡺 https $x['host'] 🡺 example.com $x['path'] 🡺 /subFolder/myfile.php $x['query'] 🡺 var=blabla $x['fragment'] 🡺 555 //=================================================== // //========== Self-defined SERVER variables ========== // //=================================================== // $_SERVER["DOCUMENT_ROOT"] 🡺 /home/user/public_html $_SERVER["SERVER_ADDR"] 🡺 143.34.112.23 $_SERVER["SERVER_PORT"] 🡺 80 [or 443, etc..] $_SERVER["REQUEST_SCHEME"] 🡺 https //similar: $_SERVER["SERVER_PROTOCOL"] $_SERVER['HTTP_HOST'] 🡺 example.com [or with WWW] //similar: $_SERVER["SERVER_NAME"] $_SERVER["REQUEST_URI"] 🡺 /subFolder/myfile.php?var=blabla $_SERVER["QUERY_STRING"] 🡺 var=blabla __FILE__ 🡺 /home/user/public_html/subFolder/myfile.php __DIR__ 🡺 /home/user/public_html/subFolder //same: dirname[__FILE__] $_SERVER["REQUEST_URI"] 🡺 /subFolder/myfile.php?var=blabla parse_url[$_SERVER["REQUEST_URI"], PHP_URL_PATH]🡺 /subFolder/myfile.php $_SERVER["PHP_SELF"] 🡺 /subFolder/myfile.php // ==================================================================// //if "myfile.php" is included in "PARENTFILE.php" , and you visit "PARENTFILE.PHP?abc": $_SERVER["SCRIPT_FILENAME"]🡺 /home/user/public_html/parentfile.php $_SERVER["PHP_SELF"] 🡺 /parentfile.php $_SERVER["REQUEST_URI"] 🡺 /parentfile.php?var=blabla __FILE__ 🡺 /home/user/public_html/subFolder/myfile.php // =================================================== // // ================= handy variables ================= // // =================================================== // // If the site uses $HTTP_or_HTTPS = [[!empty[$_SERVER['HTTPS']] && $_SERVER['HTTPS']!=='off'] || $_SERVER['SERVER_PORT']==443] ? '//':'//' ]; //in some cases, you need to add this condition too: if ['https'==$_SERVER['HTTP_X_FORWARDED_PROTO']] ... // To trim values to filename, i.e. basename[$url] 🡺 myfile.php // Excellent solution to find origin $debug_files = debug_backtrace[]; $caller_file = count[$debug_files] ? $debug_files[count[$debug_files] - 1]['file'] : __FILE__;
8 được thiết lập bởi các bộ cân bằng và không được đề cập trong danh sách các biến// ======= PATHINFO ====== // $x = pathinfo[$url]; $x['dirname'] 🡺 //example.com/subFolder $x['basename'] 🡺 myfile.php?var=blabla#555 // Unsecure! $x['extension'] 🡺 php?var=blabla#555 // Unsecure! $x['filename'] 🡺 myfile // ======= PARSE_URL ====== // $x = parse_url[$url]; $x['scheme'] 🡺 https $x['host'] 🡺 example.com $x['path'] 🡺 /subFolder/myfile.php $x['query'] 🡺 var=blabla $x['fragment'] 🡺 555 //=================================================== // //========== Self-defined SERVER variables ========== // //=================================================== // $_SERVER["DOCUMENT_ROOT"] 🡺 /home/user/public_html $_SERVER["SERVER_ADDR"] 🡺 143.34.112.23 $_SERVER["SERVER_PORT"] 🡺 80 [or 443, etc..] $_SERVER["REQUEST_SCHEME"] 🡺 https //similar: $_SERVER["SERVER_PROTOCOL"] $_SERVER['HTTP_HOST'] 🡺 example.com [or with WWW] //similar: $_SERVER["SERVER_NAME"] $_SERVER["REQUEST_URI"] 🡺 /subFolder/myfile.php?var=blabla $_SERVER["QUERY_STRING"] 🡺 var=blabla __FILE__ 🡺 /home/user/public_html/subFolder/myfile.php __DIR__ 🡺 /home/user/public_html/subFolder //same: dirname[__FILE__] $_SERVER["REQUEST_URI"] 🡺 /subFolder/myfile.php?var=blabla parse_url[$_SERVER["REQUEST_URI"], PHP_URL_PATH]🡺 /subFolder/myfile.php $_SERVER["PHP_SELF"] 🡺 /subFolder/myfile.php // ==================================================================// //if "myfile.php" is included in "PARENTFILE.php" , and you visit "PARENTFILE.PHP?abc": $_SERVER["SCRIPT_FILENAME"]🡺 /home/user/public_html/parentfile.php $_SERVER["PHP_SELF"] 🡺 /parentfile.php $_SERVER["REQUEST_URI"] 🡺 /parentfile.php?var=blabla __FILE__ 🡺 /home/user/public_html/subFolder/myfile.php // =================================================== // // ================= handy variables ================= // // =================================================== // // If the site uses $HTTP_or_HTTPS = [[!empty[$_SERVER['HTTPS']] && $_SERVER['HTTPS']!=='off'] || $_SERVER['SERVER_PORT']==443] ? '//':'//' ]; //in some cases, you need to add this condition too: if ['https'==$_SERVER['HTTP_X_FORWARDED_PROTO']] ... // To trim values to filename, i.e. basename[$url] 🡺 myfile.php // Excellent solution to find origin $debug_files = debug_backtrace[]; $caller_file = count[$debug_files] ? $debug_files[count[$debug_files] - 1]['file'] : __FILE__;
9 trong hướng dẫn sử dụng PHP.// ======= PATHINFO ====== // $x = pathinfo[$url]; $x['dirname'] 🡺 //example.com/subFolder $x['basename'] 🡺 myfile.php?var=blabla#555 // Unsecure! $x['extension'] 🡺 php?var=blabla#555 // Unsecure! $x['filename'] 🡺 myfile // ======= PARSE_URL ====== // $x = parse_url[$url]; $x['scheme'] 🡺 https $x['host'] 🡺 example.com $x['path'] 🡺 /subFolder/myfile.php $x['query'] 🡺 var=blabla $x['fragment'] 🡺 555 //=================================================== // //========== Self-defined SERVER variables ========== // //=================================================== // $_SERVER["DOCUMENT_ROOT"] 🡺 /home/user/public_html $_SERVER["SERVER_ADDR"] 🡺 143.34.112.23 $_SERVER["SERVER_PORT"] 🡺 80 [or 443, etc..] $_SERVER["REQUEST_SCHEME"] 🡺 https //similar: $_SERVER["SERVER_PROTOCOL"] $_SERVER['HTTP_HOST'] 🡺 example.com [or with WWW] //similar: $_SERVER["SERVER_NAME"] $_SERVER["REQUEST_URI"] 🡺 /subFolder/myfile.php?var=blabla $_SERVER["QUERY_STRING"] 🡺 var=blabla __FILE__ 🡺 /home/user/public_html/subFolder/myfile.php __DIR__ 🡺 /home/user/public_html/subFolder //same: dirname[__FILE__] $_SERVER["REQUEST_URI"] 🡺 /subFolder/myfile.php?var=blabla parse_url[$_SERVER["REQUEST_URI"], PHP_URL_PATH]🡺 /subFolder/myfile.php $_SERVER["PHP_SELF"] 🡺 /subFolder/myfile.php // ==================================================================// //if "myfile.php" is included in "PARENTFILE.php" , and you visit "PARENTFILE.PHP?abc": $_SERVER["SCRIPT_FILENAME"]🡺 /home/user/public_html/parentfile.php $_SERVER["PHP_SELF"] 🡺 /parentfile.php $_SERVER["REQUEST_URI"] 🡺 /parentfile.php?var=blabla __FILE__ 🡺 /home/user/public_html/subFolder/myfile.php // =================================================== // // ================= handy variables ================= // // =================================================== // // If the site uses $HTTP_or_HTTPS = [[!empty[$_SERVER['HTTPS']] && $_SERVER['HTTPS']!=='off'] || $_SERVER['SERVER_PORT']==443] ? '//':'//' ]; //in some cases, you need to add this condition too: if ['https'==$_SERVER['HTTP_X_FORWARDED_PROTO']] ... // To trim values to filename, i.e. basename[$url] 🡺 myfile.php // Excellent solution to find origin $debug_files = debug_backtrace[]; $caller_file = count[$debug_files] ? $debug_files[count[$debug_files] - 1]['file'] : __FILE__;
Các biến điều khiển máy chủ:
0. Máy khách chọn sử dụng điều này, nhưng máy chủ trả về giá trị thực của trống hoặc "BẬT".// [Let's say, if WordPress is installed in subdirectory: //example.com/wpdir/] home_url[] 🡺 //example.com/wpdir/ // If is_ssl[] is true, then it will be "https" get_stylesheet_directory_uri[] 🡺 //example.com/wpdir/wp-content/themes/THEME_NAME [same: get_bloginfo['template_url'] ] get_stylesheet_directory[] 🡺 /home/user/public_html/wpdir/wp-content/themes/THEME_NAME plugin_dir_url[__FILE__] 🡺 //example.com/wpdir/wp-content/themes/PLUGIN_NAME plugin_dir_path[__FILE__] 🡺 /home/user/public_html/wpdir/wp-content/plugins/PLUGIN_NAME/
1. Máy chủ chỉ chấp nhận các số được phép làm cổng.// [Let's say, if WordPress is installed in subdirectory: //example.com/wpdir/] home_url[] 🡺 //example.com/wpdir/ // If is_ssl[] is true, then it will be "https" get_stylesheet_directory_uri[] 🡺 //example.com/wpdir/wp-content/themes/THEME_NAME [same: get_bloginfo['template_url'] ] get_stylesheet_directory[] 🡺 /home/user/public_html/wpdir/wp-content/themes/THEME_NAME plugin_dir_url[__FILE__] 🡺 //example.com/wpdir/wp-content/themes/PLUGIN_NAME plugin_dir_path[__FILE__] 🡺 /home/user/public_html/wpdir/wp-content/plugins/PLUGIN_NAME/
2. Máy chủ chỉ chấp nhận các giao thức nhất định.// [Let's say, if WordPress is installed in subdirectory: //example.com/wpdir/] home_url[] 🡺 //example.com/wpdir/ // If is_ssl[] is true, then it will be "https" get_stylesheet_directory_uri[] 🡺 //example.com/wpdir/wp-content/themes/THEME_NAME [same: get_bloginfo['template_url'] ] get_stylesheet_directory[] 🡺 /home/user/public_html/wpdir/wp-content/themes/THEME_NAME plugin_dir_url[__FILE__] 🡺 //example.com/wpdir/wp-content/themes/PLUGIN_NAME plugin_dir_path[__FILE__] 🡺 /home/user/public_html/wpdir/wp-content/plugins/PLUGIN_NAME/
3. Nó được đặt thủ công trong cấu hình máy chủ và không có sẵn cho IPv6 theo Kralyk.// [Let's say, if WordPress is installed in subdirectory: //example.com/wpdir/] home_url[] 🡺 //example.com/wpdir/ // If is_ssl[] is true, then it will be "https" get_stylesheet_directory_uri[] 🡺 //example.com/wpdir/wp-content/themes/THEME_NAME [same: get_bloginfo['template_url'] ] get_stylesheet_directory[] 🡺 /home/user/public_html/wpdir/wp-content/themes/THEME_NAME plugin_dir_url[__FILE__] 🡺 //example.com/wpdir/wp-content/themes/PLUGIN_NAME plugin_dir_path[__FILE__] 🡺 /home/user/public_html/wpdir/wp-content/plugins/PLUGIN_NAME/
Related:
Sự khác biệt giữa http_host và server_name trong PHP là gì? Số cổng có được yêu cầu trong tham số tiêu đề "Máy chủ" HTTP không? //stackoverflow.com/a/28049503/175071
Is Port Number Required in HTTP "Host" Header Parameter?
//stackoverflow.com/a/28049503/175071
Đã trả lời ngày 17 tháng 1 năm 2012 lúc 8:57Jan 17, 2012 at 8:57
Timo Huovinentimo HuovinenTimo Huovinen
51.2K33 Huy hiệu vàng145 Huy hiệu bạc136 Huy hiệu đồng33 gold badges145 silver badges136 bronze badges
24
Ví dụ cho:
// [Let's say, if WordPress is installed in subdirectory: //example.com/wpdir/]
home_url[] 🡺 //example.com/wpdir/ // If is_ssl[] is true, then it will be "https"
get_stylesheet_directory_uri[] 🡺 //example.com/wpdir/wp-content/themes/THEME_NAME [same: get_bloginfo['template_url'] ]
get_stylesheet_directory[] 🡺 /home/user/public_html/wpdir/wp-content/themes/THEME_NAME
plugin_dir_url[__FILE__] 🡺 //example.com/wpdir/wp-content/themes/PLUGIN_NAME
plugin_dir_path[__FILE__] 🡺 /home/user/public_html/wpdir/wp-content/plugins/PLUGIN_NAME/
4// [Let's say, if WordPress is installed in subdirectory: //example.com/wpdir/]
home_url[] 🡺 //example.com/wpdir/ // If is_ssl[] is true, then it will be "https"
get_stylesheet_directory_uri[] 🡺 //example.com/wpdir/wp-content/themes/THEME_NAME [same: get_bloginfo['template_url'] ]
get_stylesheet_directory[] 🡺 /home/user/public_html/wpdir/wp-content/themes/THEME_NAME
plugin_dir_url[__FILE__] 🡺 //example.com/wpdir/wp-content/themes/PLUGIN_NAME
plugin_dir_path[__FILE__] 🡺 /home/user/public_html/wpdir/wp-content/plugins/PLUGIN_NAME/
4// ======= PATHINFO ====== //
$x = pathinfo[$url];
$x['dirname'] 🡺 //example.com/subFolder
$x['basename'] 🡺 myfile.php?var=blabla#555 // Unsecure!
$x['extension'] 🡺 php?var=blabla#555 // Unsecure!
$x['filename'] 🡺 myfile
// ======= PARSE_URL ====== //
$x = parse_url[$url];
$x['scheme'] 🡺 https
$x['host'] 🡺 example.com
$x['path'] 🡺 /subFolder/myfile.php
$x['query'] 🡺 var=blabla
$x['fragment'] 🡺 555
//=================================================== //
//========== Self-defined SERVER variables ========== //
//=================================================== //
$_SERVER["DOCUMENT_ROOT"] 🡺 /home/user/public_html
$_SERVER["SERVER_ADDR"] 🡺 143.34.112.23
$_SERVER["SERVER_PORT"] 🡺 80 [or 443, etc..]
$_SERVER["REQUEST_SCHEME"] 🡺 https //similar: $_SERVER["SERVER_PROTOCOL"]
$_SERVER['HTTP_HOST'] 🡺 example.com [or with WWW] //similar: $_SERVER["SERVER_NAME"]
$_SERVER["REQUEST_URI"] 🡺 /subFolder/myfile.php?var=blabla
$_SERVER["QUERY_STRING"] 🡺 var=blabla
__FILE__ 🡺 /home/user/public_html/subFolder/myfile.php
__DIR__ 🡺 /home/user/public_html/subFolder //same: dirname[__FILE__]
$_SERVER["REQUEST_URI"] 🡺 /subFolder/myfile.php?var=blabla
parse_url[$_SERVER["REQUEST_URI"], PHP_URL_PATH]🡺 /subFolder/myfile.php
$_SERVER["PHP_SELF"] 🡺 /subFolder/myfile.php
// ==================================================================//
//if "myfile.php" is included in "PARENTFILE.php" , and you visit "PARENTFILE.PHP?abc":
$_SERVER["SCRIPT_FILENAME"]🡺 /home/user/public_html/parentfile.php
$_SERVER["PHP_SELF"] 🡺 /parentfile.php
$_SERVER["REQUEST_URI"] 🡺 /parentfile.php?var=blabla
__FILE__ 🡺 /home/user/public_html/subFolder/myfile.php
// =================================================== //
// ================= handy variables ================= //
// =================================================== //
// If the site uses
$HTTP_or_HTTPS = [[!empty[$_SERVER['HTTPS']] && $_SERVER['HTTPS']!=='off'] || $_SERVER['SERVER_PORT']==443] ? '//':'//' ]; //in some cases, you need to add this condition too: if ['https'==$_SERVER['HTTP_X_FORWARDED_PROTO']] ...
// To trim values to filename, i.e.
basename[$url] 🡺 myfile.php
// Excellent solution to find origin
$debug_files = debug_backtrace[];
$caller_file = count[$debug_files] ? $debug_files[count[$debug_files] - 1]['file'] : __FILE__;
Notice!
- Hashtag
5 các bộ phận được sử dụng thủ công trong ví dụ trên chỉ cho mục đích minh họa, tuy nhiên, các ngôn ngữ phía máy chủ [bao gồm cả PHP] không thể phát hiện ra chúng [chỉ JavaScript mới có thể làm điều đó, vì hashtag chỉ là chức năng phụ của trình duyệt/máy khách] .// [Let's say, if WordPress is installed in subdirectory: //example.com/wpdir/] home_url[] 🡺 //example.com/wpdir/ // If is_ssl[] is true, then it will be "https" get_stylesheet_directory_uri[] 🡺 //example.com/wpdir/wp-content/themes/THEME_NAME [same: get_bloginfo['template_url'] ] get_stylesheet_directory[] 🡺 /home/user/public_html/wpdir/wp-content/themes/THEME_NAME plugin_dir_url[__FILE__] 🡺 //example.com/wpdir/wp-content/themes/PLUGIN_NAME plugin_dir_path[__FILE__] 🡺 /home/user/public_html/wpdir/wp-content/plugins/PLUGIN_NAME/
6 trả về// [Let's say, if WordPress is installed in subdirectory: //example.com/wpdir/] home_url[] 🡺 //example.com/wpdir/ // If is_ssl[] is true, then it will be "https" get_stylesheet_directory_uri[] 🡺 //example.com/wpdir/wp-content/themes/THEME_NAME [same: get_bloginfo['template_url'] ] get_stylesheet_directory[] 🡺 /home/user/public_html/wpdir/wp-content/themes/THEME_NAME plugin_dir_url[__FILE__] 🡺 //example.com/wpdir/wp-content/themes/PLUGIN_NAME plugin_dir_path[__FILE__] 🡺 /home/user/public_html/wpdir/wp-content/plugins/PLUGIN_NAME/
7 cho lưu trữ kiểu Windows, thay vì// [Let's say, if WordPress is installed in subdirectory: //example.com/wpdir/] home_url[] 🡺 //example.com/wpdir/ // If is_ssl[] is true, then it will be "https" get_stylesheet_directory_uri[] 🡺 //example.com/wpdir/wp-content/themes/THEME_NAME [same: get_bloginfo['template_url'] ] get_stylesheet_directory[] 🡺 /home/user/public_html/wpdir/wp-content/themes/THEME_NAME plugin_dir_url[__FILE__] 🡺 //example.com/wpdir/wp-content/themes/PLUGIN_NAME plugin_dir_path[__FILE__] 🡺 /home/user/public_html/wpdir/wp-content/plugins/PLUGIN_NAME/
8.// [Let's say, if WordPress is installed in subdirectory: //example.com/wpdir/] home_url[] 🡺 //example.com/wpdir/ // If is_ssl[] is true, then it will be "https" get_stylesheet_directory_uri[] 🡺 //example.com/wpdir/wp-content/themes/THEME_NAME [same: get_bloginfo['template_url'] ] get_stylesheet_directory[] 🡺 /home/user/public_html/wpdir/wp-content/themes/THEME_NAME plugin_dir_url[__FILE__] 🡺 //example.com/wpdir/wp-content/themes/PLUGIN_NAME plugin_dir_path[__FILE__] 🡺 /home/user/public_html/wpdir/wp-content/plugins/PLUGIN_NAME/
____
Cho WordPress
// [Let's say, if WordPress is installed in subdirectory: //example.com/wpdir/]
home_url[] 🡺 //example.com/wpdir/ // If is_ssl[] is true, then it will be "https"
get_stylesheet_directory_uri[] 🡺 //example.com/wpdir/wp-content/themes/THEME_NAME [same: get_bloginfo['template_url'] ]
get_stylesheet_directory[] 🡺 /home/user/public_html/wpdir/wp-content/themes/THEME_NAME
plugin_dir_url[__FILE__] 🡺 //example.com/wpdir/wp-content/themes/PLUGIN_NAME
plugin_dir_path[__FILE__] 🡺 /home/user/public_html/wpdir/wp-content/plugins/PLUGIN_NAME/
' . $escaped_url . ''; 0
' . $escaped_url . ''; 1
' . $escaped_url . ''; 2
' . $escaped_url . ''; 3
// ======= PATHINFO ====== //
$x = pathinfo[$url];
$x['dirname'] 🡺 //example.com/subFolder
$x['basename'] 🡺 myfile.php?var=blabla#555 // Unsecure!
$x['extension'] 🡺 php?var=blabla#555 // Unsecure!
$x['filename'] 🡺 myfile
// ======= PARSE_URL ====== //
$x = parse_url[$url];
$x['scheme'] 🡺 https
$x['host'] 🡺 example.com
$x['path'] 🡺 /subFolder/myfile.php
$x['query'] 🡺 var=blabla
$x['fragment'] 🡺 555
//=================================================== //
//========== Self-defined SERVER variables ========== //
//=================================================== //
$_SERVER["DOCUMENT_ROOT"] 🡺 /home/user/public_html
$_SERVER["SERVER_ADDR"] 🡺 143.34.112.23
$_SERVER["SERVER_PORT"] 🡺 80 [or 443, etc..]
$_SERVER["REQUEST_SCHEME"] 🡺 https //similar: $_SERVER["SERVER_PROTOCOL"]
$_SERVER['HTTP_HOST'] 🡺 example.com [or with WWW] //similar: $_SERVER["SERVER_NAME"]
$_SERVER["REQUEST_URI"] 🡺 /subFolder/myfile.php?var=blabla
$_SERVER["QUERY_STRING"] 🡺 var=blabla
__FILE__ 🡺 /home/user/public_html/subFolder/myfile.php
__DIR__ 🡺 /home/user/public_html/subFolder //same: dirname[__FILE__]
$_SERVER["REQUEST_URI"] 🡺 /subFolder/myfile.php?var=blabla
parse_url[$_SERVER["REQUEST_URI"], PHP_URL_PATH]🡺 /subFolder/myfile.php
$_SERVER["PHP_SELF"] 🡺 /subFolder/myfile.php
// ==================================================================//
//if "myfile.php" is included in "PARENTFILE.php" , and you visit "PARENTFILE.PHP?abc":
$_SERVER["SCRIPT_FILENAME"]🡺 /home/user/public_html/parentfile.php
$_SERVER["PHP_SELF"] 🡺 /parentfile.php
$_SERVER["REQUEST_URI"] 🡺 /parentfile.php?var=blabla
__FILE__ 🡺 /home/user/public_html/subFolder/myfile.php
// =================================================== //
// ================= handy variables ================= //
// =================================================== //
// If the site uses
$HTTP_or_HTTPS = [[!empty[$_SERVER['HTTPS']] && $_SERVER['HTTPS']!=='off'] || $_SERVER['SERVER_PORT']==443] ? '//':'//' ]; //in some cases, you need to add this condition too: if ['https'==$_SERVER['HTTP_X_FORWARDED_PROTO']] ...
// To trim values to filename, i.e.
basename[$url] 🡺 myfile.php
// Excellent solution to find origin
$debug_files = debug_backtrace[];
$caller_file = count[$debug_files] ? $debug_files[count[$debug_files] - 1]['file'] : __FILE__;
9 là một mảng chứa thông tin như tiêu đề, đường dẫn và vị trí tập lệnh. Các mục trong mảng này được tạo bởi máy chủ web. Không có gì đảm bảo rằng mọi máy chủ web sẽ cung cấp bất kỳ máy chủ nào trong số này; Máy chủ có thể bỏ qua một số, hoặc cung cấp những người khác không được liệt kê ở đây. Điều đó nói rằng, một số lượng lớn các biến này được tính trong các đặc tả »CGI/1.1, vì vậy bạn sẽ có thể mong đợi những điều đó.$url = "http" . [[$_SERVER['SERVER_PORT'] == 443] ? "s" : ""] . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
2 chứa cùng một thông tin ban đầu, nhưng không phải là một siêu thị. [Lưu ý rằng $url = "http" . [[$_SERVER['SERVER_PORT'] == 443] ? "s" : ""] . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
2 và // ======= PATHINFO ====== //
$x = pathinfo[$url];
$x['dirname'] 🡺 //example.com/subFolder
$x['basename'] 🡺 myfile.php?var=blabla#555 // Unsecure!
$x['extension'] 🡺 php?var=blabla#555 // Unsecure!
$x['filename'] 🡺 myfile
// ======= PARSE_URL ====== //
$x = parse_url[$url];
$x['scheme'] 🡺 https
$x['host'] 🡺 example.com
$x['path'] 🡺 /subFolder/myfile.php
$x['query'] 🡺 var=blabla
$x['fragment'] 🡺 555
//=================================================== //
//========== Self-defined SERVER variables ========== //
//=================================================== //
$_SERVER["DOCUMENT_ROOT"] 🡺 /home/user/public_html
$_SERVER["SERVER_ADDR"] 🡺 143.34.112.23
$_SERVER["SERVER_PORT"] 🡺 80 [or 443, etc..]
$_SERVER["REQUEST_SCHEME"] 🡺 https //similar: $_SERVER["SERVER_PROTOCOL"]
$_SERVER['HTTP_HOST'] 🡺 example.com [or with WWW] //similar: $_SERVER["SERVER_NAME"]
$_SERVER["REQUEST_URI"] 🡺 /subFolder/myfile.php?var=blabla
$_SERVER["QUERY_STRING"] 🡺 var=blabla
__FILE__ 🡺 /home/user/public_html/subFolder/myfile.php
__DIR__ 🡺 /home/user/public_html/subFolder //same: dirname[__FILE__]
$_SERVER["REQUEST_URI"] 🡺 /subFolder/myfile.php?var=blabla
parse_url[$_SERVER["REQUEST_URI"], PHP_URL_PATH]🡺 /subFolder/myfile.php
$_SERVER["PHP_SELF"] 🡺 /subFolder/myfile.php
// ==================================================================//
//if "myfile.php" is included in "PARENTFILE.php" , and you visit "PARENTFILE.PHP?abc":
$_SERVER["SCRIPT_FILENAME"]🡺 /home/user/public_html/parentfile.php
$_SERVER["PHP_SELF"] 🡺 /parentfile.php
$_SERVER["REQUEST_URI"] 🡺 /parentfile.php?var=blabla
__FILE__ 🡺 /home/user/public_html/subFolder/myfile.php
// =================================================== //
// ================= handy variables ================= //
// =================================================== //
// If the site uses
$HTTP_or_HTTPS = [[!empty[$_SERVER['HTTPS']] && $_SERVER['HTTPS']!=='off'] || $_SERVER['SERVER_PORT']==443] ? '//':'//' ]; //in some cases, you need to add this condition too: if ['https'==$_SERVER['HTTP_X_FORWARDED_PROTO']] ...
// To trim values to filename, i.e.
basename[$url] 🡺 myfile.php
// Excellent solution to find origin
$debug_files = debug_backtrace[];
$caller_file = count[$debug_files] ? $debug_files[count[$debug_files] - 1]['file'] : __FILE__;
9 là các biến khác nhau và PHP xử lý chúng như vậy]
' . $escaped_url . ''; 4
' . $escaped_url . ''; 5
Đã trả lời ngày 15 tháng 7 năm 2018 lúc 9:46Jul 15, 2018 at 9:46
' . $escaped_url . ''; 6
Bạn có thể sử dụng một số bộ lọc khác nhau để điều chỉnh nó theo ý thích của bạn.
Đã trả lời ngày 18 tháng 6 năm 2018 lúc 19:06Jun 18, 2018 at 19:06
CodercoderCoder
2.6362 Huy hiệu vàng21 Huy hiệu bạc23 Huy hiệu đồng2 gold badges21 silver badges23 bronze badges
Tôi đã sử dụng tuyên bố này.
$url = "//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
$escaped_url = htmlspecialchars[ $url, ENT_QUOTES, 'UTF-8' ];
echo '' . $escaped_url . '';
7
' . $escaped_url . ''; 8
Thử nghiệm
' . $escaped_url . ''; 9
Đã trả lời ngày 12 tháng 5 năm 2018 lúc 17:22May 12, 2018 at 17:22
Abbas Arifabbas ArifAbbas Arif
3543 Huy hiệu bạc14 Huy hiệu Đồng3 silver badges14 bronze badges
3
Use:
function url_origin[ $s, $use_forwarded_host = false ]
{
$ssl = [ ! empty[ $s['HTTPS'] ] && $s['HTTPS'] == 'on' ];
$sp = strtolower[ $s['SERVER_PROTOCOL'] ];
$protocol = substr[ $sp, 0, strpos[ $sp, '/' ] ] . [ [ $ssl ] ? 's' : '' ];
$port = $s['SERVER_PORT'];
$port = [ [ ! $ssl && $port=='80' ] || [ $ssl && $port=='443' ] ] ? '' : ':'.$port;
$host = [ $use_forwarded_host && isset[ $s['HTTP_X_FORWARDED_HOST'] ] ] ? $s['HTTP_X_FORWARDED_HOST'] : [ isset[ $s['HTTP_HOST'] ] ? $s['HTTP_HOST'] : null ];
$host = isset[ $host ] ? $host : $s['SERVER_NAME'] . $port;
return $protocol . '://' . $host;
}
function full_url[ $s, $use_forwarded_host = false ]
{
return url_origin[ $s, $use_forwarded_host ] . $s['REQUEST_URI'];
}
$absolute_url = full_url[ $_SERVER ];
echo $absolute_url;
0Nguồn: Phát hiện root, đường dẫn và URL của PHP
Đã trả lời ngày 12 tháng 1 năm 2015 lúc 10:36Jan 12, 2015 at 10:36
Hpakniahpakniahpaknia
2.6613 Huy hiệu vàng33 Huy hiệu bạc59 Huy hiệu Đồng3 gold badges33 silver badges59 bronze badges
1
Bạn có thể sử dụng
$url = "http" . [[$_SERVER['SERVER_PORT'] == 443] ? "s" : ""] . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
6 như được minh họa trong đoạn trích bên dưới:function url_origin[ $s, $use_forwarded_host = false ]
{
$ssl = [ ! empty[ $s['HTTPS'] ] && $s['HTTPS'] == 'on' ];
$sp = strtolower[ $s['SERVER_PROTOCOL'] ];
$protocol = substr[ $sp, 0, strpos[ $sp, '/' ] ] . [ [ $ssl ] ? 's' : '' ];
$port = $s['SERVER_PORT'];
$port = [ [ ! $ssl && $port=='80' ] || [ $ssl && $port=='443' ] ] ? '' : ':'.$port;
$host = [ $use_forwarded_host && isset[ $s['HTTP_X_FORWARDED_HOST'] ] ] ? $s['HTTP_X_FORWARDED_HOST'] : [ isset[ $s['HTTP_HOST'] ] ? $s['HTTP_HOST'] : null ];
$host = isset[ $host ] ? $host : $s['SERVER_NAME'] . $port;
return $protocol . '://' . $host;
}
function full_url[ $s, $use_forwarded_host = false ]
{
return url_origin[ $s, $use_forwarded_host ] . $s['REQUEST_URI'];
}
$absolute_url = full_url[ $_SERVER ];
echo $absolute_url;
1
Nyedidikeke
6.2427 Huy hiệu vàng43 Huy hiệu bạc53 Huy hiệu Đồng7 gold badges43 silver badges53 bronze badges
Đã trả lời ngày 13 tháng 4 năm 2017 lúc 8:35Apr 13, 2017 at 8:35
Ninjaninjaninja
4651 Huy hiệu vàng7 Huy hiệu bạc13 Huy hiệu đồng1 gold badge7 silver badges13 bronze badges
2
Tôi nghĩ phương pháp này là tốt..ture nó
function url_origin[ $s, $use_forwarded_host = false ]
{
$ssl = [ ! empty[ $s['HTTPS'] ] && $s['HTTPS'] == 'on' ];
$sp = strtolower[ $s['SERVER_PROTOCOL'] ];
$protocol = substr[ $sp, 0, strpos[ $sp, '/' ] ] . [ [ $ssl ] ? 's' : '' ];
$port = $s['SERVER_PORT'];
$port = [ [ ! $ssl && $port=='80' ] || [ $ssl && $port=='443' ] ] ? '' : ':'.$port;
$host = [ $use_forwarded_host && isset[ $s['HTTP_X_FORWARDED_HOST'] ] ] ? $s['HTTP_X_FORWARDED_HOST'] : [ isset[ $s['HTTP_HOST'] ] ? $s['HTTP_HOST'] : null ];
$host = isset[ $host ] ? $host : $s['SERVER_NAME'] . $port;
return $protocol . '://' . $host;
}
function full_url[ $s, $use_forwarded_host = false ]
{
return url_origin[ $s, $use_forwarded_host ] . $s['REQUEST_URI'];
}
$absolute_url = full_url[ $_SERVER ];
echo $absolute_url;
2Đã trả lời ngày 2 tháng 6 năm 2015 lúc 3:00Jun 2, 2015 at 3:00
UWU_SANDUNUWU_SANDUNUWU_SANDUN
1.06713 Huy hiệu bạc18 Huy hiệu đồng13 silver badges18 bronze badges