16

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:

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

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.

Filed in: PHP Tags: ,

Related Posts

Bookmark and Promote!

16 Responses to "PHP: Get Current Page URL"

  1. Naomi says:

    Great information. Lucky me I ran across your website by chance (stumbleupon). I have book marked it for later!

  2. Armin says:

    Hi Taimoor,
    I’m looking for a URL shortener script for my forum. Do you know how can I create that? I think you’ve taught half of the way in this tutorial.

    • Taimoor says:

      Hi Armin,
      Can you please mention which forum software you are using? There must be some plugins for popular forum softwares.
      Update:
      A new post has been made for this: http://taimoorsultan.com/bit-ly-as-mybb-url-shortener/
      Now, integrate Bit.ly with MyBB.

      • Armin says:

        I’m using MyBB forum. No plugin exist for that :(
        Some hours ago I succeed to generate bit.ly short url of current page using your code and another post in a website.
        Now I have a piece of PHP code which produce this short url, but I don’t know how can I create a button or a link to use that, I want when users click on it, a popup box appears and this code will be loaded.

        • Taimoor says:

          OK. What do you want exactly?
          You want to have URL shortener which shrink URLs appearing in your posts? Like large external link.
          Or, you want to shorten your forum urls for easy sharing?
          I think you should check Yourls.org :)

          • Armin says:

            I want to shorten forum URLS for sharing.
            This script generates shortened URL of current page using Bit.ly and your code. Now I just want to implement this script in my forum. for example I want to create a button and when user clicks on it a box opens and this scripts generate the shortened URL of current page. I’m not familiar with yourls and it’s API. if it provides easier services I will switch to it. for now I just need to insert ShortURL generator for my forum in any way that it’s possible.

            results->nodeKeyVal->hash;}

            echo bitly($url);

            ?>

  3. Armin says:

    Code get corrupted in previous comment

    $url=”http://”.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    function bitly($url)
    {return ‘http://bit.ly/’.simplexml_load_file(‘http://api.bit.ly/shorten?version=2.0.1&format=xml&login={Bitly USER ID}Key={bitly API key}&longUrl=’.urlencode($url))->results->nodeKeyVal->hash;}

    echo bitly($url);

    • Taimoor says:

      Ok. You know what, Yourls has the option to bookmark a special piece of code in your browser, and every time you need to make a url short, you can press that button in your browser and short url will appear in popup. This is simple.
      And it will also work if you post that java script code into your forum. For example you can paste that into the head section with a image button or text. Every time a user clicks on it, a small popup with the shorten URL will appear.
      :)

  4. Armin says:

    Thanks Taimoor, but as I’m not good at all in programming I don’t now how should I do that. :(

    REALLY?!!! You did that for mybb?? Would you please explain the steps with details? My forum charset is UTF-8 and I have enabled GoogleSEO friendly URL. each URL after copying will be converted to moe than 3 lines and it’s too ugly for sharing in emails or chat!
    I would be really appreciated if you help me to add this function to my forum. I can’t even write a piece of code for a popup box, Actually I modify the codes that I find in the net.

© 2013 TaimoorSultan.com. All rights reserved. XHTML / CSS Valid.
Designed by Taimoor Sultan.