RSS

Dynamically Getting File and Website Address

Wed, Nov 12, 2008

Code, PHP

Getting the current web site’s address is pretty simple and straight forward in PHP, no crazy functions nor regular expressions are needed. Below I demonstrate how to achieve this one line of code. Also I shall show you how to retrieve the page’s path from the root.

1. Get the current website address.

$website = $_SERVER['HTTP_HOST'];

Woah that was simple! Now the current folder path.

2. Get the current folder path.

$filePath= $_SERVER['REQUEST_URI'];

Did you catch that? It was so fast.

Put the first part and the second together (and of course add an “http://”) and you have the current file’s complete url!

$fileWebAddress= "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
Share and Enjoy:
  • Facebook
  • Twitter
  • MySpace
  • Digg
  • del.icio.us
  • Google Bookmarks
  • Yahoo! Buzz
,

Comments are closed.