Get Current Website Page URL in PHP 8 | Codexpress Programming Blog - Tutorials about, PHP, MySQL and Web Development

Get Current Website Page URL in PHP 8

One reason why you ought not hardcode your URL in config is the point at which you have various stages where your program will be installed on (different server). Every single one of them will have their particular URL, and you would prefer not to change your code as per which server your program is introduced on.

Have a look at $_SERVER['REQUEST_URI'], i.e.

$actual_link = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";

(Note that the double quoted string syntax is perfectly correct)

If you want to support both HTTP and HTTPS, you can use

$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";

Note: using this code has security implications. The client can set HTTP_HOST and REQUEST_URI to any arbitrary value it wants.