Pages - পৃষ্ঠাসমূহ

How to get current page url or link in php

This post shows how to get current page url or link in php Sometimes need some information about the websites or current page for special purposes. In php, to show the current page url or link, which is shown in the browser window url is very easy. Because in php there has some builtin method. But to get complete url or link need some arrangement.

Hide Example Show Example


Suppose your Browser address var url is "http://www.taslimblogger.blogspot.com/2011/11/how-to-get-current-page-url-or-link-in.html". So now learn this method to get output like this.

Get the Hostname

In php get the host of the website is very easy, just print this line,
<?php
 echo "\n Host : ".$_SERVER['HTTP_HOST'];
?>

//output
//www.taslimblogger.blogspot.com

Get the server name

Server name in php call the server function with parameter SERVER_NAME
<?php 
 echo "\n Server Name : ".$_SERVER['SERVER_NAME']; 
?>

//output
//www.taslimblogger.blogspot.com

Get the server port in php

Get the current port of the url
<?php 
 echo "\n Server Port : ". $_SERVER["SERVER_PORT"];
?>

//output
//Server Port : 80

Get the Server address

Protocol name
<?php 
 echo "\n Server Address : ".$_SERVER["SERVER_PROTOCOL"];  
?>

//output
//Server Address : HTTP/1.1

Get the url without hostname

See all code in action
<?php 
 echo "\n This page : ".$_SERVER['PHP_SELF'];  
?>

//output
//This page : 2011/11/how-to-get-current-page-url-or-link-in.html

Get the current page name in php

<?php 
 echo "\n URL : ".$_SERVER['REQUEST_URI']; 
?>

//output
//URL : 2011/11/how-to-get-current-page-url-or-link-in.html

Method 1 Get the complete url for current page

See all code in action
<?php 
 $protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https')=== FALSE ? 'http' : 'https';
    $host     = $_SERVER['HTTP_HOST'];
    $script   = $_SERVER['SCRIPT_NAME'];
    $params   = $_SERVER['QUERY_STRING'];     

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

    echo $currentUrl;
?>

//output
//http://www.taslimblogger.blogspot.com/2011/11/how-to-get-current-page-url-or-link-in.html

Method 2 : Get the complete url for current page

<?php
function currentPageUrl() {
    $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;
}
?>
Now call the function to see the current page url
<?php
  echo curPageURL();
?>

//output
//http://www.taslimblogger.blogspot.com/2011/11/how-to-get-current-page-url-or-link-in.html

Find the file name from url

If anybody want to find the file name from the url it is also done easily.see this method
<?php
function curPageName() {
     return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}

echo "The current page name is ".curPageName();
?>

//output
//The current page name is how-to-get-current-page-url-or-link-in.html

2 comments:

  1. Storing current url in a variable using php is possible by using some lines. Here I am going to teach you, how to get current url in a variable in PHP.



    https://www.codepointhub.com/how-to-get-current-url-in-a-variable-using-php/

    ReplyDelete
  2. Storing current url in a variable using php is possible by using some lines.

    ReplyDelete