This question already has answers here:
Get the full URL in PHP
(27 answers)
Closed 8 years ago.
In PHP, how can I get the URL of the current page? Preferably just the parts after http://domain.example.
$_SERVER['REQUEST_URI']
For more details on what info is available in the $_SERVER array, see the PHP manual page for it.
If you also need the query string (the bit after the ? in a URL), that part is in this variable:
$_SERVER['QUERY_STRING']
If you want just the parts of URL after http://domain.example, try this:
<?php echo $_SERVER['REQUEST_URI']; ?>
If the current URL was http://domain.example/some-slug/some-id, echo will return only /some-slug/some-id.
If you want the full URL, try this:
<?php echo 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>
$uri = $_SERVER['REQUEST_URI'];
This will give you the requested directory and file name. If you use mod_rewrite, this is extremely useful because it tells you what page the user was looking at.
If you need the actual file name, you might want to try either $_SERVER['PHP_SELF'], the magic constant __FILE__, or $_SERVER['SCRIPT_FILENAME']. The latter 2 give you the complete path (from the root of the server), rather than just the root of your website. They are useful for includes and such.
$_SERVER['PHP_SELF'] gives you the file name relative to the root of the website.
$relative_path = $_SERVER['PHP_SELF'];
$complete_path = __FILE__;
$complete_path = $_SERVER['SCRIPT_FILENAME'];
The other answers are correct. However, a quick note: if you're looking to grab the stuff after the ? in a URI, you should use the $_GET[] array.
You can use $_SERVER['HTTP_REFERER'] this will give you whole URL for example:
suppose you want to get url of site name www.example.com then $_SERVER['HTTP_REFERER'] will give you https://www.example.com
Related
Might be an easy question for you, but I'm breaking my head over this one.
I have a php file that needs to know it's current directory url to be able to link to something relative to itself.
For example, currently I know to get the current directory path instead of the url. When I use this I get the path:
realpath(__DIR__)
result:
/Applications/MAMP/htdocs/mysite/dir1/dir2/dir3
But this would be my desired result:
http://localhost:8888/dir1/dir2/dir3
Note that this is not the location of the current page. The page calls a file from "http://localhost:8888/dir1/dir2/dir3/myfile.php"
And "myfile.php" has the script from above.
-- edit to elaborate more details --
Thanks for your answers. But I get that I need to add more detail.
http://localhost:8888/index.php calls: "http://localhost:8888/dir1/dir2/dir3/myfile.php"
"myfile.php" needs to know it's place in the universe :) "Where am I"
"myfile.php" should know it's url location is "http://localhost:8888/dir1/dir2/dir3/"
Use echo $_SERVER['PHP_SELF'];
For example if the URL is http://localhost/~andy/test.php
The output would be:
/~andy/test.php
That's enough to generate a relative URL.
If you want the directory your current script is running in - without the filename - use:
echo dirname($_SERVER['PHP_SELF']);
In the case above that will give you /~andy (without test.php at the end). See http://php.net/manual/en/function.dirname.php
Please note that echo getcwd(); is not what you want, based on your question. That gives you the location on the filesystem/server (not the URL) that your script is running from. The directory the script is located in on the servers filesystem, and the URL, are 2 completely different things.
There is also a function to parse URL's built in to PHP: http://php.net/manual/en/function.parse-url.php
If your URL is like this: https://localhost.com/this/is/a/url
$_SERVER['DOCUMENT_ROOT'] - gives system path [/var/www/html/this/is/a/url]
$_SERVER['PHP_SELF'] - gives the route of the current file (after the domain name) [/this/is/a/url]
$_SERVER['SERVER_NAME'] - gives the domain name [localhost.com]
$_SERVER['HTTP_REFERER'] - gives the correct HTTP(S) protocol and domain name. [https://localhost.com]
If you would like to get the full url, you can do something like:
echo $_SERVER['HTTP_REFERER'] . $_SERVER['PHP_SELF'];
However, I do believe in this case, that all you need is the relative path.. and in that case you should only need to use $_SERVER['PHP_SELF'];
I've found a solution here:
https://stackoverflow.com/a/1240574/7295693
This is the code I'll now be useing:
function get_current_file_url($Protocol='http://') {
return $Protocol.$_SERVER['HTTP_HOST'].str_replace($_SERVER['DOCUMENT_ROOT'], '', realpath(__DIR__));
}
Based on your question, I believe this will get you what your want:
$_SERVER['HTTP_HOST'] . substr($_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], "/"));
Reference:
$_SERVER['HTTP_HOST'] - In your case this would return: http://localhost:8888
$_SERVER['REQUEST_URI'] - In your case this would return: /dir1/dir2/dir3/myfile.php
With the added substr() and strrpos() methods, you can strip the _myfile.php` off of the end to get the desired result:
http://localhost:8888/dir1/dir2/dir3
This question already has answers here:
Get the full URL in PHP
(27 answers)
Closed 5 years ago.
I am starting to build my own MVC using PHP and I am trying to get the current URL in order to create a routing system. I am following a video and the code is as follows. However, it isn't working.
<?php
// phpinfo();
echo "15";
$url = $_GET['url'];
// echo $url;
You are mistaking $_SERVER['REQUEST_URI'] for $_GET['url']
$_GET['URL'] is a get statment .. Meaning you need to pass a variable through the URL IE http://example.com?URL=myurl
To get the current URL .. Simply use echo $_SERVER['REQUEST_URI'];
$_GET will get you the get parameters, not the entire url. Use $_SERVER['REQUEST_URI'] for that.
Let's say I have the page:
index.php?page=page-title-here
I want to get the current page name including the $_GET variable in the URL.
I am currently using this:
basename(__FILE__)
It outputs "index.php", the actual file name. Any idea how to also include the $_GET variable so that it will output "index.php?page=page-title-here"?
The variable $_SERVER["REQUEST_URI"] gives you the file with GET parameters. Also includes folders in the url.
Edit: Use $page = end(explode('/', $_SERVER["REQUEST_URI"])); if you want to get rid of the folders from the url.
You can do so using the REQUEST_URI:
echo $_SERVER['REQUEST_URI'];
From the manual:
REQUEST_URI: The URI which was given in order to access this page; for instance
Try...
$page = (__FILE__) . '?' . $_GET['page'];
Try $_SERVER['REQUEST_URI'] (there are lots of interesting things in $_SERVER)
Use:
basename($_SERVER['REQUEST_URI'])
I was wondering if it is possible to grab the URL from which a page is accessed? For example say if I have a file index.php. It can be accessed from virtually anywhere, depending on where I placed it. For example:
- http://folder1/index.php
- http://nicefolder1/index.php
Is there anyway I can find out where the page was accessed from? I'd like to perhaps parse that URL and if it was from nicefolder1, I'd like to do something like echo "That was a good location to be executed from" :D! Just something I was curious about ...
$_SERVER['PHP_SELF']
Should have the url you want.
More Info
I am not sure if I understand your question, but here's my take on this:
$_SERVER['HTTP_HOST'] holds the host name, and $_SERVER['REQUEST_URI'] the absolute path to the requested resource. These two, combined with a scheme prefix, gives you the complete URL:
$url = 'http://'. $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
Take a look at the $_SERVER reference on php.net, $_SERVER['PHP_SELF'] should do the trick.
How to get current page URL and title in WordPress?
Here are few super global variables for getting url info:
$_SERVER['PHP_SELF'];
$_SERVER['REQUEST_URI'];
$_SERVER['SCRIPT_NAME']
$_SERVER['SCRIPT_NAME']
$_SERVER['SCRIPT_NAME'] will be same - /index.php. It is irrespective of the actual URI ($_SERVER['REQUEST_URI']) used to access the site.
As it returns the actual script name, it fails provide additional path information that may be present. So if the $_SERVER['REQUEST_URI'] is /index.php/big/directory/ then too the $_SERVER['SCRIPT_NAME'] will be same - /index.php.
$_SERVER['SCRIPT_NAME'] is supported on all platforms
$_SERVER['PHP_SELF']
This is the filename of the currently executing script, relative to the document root. However, unlike $_SERVER['SCRIPT_NAME'], it provides additional path information like $_SERVER['REQUEST_URI'] when the actual php file is present in the path. So when the $_SERVER['REQUEST_URI'] is /index.php/big/directory/ then $_SERVER['PHP_SELF'] will be /index.php/big/directory/.
However if all the URI's under http://www.example.com/ is mapped to http://www.example.com/index.php, then, for example, http://www.example.com/abc/def will return /index.php like $_SERVER['SCRIPT_NAME']. Note that $_SERVER['REQUEST_URI'] data is ignored for this request.
$_SERVER['REQUEST_URI']
It will give you entire url including query string vars.
Title of the page:
There is no built-in functionality to get title of the page, however, you can use the HTML Simple DOM to read the contents of <h2.
Example:
$title = str_get_html('<title></title>');
Now, you can use $title however, you like. Visit the their site for more info about it.
If you are using wordpress, and you want to print the post title you can use,
<?php echo get_the_title(); ?>
documentation here: http://codex.wordpress.org/get_the_title
you can get the URL by
'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']
or
$_SERVER['SCRIPT_URI']
might work on some platforms.
the title is something that the PHP program gives back to the browser via HTML:
<html><head><title>title is here</title> ...
so it is not something that you "get" unless you get something from the DB and send back to the browser.
Update... do you mean the title of page that leads to your php file? (the user clicks on that page to get to your php file.) in that case, you can use $_SERVER['HTTP_REFERER'] but it is not guaranteed to contain any data (but most of the time it will).