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
222
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,938
Replies
3
Views
5,245
Replies
8
Views
9,177
Replies
26
Views
23,083
Replies
7
Views
3,699
Latest Threads
Replies
1
Views
24
Replies
0
Views
179
Replies
1
Views
39
Replies
2
Views
81

Latest postsNew threads

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