Get current URL with PHP in Apache or IIS


/ Published in: PHP
Save to your folder(s)

Get Current URL Path on Apache / IIS


Copy this code and paste it in your HTML
  1. // On Apache ->
  2.  
  3. function selfURL() {
  4. $s = empty($_SERVER["HTTPS"]) ? ''
  5. : ($_SERVER["HTTPS"] == "on") ? "s"
  6. : "";
  7. $protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
  8. $port = ($_SERVER["SERVER_PORT"] == "80") ? ""
  9. : (":".$_SERVER["SERVER_PORT"]);
  10. return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
  11. }
  12. function strleft($s1, $s2) {
  13. return substr($s1, 0, strpos($s1, $s2));
  14. }
  15.  
  16. // On IIS ->
  17.  
  18. function selfURL(){
  19. if(!isset($_SERVER['REQUEST_URI'])){
  20. $serverrequri = $_SERVER['PHP_SELF'];
  21. }else{
  22. $serverrequri = $_SERVER['REQUEST_URI'];
  23. }
  24. $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
  25. $protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
  26. $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
  27. return $protocol."://".$_SERVER['SERVER_NAME'].$port.$serverrequri;
  28. }
  29.  
  30. // Get Current URL ->
  31.  
  32. print(selfURL());

URL: http://dev.kanngard.net/Permalinks/ID_20050507183447.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.