Right now I'm printing the current URL I have and it kinda does it ok:
$currentURL = basename($_SERVER['PHP_SELF']);
The problem is that it only prints
solutions.php
instead of printing
solutions.php?id=581298a7-6c08-11e3-9bea-742f689f29f1
Anybody knows why this is happening? I need to get to print the part of
?id=581298a7-6c08-11e3-9bea-742f689f29f1
Because then I will be performing a substring to get just the ID.
Use parse_url, which parses a URL and returns its various components in a usable array. You'll also want to make sure you're parsing the correct string; in this case use:
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
Full usage:
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
print_r(parse_url($url));
This is expected behavior, because everything from the ? afterwards are not part of the filename. The parameters that you're looking for are in the $_GET superglobal.
Related
I'd like to echo the depth (or number of directories from my home) of my current page's URL using PHP. How would I do that?
For example, if I'm on mysite.com, the output displays "0", if I'm on mysite.com/recipes, the output displays "1", and if I'm on mysite.com/recipes/pies, the output displays "2", and so on.
How do I do that?
I tried simplifying it and doing this, but it's exporting as 0:
$folder_depth = substr_count($_SERVER["PHP_SELF"] , "/");
echo $folder_depth;
Just for fun, here is my cheap and cheezy solution using PHP's parse_url() and its PHP_URL_PATH return value along with a couple of other functions:
$url = 'http://universeofscifi.com/content/tagged/model/battlestar_galactica.html';
echo var_dump(parse_url($url, PHP_URL_PATH));
echo count(explode('/', (parse_url($url, PHP_URL_PATH)))) - 2;
This returns:
string(47) "/content/tagged/model/battlestar_galactica.html"
3
I subtract 2 from the count to discard the domain at the front and the file at the end, leaving only the directory depth count.
If you won't have a query string, you can explode on /. If you will have a query string, you need to remove that first, such as...
$url = preg_replace('/?.*$/','',$url);
If you have http:// or https:// at the front of your URL, that can mess it up also. So remove it...
$url = preg_replace('~^https*://~','',$url);
Now, you only have the url as example.com/some/path/to/something. You can explode on / and get a count:
$a = explode('/',$url);
The size of $a will be 1 more than what you want. So, you need to subtract one:
$depth = sizeof($a)-1;
New problem... I just counted the file itself, such as example.com/links.html will come up as 1, not just 0. So, before the explode I need to get rid of the file name. But... how do I know if it is a file or a directory? That isn't built into the URL specification. For example, example.com/test could be a file or it could be a directory (and then it automatically goes to example.com/test/index.html). You need to assume what file extensions you will have and remove those files before you explode, such as:
$url = preg_replace('~/[^/]+.(html|php|gig|png|mp3)$~','',$url);
#kainaw, I like your answer! Thanks!
I took a spin on that. First, I noticed I was using the wrong PHP function to get the part of the URL I needed. Second, I needed to use #kaniaw's example and get the parts of the URL which I'm supposed to count, and ignore the others.
I also had to account for urls without content between the "/", so something like /word//// would still count as 1. Therefore, I only counted array elements after explode() which were not empty.
Here's my code:
$url = $_SERVER['REQUEST_URI'];
//echo "*".$_SERVER['REQUEST_URI']."*";
//$url = preg_replace('/?.*$/','',$url);
//$url = preg_replace('~^https*://~','',$url);
//$url = preg_replace('~/[^/]+.(html|php|gig|png|mp3)$~','',$url);
$a = explode('/',$url);
$depth =count(array_filter($a));
echo $depth;
I commented out some of those lines because I didn't seen them, but they were mentioned above.
Thanks!
I have a link. Eg. abc.com/qwerty. I want to extract the part after / of every input just like examples below and use it just like a PHP GET input value and store it to a variable $page. Essentially, the link abc.com/qwerty should work like abc.com/proc.php?x=qwerty
Typed link Part to be used as PHP GET input
abc.com/cvbx cvbx
abc.com/ghvs ghvx
abc.com/pabc pabc
How can I do this?
You can use: $_SERVER['REQUEST_URI']
$request_uri = $_SERVER['REQUEST_URI']; //returns '/cvbx'
$segments = array_filter(explode('/', $request_uri)); //array_filter to remove empty elements.
You can parse the url as described at http://php.net/manual/en/function.parse-url.php .
If you have urls as described there, you can get the path part with:
$input = substr(parse_url($url, PHP_URL_PATH),1);
substr is to remove the starting /
I have search high and low for an answer to my question and I cannot find it. Basically what I want to do is get the path after a php script. ex. "http://www.example.com/index.php/arg1/arg2/arg3/etc/" and get arg1, arg2, arg3, etc in an array. How can I do this in php and once I do this will "http://www.example.com/arg1/arg2/arg3/etc" still return the same results. If not then how can I achieve this?
Here is how to get the answer, and a few others you will have in the future. Make a script, e.g. "test.php", and just put this one line in it:
<?php phpinfo();
Then browse to it with http://www.example.com/test.php/one/two/three/etc
Look through the $_SERVER values for the one that matches what you are after.
I see the answer you want in this case in $_SERVER["PATH_INFO"]: "/one/two/three/etc"
Then use explode to turn it into an array:
print_r( explode('/',$_SERVER['PATH_INFO'] );
However sometimes $_SERVER["REQUEST_URI"] is going to be the one you want, especially if using URL rewriting.
UPDATE:
Responding to comment, here is what I'd do:
$args = explode('/',$_SERVER['REQUEST_URI']);
if($args[0] == 'index.php')array_shift($args);
Try like this :
$url ="http://www.example.com/index.php/arg1/arg2/arg3/etc/";
$array = explode("/",parse_url($url, PHP_URL_PATH));
if (in_array('index.php', $array))
{
unset($array[array_search('index.php',$array)]);
}
print_r(array_values(array_filter($array)));
Can you try using parse_url and parse_str
$url =$_SERVER['REQUEST_URI'];
$ParseUrl = parse_url($url);
print_r($ParseUrl);
$arr = parse_str($ParseUrl['query']);
print_r($arr);
Following returns empty array, so on my localhost $_SERVER[] doesn't return anything. Why?
Url in browser looks like this: localhost/final/events/2012-10/
$current_url_all = parse_url($_SERVER['PATH_INFO']);
print_r($current_url_all);
What am I doing wrong? How else can I grab that last date piece from url?
Try to use $_SERVER["REQUEST_URI"] instad, $_SERVER['PATH_INFO'] seems to be just aviable if you invoke the script like a directory:
http://example.org/script.php/foo
You might want to use
$_SERVER['REQUEST_URI']
u can manipulate the string. use strpos to get the position of localhost/final/events/ then substring to get the rest.
also u can use $end = end((explode('/', $url)));
then get the last value from the array
For example, if there is a url like www.website.com/hello/richard, would it be possible to echo hello and 100 separately onto my page.
eg:
hello how are you today richard
You can get the data from $_SERVER['REQUEST_URI'] and then do whatever you like with it.
Yes it would be. Try this:
$myURL = $_SERVER['REQUEST_URI'];
$myTokens = explode('/', $myURL);
echo $myTokens[1] . "blah" . $myTokens[2];
This code gets the current URL into the myURL variable, then it calls a function called explode which turns it into an array based on the position of the '\' character. Then it echos out certain elements of that array. If you play around with output using echo you will soon see for yourself what is going on.
Sure that's possible. You can get URL as a string using $_SERVER['request_uri]. Then you might want to use explode function to firm array of strings where delimiter is /. Then you may parse it. Or you can do this via .htaccess using rewrite rule