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
Related
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'
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'])
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);
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.
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