Posted By

hasantayyar on 01/28/10


Tagged

get url php page current


Versions (?)


Advertising

Website Promotion DIRECTORY is a crucial factor for all websites that need to gain better organic search engine rankings and increase website traffic.
Submitting your website as part of your Web Promotion strategy to our SEO friendly and high traffic Business Directory for review is an excellent way to gain a valuable backlink and increase your websites visibility online.

Submit Site


Get current page url


Published in: PHP 






URL: http://www.webcheatsheet.com/PHP/get_current_page_url.php

Expand | Embed | Plain Text
  1. <?php
  2. function curPageURL() {
  3. $pageURL = 'http';
  4. if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
  5. $pageURL .= "://";
  6. if ($_SERVER["SERVER_PORT"] != "80") {
  7. $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
  8. } else {
  9. $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
  10. }
  11. return $pageURL;
  12. }
  13. ?>
  14. You can now get the current page URL using the line:
  15.  
  16. <?php
  17. echo curPageURL();
  18. ?>
  19. Sometimes it is needed to get the page name only. The following example shows how to do it:
  20.  
  21. <?php
  22. function curPageName() {
  23. return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
  24. }
  25.  
  26. echo "The current page name is ".curPageName();
  27. ?>

Report this snippet 

You need to login to post a comment.