Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the full URL of the current page (PHP) [duplicate]

I'm working on this page: http://localhost/projectname/custom.php

Both <?php echo $_SERVER['REQUEST_URI']; ?> and <?php echo $PHP_SELF; ?> don't give full location. What should I use to grab the full url location?

like image 365
eozzy Avatar asked Feb 10 '10 12:02

eozzy


People also ask

How can I get full page URL in PHP?

In PHP. there is a superglobal variable that can provide you with the URL of the current working page. The $_SERVER is an inbuilt variable that can access the page URL, after that, you have to check that the URL is following which type of HTTP protocol.

How do you get the current page URL?

Answer: Use the window. location. href Property location. href property to get the entire URL of the current page which includes host name, query string, fragment identifier, etc.

How get slug URL in PHP?

php function createUrlSlug($urlString) { $slug = preg_replace('/[^A-Za-z0-9-]+/', '-', $urlString); return $slug; } echo createUrlSlug('this is the example demo page'); // This will return 'this-is-the-example-demo-page' ?>

How can I get full URL in WordPress?

please use these code <? php esc_url(the_permalink()); ?> global $wp; $current_url = home_url( add_query_arg( array(), $wp->request ) );


2 Answers

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)); }
like image 83
Sorantis Avatar answered Sep 27 '22 19:09

Sorantis


There isn't a native method as far as I know, but you could use this:

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;
}
like image 23
harpax Avatar answered Sep 27 '22 20:09

harpax