Hongmu Notes
Home Language Notes PHP returns the URL of the current webpage; you can determine whether it is HTTP or HTTPS on your own.
Language Notes PHP

PHP returns the URL of the current webpage; you can determine whether it is HTTP or HTTPS on your own.

PHP returns the URL of the current webpage; you can determine whether it is HTTP or HTTPS on your own.

This is a PHP function used to retrieve the URL of the current request. It constructs the URL based on the current request's protocol, host, and path, and then returns that URL.

The code logic within the function is as follows:

First, by making a judgment$_SERVER['HTTPS']Determine whether the value is 'on' to ascertain if the current request uses the HTTPS protocol. If so, set the protocol to HTTPS.'https'; otherwise, set to'http'

Next, retrieve.$_SERVER['HTTP_HOST']The value represents the hostname of the current request.

Then, retrieve.$_SERVER['REQUEST_URI']The value represents the path of the current request.

Finally, combine the protocol, host, and path to form a complete URL and return it.

PHP code

public function getUrl() {
        $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http';
        $host = $_SERVER['HTTP_HOST'];
        $path = $_SERVER['REQUEST_URI'];
        $url = $protocol . '://' . $host . $path;
        return $url;
}
微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

PHP ob function record

PHP ob function record Language Notes PHP

Usage of the following three functions ob_get_contents(); ob_end_clean(); ob_start(); You can use these functions to buffer local files and execute local script code. Use ob_start() to save the output code into the buffer, and the page will not be displayed; then use ob_get_contents to get the data in the buffer. o…
👁 141
PHP preg

PHP preg Language Notes PHP

The preg_match_all function is used to perform a global regular expression match. preg_match_all() Syntax int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG…
👁 169
Detailed explanation of PHP ternary operator and if

Detailed explanation of PHP ternary operator and if Language Notes PHP

Ternary operator condition ? Result 1 : Result 2 Explanation: The position in front of the question mark is the condition for judgment. If the condition is met, the result is 1, and if it is not met, the result is 2. This article compares and explains the ternary operator and if...else... in detail. I hope it will be helpful to everyone. Today when I was revising my paper online, I encountered a statement that I couldn’t understand: $if_summary = $row['IF_SUMMARY']=…
👁 189
PHP cast type

PHP cast type Language Notes PHP PHP collection PHP and mysql

Get the data type 1. If you want to check the value and type of an expression, use var_dump(). 2. If you just want to get an easy-to-read type expression for debugging, use gettype(). 3. To check a certain type, do not use gettype(), but use the is_type() function. Converting Strings to Numbers When a string is evaluated as a number, the result is determined according to the following rules...
👁 232

Recommended reading

(Adaptive Mobile Version) Responsive, minimalist personal blog and self-media PbootCMS template – Article-based blog website source code download: 0154

(Adaptive Mobile Version) Responsive, minimalist personal blog and self-media PbootCMS template – Article-based blog website source code download: 0154 Practical Collection pbootcms Template

An adaptive, mobile-friendly, responsive, minimalist personal blog and self-media PbootCMS template. Featuring a minimalist design approach that prioritizes the content reading experience, this template is ideal for bloggers, writers, or content creators. Its clean and elegant layout effectively highlights text and ideas, making it the perfect choice for building a personal brand or a showcase platform for your works. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password:...
👁 30
Responsive jewelry and diamond jewelry website template – 0978

Responsive jewelry and diamond jewelry website template – 0978 Practical Collection Yiyou template

An eyouCMS responsive website template designed for the jewelry, diamond, and accessory industry. Its premium, luxurious design is ideal for showcasing jewelry products, diamond jewelry pieces, brand stories, and customisation services. This template helps jewelry brands present their products online and attract high-net-worth customers. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for YououCMS | YououCMS...
👁 30
(Adaptive Mobile) Responsive Electronic Products – PBootCMS Website Template; Download HTML5 Electronic Components Website Source Code – 0808

(Adaptive Mobile) Responsive Electronic Products – PBootCMS Website Template; Download HTML5 Electronic Components Website Source Code – 0808 Practical Collection pbootcms Template

A responsive electronic product and component website template compatible with mobile devices. Featuring a modern, tech-driven design, this template is ideal for electronic component manufacturers or electronics brands to showcase their product lines. It helps electronics companies demonstrate their technical expertise online and attract customers. Template Overview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 49
Skin Care Microcosmic Beauty Device Website Template 0295

Skin Care Microcosmic Beauty Device Website Template 0295 Practical Collection Yiyou template

This EyouCMS template is designed for the skin care and micro-beauty device industry, featuring a fashionable and tech-driven design style ideal for showcasing beauty device products, skincare solutions, technical specifications, and brand identity. It helps beauty device brands showcase their products online and attract both beauty salons and individual consumers. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS...
👁 50
(Adaptive Mobile Version) Responsive Material Automation and Machining Website – PBootCMS Template (HTML5) – Blue Marketing-Theme Machinery and Equipment Website Source Code Download: 0155

(Adaptive Mobile Version) Responsive Material Automation and Machining Website – PBootCMS Template (HTML5) – Blue Marketing-Theme Machinery and Equipment Website Source Code Download: 0155 Practical Collection pbootcms Template

This adaptive mobile-friendly, responsive PbootCMS template for material automation and machining applications features an HTML5 blue marketing style. Its modern, tech-driven design is ideal for showcasing automated production lines, machining equipment, and related technologies. It enables automation equipment manufacturers to present their solutions online and attract manufacturing industry clients. Template Overview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin
👁 44
Responsive boutique leather goods high-end luggage website template 0979

Responsive boutique leather goods high-end luggage website template 0979 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for the premium leather goods and high-end luggage industry. Its sophisticated and fashionable design perfectly showcases leather goods products, brand stories, design concepts, and customisation services. It enables leather goods brands to showcase their products online and attract consumers who value a quality lifestyle. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS...
👁 46