How to get the current URL in PHP

PHP allows the use of the $_SERVER superglobal variable to retrieve the current URL. This variable has information about the server, such as headers, paths, and script locations.

Check $_SERVER["HTTPS"] and determine the protocol (http://またはhttps://) based on the results. Then use $_SERVER[” HTTP_HOST"] to get the hostname and $_SERVER["REQUEST_URI"] to get the requested URI.

$current_url = (empty($_SERVER["HTTPS"]) ? "http://" : "https://") . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top