How to get current page URL in Drupal? - php

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'

Related

Get current name for path in url

How can i grab everything that is after http://www.domain.com/somefolder/ in php.For e.g if http://www.domain.com/somefolder/login is accessed , i just need the login part.
I tried the following
basename($_SERVER['PHP_SELF']); and basename(__FILE__); both gives me index.php
You can do it using strlen and substr functions:
<?php
$url = 'http://www.domain.com/somefolder/login';
$remove = 'http://www.domain.com/somefolder/';
echo substr($url, strlen($remove));
EDIT And you should make sure that your urls always start with www. subdomain. Otherwise you can get unexpected results.
I found the answer
$_SERVER['REQUEST_URI'] was what i need
P.S Ty for the downvotes

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

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

links not working on web server

i have developed a php application which is running perfect on local server. when i deployed the application on web server the links are not working
1) my site is "abc.myapplication.com" (abc is subdomain)
i defined following variable in config file
define('ROOT_PATH', $_SERVER['DOCUMENT_ROOT']);
ROOT_PATH variable shows /home/punjabfo/public_html/abc (which is perfect)
for link i used following code
Add Record
link should go to "abc.myapplication.com/addrecord.php" but link go to
"abc.myapplication.com/home/punjabfo/public_html/abcaddrecord.php"
i tried a lot but could not fin the issue. please help. thanks
What is wrong with Keeping It Simple
Add Record
Let the server do all the work, as it gets it right and you do less messing around.
Try
define('ROOT_PATH', $_SERVER['HTTP_HOST']);
Just use
Add Record
You can try -
$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo 'http://'.parse_url($url, PHP_URL_HOST) . '/';
Your issue is "$_SERVER['DOCUMENT_ROOT']". $_SERVER['DOCUMENT_ROOT'] stands for the root directory on the server (dir-path). What you need, is the URL and not the file-system-path.
Take a look on
<?php
echo "<pre>";
var_dump($_SERVER);
echo "</pre>";
?>
Why not to do
Add Record
Of course you do not need ROOT_PATH in the URL. What you do is returning full path of the file, instead of link. And btw, full path is incorrect itself, as you forgot slash before addrecord.php.

Test if on a certain page with php?

Using PHP, is there a way to test if the browser is accessing a certain page?
For example, I have a header file called header.php which is being pulled into a couple different pages. What I want to do is when I go to a different page, I want to append certain variable to the title.
Example.
Inside header.php:
<?php
$titleA = " Online Instruction";
$title B = "Offline";
?>
<h2>Copyright Info: <?php if ('onlineinstruction'.php) echo $titleA; ?> </h2>
edit: also if you believe there is a simpler way to do this, let me know!
You can use $_SERVER['REQUEST_URI'], $_SERVER['PHP_SELF'], or __FILE__ depending on your version of PHP and how you have your code setup. If you are in a framework it may have a much more developer-friendly function available. For example, CodeIgniter has a function called current_url()
Per PHP Docs:
$_SERVER['REQUEST_URI']: The URI which was given in order to access
this page; for instance, '/index.html'.
$_SERVER['PHP_SELF']: The filename of the currently executing script,
relative to the document root. For instance, $_SERVER['PHP_SELF'] in a
script at the address http://example.com/test.php/foo.bar would be
/test.php/foo.bar. The __ FILE__ constant contains the full path and
filename of the current (i.e. included) file. If PHP is running as a
command-line processor this variable contains the script name since
PHP 4.3.0. Previously it was not available.
<?php
$url = $_SERVER["REQUEST_URI"];
$pos = strrpos($url, "hello.php");
if($pos != false) {
echo "found it at " . $pos;
}
?>
http://www.php.net/manual/en/reserved.variables.server.php
http://php.net/manual/en/function.strrpos.php
You can use this variable to find out what page you're on:
$_SERVER['PHP_SELF']
http://www.php.net/manual/en/reserved.variables.server.php
read this: http://php.net/manual/en/language.constants.predefined.php
Any php page can include the following code, putting the applicable page title in the variable.
<?php
//Set Page Title for header template
$page_title = 'Welcome';
require_once("templates/header.php");
?>
In the header template:
<title><?php echo $pageTitle ?></title>
Any page called up will show your preferred title in the browser tab, and be usable as a variable on the specific page.

Get Full URL with PHP & Include Subdomain

I have been using the following to discover the full url, but it does not detect the subdomain I am calling from.
$current_url = "http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]."";
echo $current_url;EXIT;
Any advice?
$_SERVER['HTTP_HOST'] should include the subdomain. Can you link us to an example page?
Tested and works fine for me. Used this code:
<?
$current_url = "http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]."";
echo $current_url;EXIT;
?>

Categories