I am using XAMPP on Windows Vista. In my development, I have http://127.0.0.1/test_website/
.
How do I get http://127.0.0.1/test_website/
with PHP?
I tried something like these, but none of them worked.
echo dirname(__FILE__)
or
echo basename(__FILE__);
etc.
I found this on http://webcheatsheet.com/php/get_current_page_url.php
Add the following code to a page:
You can now get the current page URL using the line:
Sometimes it is needed to get the page name only. The following example shows how to do it:
Try this:
Learn more about the
$_SERVER
Predefined VariableIf you plan on using https, you can use this:
Per this answer, please make sure to configure your Apache properly so you can safely depend on
SERVER_NAME
.NOTE: If you're depending on
HTTP_HOST
key (which contains user input), you still have to make some cleanup, remove spaces, commas, carriage return , anything that is not a valid character for a domain. Check php builtin parse_url functions for example.Try using:
$_SERVER['SERVER_NAME'];
I used it to echo the base url of my site to link my css.
Hope this helps!
Fun 'base_url' snippet!
Use as simple as:
Here's one I just put together that works for me. It will return an array with 2 elements. The first element is everything before the ? and the second is an array containing all of the query string variables in an associative array.
Note: This is an expansion of the answer provided by maček above. (Credit where credit is due.)