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 the path with query string on the current http request 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'
.
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']
How to get current PHP page name
You can use basename()
and $_SERVER['PHP_SELF']
to get current page file name
echo basename($_SERVER['PHP_SELF']); /* Returns The Current PHP File Name */
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 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']; ?>
PHP - Getting Current URL
$query = $_SERVER['PHP_SELF'];
$path = pathinfo( $query );
$what_you_want = $path['basename'];
Voila.
How to get current URL and change parameter PHP
According to Scuzzy
answer and jlcolon13
Modifications on Scuzzy
answer in this Question
I merge both answers to make it simpler for you. Just Copy code below and paste it into your file!.
function merge_querystring($url = null,$query = null,$recursive = false){
// $url = 'https://www.google.com?q=apple&type=keyword';
// $query = '?q=banana';
// if there's a URL missing or no query string, return
if($url == null)
return false;
if($query == null)
return $url;
// split the url into it's components
$url_components = parse_url($url);
// if we have the query string but no query on the original url
// just return the URL + query string
if(empty($url_components['query']))
return $url.'?'.ltrim($query,'?');
// turn the url's query string into an array
parse_str($url_components['query'],$original_query_string);
// turn the query string into an array
parse_str(parse_url($query,PHP_URL_QUERY),$merged_query_string);
// merge the query string
if ($recursive == true) {
$merged_result = array_filter(array_merge_recursive($original_query_string, $merged_query_string));
} else {
$merged_result = array_filter(array_merge($original_query_string, $merged_query_string));
}
// Find the original query string in the URL and replace it with the new one
$new_url = str_replace($url_components['query'], http_build_query($merged_result), $url);
// If the last query string removed then remove ? from url
if(substr($new_url, -1) == '?') {
return rtrim($new_url,'?');
}
return $new_url;
}
Usage
<a href="<?=merge_querystring($url,'?pro=en');?>">EN</a>
<a href="<?=merge_querystring($url,'?pro=as');?>">AS</a>
Special thanks to Scuzzy
& jlcolon13
Related Topics
Setting Up a Cronjob in Windows Xampp
Using Preg_Split with Multiple Spaces
How to Make My PHP Ide Understand Dependency Injection Containers
Group Subarrays by One Column, Make Comma-Separated Values from Other Column Within Groups
Variable Variables Pointing to Arrays or Nested Objects
Simple Form Not Sending Data via _Post
Isset() Function Is Returning True Even When Item Is Not Set
PHP Checkbox Set to Check Based on Database Value
Importing Multiple CSV Files to MySQL Tables
Storing and Retrieving an Array in a PHP Cookie
MySQL Database Won't Start in Xampp Manager-Osx
Dynamically Load Information to Twitter Bootstrap Modal
Avoiding Recursion with Doctrine Entities and Jmsserializer
Advantages/Inconveniences of Heredoc VS Nowdoc in PHP