How can I get current page name including $_GET variable in URL? - php

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'])

Related

How to get current page URL in Drupal?

I'm trying to output the current page URL on a Drupal site (Drupal 7), I've tried the following...
<?php print current_path(); ?>
Which outputs the page name but not the URL, any ideas on how to output the full URL too?
In straight PHP you can use this:
$curPathName = $_SERVER["DOCUMENT_ROOT"];
$currentURL = $_SERVER["HTTP_HOST"];
I don't see why that wouldn't work. That will give you the path and the URL.
You can use
<?php
global $base_url;
print $base_url . '/' . current_path();
?>
If you want to keep things done "the drupal way", you can use current_path() or request_path().
https://api.drupal.org/api/drupal/includes!path.inc/function/current_path/7
https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/request_path/7
You can also use $base_url instead of relying on $_SERVER.
The solution durbpoisn gave will give you the URL in the browser. If that is an alias, you can use drupal_get_normal_path() to get the internal path
https://api.drupal.org/api/drupal/includes!path.inc/function/drupal_get_normal_path/7
in drupal9
$current_path = \Drupal::service('path.current')->getPath();
$path_alias = \Drupal::service('path_alias.manager')->getAliasByPath($current_path);
On D7 if you want to get the current path you can use:
$path = drupal_get_path_alias();
So if your current URL is:
www.test.com/hello/world
You will get:
'hello/world'

how to take the link with its variables from the page

how can i take the link of the page that i am on with its variables ?
example i have the page link is
article.php?article_id=10&article_title=title&lang=ar
when i use the $_SERVER['SCRIPT_NAME'] variable it takes only article.php
and im rewriting the url as well so it looks like this
article/10/title/ar
what i want to do is just make a link that is to an English page so im trying to make it look like this
article/10/title/en
how can i do that?
Since the data looks like its passed with a HTTP GET method, you can use this
$_GET["lang"];
This returns the value assigned to "lang"
$_SERVER['QUERY_STRING'] will have all the parameters. You can also check $_SERVER['REQUEST_URI'] and that should contain the whole url, file and parameters.
Something like:
$params = $_GET;
$params['lang'] = 'en';
$link = basename($_SERVER['SCRIPT_NAME']) . implode('/', $params);

$_GET reads url as string and not as an array

My $_GET is reading the URL as a string and does not separate different GET variables.
My URL is:
www.mysite.com/index.php?cat=archive?page=2
Using $_GET I'd like to use the variables $cat and $page.
echo $_GET['cat'];
Returns: archive?page=2.
echo $_GET['page'];
Returns: nothing.
What am I doing wrong here? Should I manually separate the URL into variables?
the '?' is only used once in the url. you should use & to separate each variable.
your URL should be www.mysite.com/index.php?cat=archive&page=2
and then you can echo on cat and page
url in not correct form where you are made it
www.mysite.com/index.php?cat=archive?page=2
correct it with
www.mysite.com/index.php?cat=archive&page=2

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