how to get pagename from the URL in php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • neovantage
    New Member
    • Aug 2008
    • 245

    how to get pagename from the URL in php

    Hey all,
    i want to get the page name from the url. e.g.
    My URL is


    and i want to get page name newthread.php. How can i do that? Kindly help me out to sort out my problem.

    Thanks in Advance

    kind regards,
    Mohsin Rafique
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Check out $_SERVER - PHP_SELF may be what you're looking for.

    Comment

    • Amzul
      New Member
      • Oct 2007
      • 130

      #3
      try parse_url() http://www.php.net/manual/en/function.parse-url.php

      Comment

      • neovantage
        New Member
        • Aug 2008
        • 245

        #4
        Sir both the things i know but i just want to get the page name only! not the whole URL or path or host name

        Comment

        • Amzul
          New Member
          • Oct 2007
          • 130

          #5
          [CODE=php]$url ='http://bytes.com/newthread.php';
          $data = parse_url($url) ;
          echo $data['path'];[/CODE]

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            you can check out the $_SERVER variables when you call phpinfo().

            Comment

            • neovantage
              New Member
              • Aug 2008
              • 245

              #7
              Sir in my case, my path is "rage/biz/site/products.php" and my url is "http://localhost/rage/biz/site/products.php"

              As i am running application on my local machine by creating sub directories at the root folder. So in this case how can i get the pagename?

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                get all characters after the last "/". strrpos() may be useful for that.

                Comment

                • pbmods
                  Recognized Expert Expert
                  • Apr 2007
                  • 5821

                  #9
                  Try running basename() on the path.

                  PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.

                  Comment

                  • neovantage
                    New Member
                    • Aug 2008
                    • 245

                    #10
                    Thank you very much. it resolved now

                    Comment

                    • Oliver Francis

                      #11
                      Found this on another website, gets all characters after last "/" this equals to pagename, for example it will echo "index.php" from

                      http://mywebsite.com/folder1/folder2/index.php

                      Code:
                      <?php
                      function curPageName() {
                       return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
                      }
                      
                      echo "The current page name is ".curPageName();
                      ?>

                      Comment

                      Working...