Get current page URL and title in WordPress? - php

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).

Related

php get URL of current file directory

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

$_SERVER['REQUEST_URI'] returns PHP file not URL

Below are the relevant lines of code:
update.js
$('#tablePlaceholder').load('updateTable.php');
updateTable.php
$path = $_SERVER['REQUEST_URI'];
Normally $_SERVER['REQUEST_URI'] returns the URL, however in this case, the URI at the moment of the request is technically the PHP file, so what is returned is updateTable.php instead of the actual URL. Is there an alternate command I can use to get the URL instead of URI? I have resorted to storing the URL in a session/global variable to retain access to the actual URL in the PHP file, but it seems there must be a better way, though I have not found it. Any tips would be most welcome!
I was looking at the documentation for the jQuery load() command:
http://api.jquery.com/load/
It looks like I might be able to pass the URL as a string via the data argument?
$_SERVER['REQUEST_URI'] is supposed to give path of current script relative to root and not the full url. If you want to get full url path, take a look at the same question on stackoverflow with the solution also being there
Getting the full URL of the current page (PHP)

How to check address a page is accessed from?

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 the current full url (url + fragments) of the webpage in PHP?

I want to fetch the current URL which I've received as a callback during authorization from Salesforce.com. The data in the url is in the fragments portion.
You can use a combination of $_SERVER['HTTP_HOST'] and $_SERVER['REQUEST_URI'] but HTTP_HOST is not very reliable so try defining URL's for known sources and appending the request uri to the end.
define('URI', 'http://Salesforce.com');
echo URI. $_SERVER['REQUEST_URI'];
You will not be able to get the fragment portion of the URI back as it is used on the client only. If you need this you will manually have to send them to a server with some JavaScript.
With the $_SERVER variable, you could do this:
echo $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
Take a look at the following page for all available keys: php.net
I belive its something like $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING'];
http://php.net/manual/en/reserved.variables.server.php

How to get URL of current page in PHP [duplicate]

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

Categories