url of the current page without page name and query string - php

As mentioned in the question, I want to get the url of the page without the page name and query string.
For example my url is:
http://sub.domain.com/app/page.php?var=abc
what I want from this is:
http://sub.domain.com/app/
without the query string and page name.
I found this tutorials:
http://www.phpf1.com/tutorial/get-current-page-url.html
it was helpful but not exactly what I want.
Thanks for your answers.

The dirname(__FILE__) is not the same as happyhardik's goal.
substr($_SERVER['PHP_SELF'], 0, strrpos($_SERVER['PHP_SELF'], '/'))
returns: /wwwroot/activetest2
dirname(__FILE__)
returns: /home/content/94/3671394/html/wwwroot/activetest2
To get the full URL:
$_SERVER['SERVER_NAME'].substr($_SERVER['PHP_SELF'], 0, strrpos($_SERVER['PHP_SELF'], '/'))
returns: www.domainname.com/wwwroot/activetest2

I suggest the following trick:
$location = dirname(__FILE__);
If you need an absolute URL, add the following in front of it:
$protocol = 'http'.(!empty($_SERVER['HTTPS']) ? 's' : '');
$root = $protocol.'://'.$_SERVER['SERVER_NAME'];

The server vars don't hold only the path without the page name.
So you need to strip the page name from PHP_SELF (everything after the last / should do the trick)

you can use HTACCESS for this problem. so by using htaccess your URL can be changed.
Thanks.

Exactly answer that you are looking for is
$protocol = 'http'.(!empty($_SERVER['HTTPS']) ? 's' : '');
$currURL = $protocol.'://'.$_SERVER['SERVER_NAME'].substr($_SERVER['PHP_SELF'], 0, strrpos($_SERVER['PHP_SELF'], '/'));
=======================================Explanation
For Example your current url is
http://sub.domain.com/app/page.php?var=abc
it will return
http://sub.domain.com/app

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'

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

Get real base URL

You can get a base URL with $_SERVER["SERVER_NAME"], but sometimes if you have different projects in the same website, you need to save them in different directories. For example, if you have /var/www/myproject/, the real base URL is http://yourhost.com/myproject/.
If you try with $_SERVER["SERVER_NAME"] + $_SERVER["REQUEST_URI"], it gives the full request URL, and the problem becomes worse when you use friendly URLs. For example, if you use it in something like http://yourhost.com/myproject/controller/action/ you can't get the real base URL (yourhost.com/myproject).
How can I get the real base URL in these situations?
If I understood you right, you may need this..
$uri = isset($_SERVER['REQUEST_URI']) ? strip_tags($_SERVER['REQUEST_URI']) : '';
$urlvariables = (substr($uri, 0, 1) == '/') ? substr($uri, 1) : $uri;
$variables = explode('/', $uri);
echo $whatyouneed = $_SERVER['HTTP_HOST'] . '/' . $variables['1'];
You may give it a try.
Define it. If you have any sort of global project configuration at all (you should), you can set it up there. That’s more or less how CodeIgniter (for example) does it.
define('BASE_URL', '/myproject/'); # Include the host name if you truly must
To calculate it, put this code in your project root:
define('BASE_PATH', str_replace('\\', '/', __DIR__));
define('BASE_URL', preg_replace('|/+|', '/', strtolower(preg_replace('|^'.str_replace('\\', '/', realpath($_SERVER['DOCUMENT_ROOT'])).'|i', '', BASE_PATH))));

How to detect if user go to a link look like our rule?

example:
I have a webpage abc.com/index.php
I want to detect if user connect to abc.com/index.php/bbdh,
abc.com/index.php/bb/saggfd/gjasgd/hsahgd, abc.com/index.php/bb/saggfd/gjas and so on without having a page like that on my host.
I'm using php and need some way to do that without using .htaccess file.
Thanks for any help!
have a look at $_SERVER['REQUEST_URI'], every thing you need will be accessible here. E.g. for:
abc.com/index.php/bb/saggfd/gjas
it would be:
/index.php/bb/saggfd/gjas
strip the /index.php with something like:
echo substr($_SERVER['REQUEST_URI'], strlen($_SERVER['SCRIPT_NAME']));
and you're set.
Update on how to handle GET-parameters:
// get the full request uri
$requestUri = substr($_SERVER['REQUEST_URI'], strlen($_SERVER['SCRIPT_NAME']));
// let php parse that url (though it's just a part of the full url)
$chunks = parse_url($requestUri);
// let php parse the query string
parse_str(isset($chunks['query']) ? $chunks['query'] : '', $chunks['query']);
// debug output
print_r($chunks);

PHP - Get string from URL

by having an URL like this: mysite.com/subfolder/helloworld - Is it possible to read the "helloworld" from within a PHP page?
I would like to use the string as a part to load some content.
end( explode( '/', $_SERVER['REQUEST_URI'] ) )
without the end() call it will give you all the parts of the URL
You should read about URL rewriting.
This is more clean than "reading" the current URL. Basically you can redirect your URL (transparently) to something like :
mysite.com/index.php?folder=subfolder&category=helloworld
Then in PHP, you can access the URL parameter with :
$folder = $_GET['subfolder']
$category = $_GET['category']
This is maybe not the kind of answer you were expecting, but it can be interesting to know.
The request url (everything from the first / after the domain name) can be found in $_SERVER["REQUEST_URI"]
Hello have u used 'strstr' keyword of php?
please try like this
if(strstr($_SERVER["REQUEST_URI"], "helloworld"))
return true;
else
return false;
May be this will be helpful to you.

Categories