Quantcast
Viewing latest article 16
Browse Latest Browse All 18

PHP: Get Current Page URL

Yesterday I was designing a PHP web page and while using Facebook Comments Social Plugin, it was taking same single URL for all comments. Because of this, all comments were showing on all pages because I just put domain name (http://taimoorsultan.com) in the Facebook comments code. But after some searching I got a piece of PHP code, through which I was able to get URL of current page. And every comment made on a specific page/post was just visible at that page, and other pages don’t show that comment. This was exactly what I was searching for. Well, there are too many other reasons for which we can feel need for having a code which can insert Current Page URL automatically. Today I thought of sharing that piece of code with you people. PHP has a very simple yet very useful. Let me share and explain a bit about it.

PHP: Get Current Page URL

1. Add the following code at the starting of the page:

<?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;
}
?>

Now, every time you need to get the Current Page URL in that page, you can write this line of php code:

<?php
  echo curPageURL();
?>

And that’s it. This is a very simple code to get Current Page URL in any page or any function. Hope  it helps you.

The post PHP: Get Current Page URL appeared first on TaimoorSultan.com.


Viewing latest article 16
Browse Latest Browse All 18

Trending Articles