How to Get Url of Current Page in PHP

How to get URL of current page in PHP


$_SERVER['REQUEST_URI']

For more details on what info is available in the $_SERVER array, see the PHP manual page for it.

If you also need the query string (the bit after the ? in a URL), that part is in this variable:

$_SERVER['QUERY_STRING']

Get the full URL in PHP

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]";

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

Get current URL path with query string in PHP

You want $_SERVER['REQUEST_URI']. From the docs:

'REQUEST_URI'


The URI which was given in order to access this page; for instance, '/index.html'.

Getting the full URL of the current page (PHP)


function selfURL() 
{
$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
}

function strleft($s1, $s2) { return substr($s1, 0, strpos($s1, $s2)); }

how to get the url of the current page using php


<?php
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo $url;
?>

How to get the full URL of the current page using PHP

You can use this for HTTP request

<?php $current_url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>

You can use this for HTTPS request

<?php $current_url="https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>

You can use this for HTTP/HTTPS request

<?php $current_url="//".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>

Get the current URL #ID in PHP

Done it!

Thanks to the steven7mwesigwa comment, thanks bro!

I changed the button href to:

<button href="mysite.com/exams/articles/?id=v-pills-1#v-pills-1">

Mantaining the #ID funcionality and making it possible to get the id= with PHP, since steven said PHP can't get the #value without the ?= parameter.

And used the following PHP code that I've tried before but didn't work.

$argumento = $_GET['id'];

Thanks to everyone.



Related Topics



Leave a reply



Submit