PHP Get Current URL

Maxoq

Well-known member
Registered
Joined
Feb 25, 2015
Messages
520
Points
28
In PHP, how can I get the URL of the current page? Preferably just the parts after http://www.example.com or full URL. I can use same codes in a Wordpress page?

For example

Code:
http://www.example.com/category/this-is-my-first-post.html
I want to get this part

Code:
category/this-is-my-first-post.html
or full URL

Code:
http://www.example.com/category/this-is-my-first-post.html
Thanks
 

LJSHost

Well-known member
Hosting Provider
Registered
Joined
Jul 5, 2016
Messages
1,031
Points
63
This will get the URL of the page

Code:
$_SERVER['REQUEST_URI']
 

Maxoq

Well-known member
Registered
Joined
Feb 25, 2015
Messages
520
Points
28
Maxoq
This gets /category/this-is-my-first-post.html

how to get full url?
 

FerdieQO

Well-known member
Joined
Jul 15, 2016
Messages
213
Points
28
FerdieQO
Creating a function like this

Code:
<?php
function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
show full URL whenever you want to display on.

Code:
echo curPageURL();
or using these codes

Code:
$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') === 
FALSE ? 'http' : 'https';            // Get protocol HTTP/HTTPS
$host     = $_SERVER['HTTP_HOST'];   // Get  www.domain.com
$script   = $_SERVER['SCRIPT_NAME']; // Get folder/file.php
$params   = $_SERVER['QUERY_STRING'];// Get Parameters occupation=odesk&name=ashik

$currentUrl = $protocol . '://' . $host . $script . '?' . $params; // Adding all

echo $currentUrl;
If your URL doesn't have parameters then remove this $params in this line

Code:
$currentUrl = $protocol . '://' . $host . $script . '?' . $params; // Adding all
It should be

Code:
$currentUrl = $protocol . '://' . $host . $script;
Hope this helps!
 

Maxoq

Well-known member
Registered
Joined
Feb 25, 2015
Messages
520
Points
28
I tested the codes and they worked on my site, I thought it was more simpler but tried to search then it's quite different from what I thought.
Thanks you guys for supporting me solve this.
 
Older Threads
Newer Threads
Replies
2
Views
2,902
Replies
3
Views
5,188
Replies
8
Views
8,987
Replies
26
Views
22,854
Replies
7
Views
3,616
Latest Threads
Recommended Threads
  • Locked
Replies
10
Views
6,283
Replies
3
Views
2,269
Replies
4
Views
2,569

Latest Hosting OffersNew Reviews

Sponsors

Tag Cloud

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top