I am using $_SERVER["REQUEST_URI"] to get the current url. Then I am passing that url to another page via href.
echo "<a href='second.php?url=".$_SERVER["REQUEST_URI"]."'>Click here</a>";
//the url in this case is index.php?tit=most+wanted&id=23&c_id=11&ran=378834GSF844
Then on my second page when I do the below
$mc = $_GET['url'];
echo $mc;
I only get /index.php?tit=most+wanted
What happened to other three parameters? is it possible also to get rid of the slash on the front?
$_GET and $_POST are for single parameters only. $_SERVER['QUERY_STRING'] grabs the entire URL query.
Try to see the values of $_SERVER.
var_dump($_SERVER);
You can see the values and the suitable key you want.
I suggest you use $_SERVER["QUERY_STRING"] instead of $_SERVER["REQUEST_URI"].
About the results you have right now, its because of this character "&".
It acts as the delimiter and the next character from that point to the following character "=" will be the a key from $_GET variable and after that is the value.
i think these what you have right now:
$_GET["url"] = index.php?tit=most+wanted
$_GET["id"] = 23
$_GET["c_id"] = 11
$_GET["ran"] = 378834GSF844
Try using var_dump function to simply see the whole $_GET values.
var_dump($_GET);
Related
I've had a look around and can't find an answer specific to my needs...
This is the URL being sent back to my redirect page from an identity server: http://localhost/?code=92092c2f65560d835a73cda852393316&state=random_state&session_state=CEwifhoy_x1RJWH-DoV_9Q1yJVNUXV4z5YRGGuXsUvs.39c667f469b8f6585a9f699d0b7d76f3
I need to get the code between "code=" and "&state".
Then put it in the <> part of "grant_type=authorization_code&code=<>&redirect_uri=<>" so I can request a token. This part I can do, I just can't figure out how to get the code out of the URL.
Thanks.
You should just use $_GET environment variable.
All these data after the file address itself (in your case /) and ? sign are GET data.
To retrieve "code" value you should use:
$variable = $_GET['code'];
It's just as simple as that.
For this you can use the $_GET variable. It will become more clear for you if you do:
<?php
print_r($_GET);
?>
Then you can see there is a key "code" in this array. You can get the value of this key like this:
<?php
$code = $_GET['code'];
?>
Use parse_url with a component of PHP_URL_QUERY to get the query string.
Then parse_str to get the particular argument
http://php.net/manual/en/function.parse-url.php
http://php.net/manual/en/function.parse-str.php
I need to extract a variable's value from a string, which happens to be a URL. The string/url is loaded as part of a separate php query, not the url in the browser.
The url's will look like:
index.php?option=com_content&view=article&catid=334:golfeq&id=2773:xcelsiors&Itemid=44
How can I always find & capture the value of the id which in this example is 2773?
I read several examples already, but what I've tried captures the id value of the current page which is being viewed in the browser, and not the URL string.
Thanks
You are looking for a combination or parse_url (which will isolate the query string for you) and parse_str (which will parse the variables and put them into an array).
For example:
$url = 'index.php?option=com_content&view=article&catid=334:golfeq&id=2773:xcelsiors&Itemid=44';
// This parses the url for the query string, and parses the vars from that
// into the array $vars (which is created on the spot).
parse_str(parse_url($url, PHP_URL_QUERY), $vars);
print_r($vars); // see what's in there
// Parse the value "2773:xcelsiors" to isolate the number
$id = reset(explode(':', $vars['id']));
// This will also work:
$id = intval($vars['id']);
echo "The id is $id\n";
See it in action.
You can use parse_str
You can use parse_url to parse URLs!
But you can use, to extract directly the numbers of ID variable:
$url = 'index.php?option=com_content&view=article&catid=334:golfeq&id=2773:xcelsiors&Itemid=44';
$id = preg_replace("/^.*[&?]id=([0-9]+).*$/",'$1',$url);
echo $id;
for example the link is:
http://www.test.com/abc.php?config.scp
Is it possible to get the value "config.scp" in the php program? Thanks!
That data is contained in $_SERVER['QUERY_STRING']
If you want a simple string, use $_SERVER['QUERY_STRING'].
If you still need an array with rest of the variables, use $_GET. If you var_dump( $_GET ) on link you provided, you should get:
array(1) {
["config_scp"]=>
string(0) ""
}
You can easily parse it now.
There's one gotcha with dot in that particular query string tho. PHP variables cannot contain dots, so it's changed into _.
Yes... in that case data is field name but be aware that dot is not allowed in $_GET array index.
Also, you can explode $_SERVER['QUERY_STRING'] with & character and look into resulting array for element's value you need.
Hy
See this example:
$url = 'http://www.test.com/abc.php?config.scp';
$p_url = end(explode('?', $url));
echo $p_url;
I've this url:
http://localhost/alignment/?page_id=52&q=2
I want to get the portion: ?page_id=52 , how can I do that?
$_SERVER['QUERY_STRING']
or
$url = "http://localhost/alignment/?page_id=52&q=2";
$bits = explode("?", $url);
$querystring = $bits[1]; // this is what you want
but the first one will be much more reliable and is easier. :)
EDIT
if you meant that you just wanted that one variable use:
$_GET['page_id']
This is called a query string. The main portion of the query string is separated by the rest of the URL with a ?. Each argument in the query string is separated by a &.
PHP:
$_SERVER['QUERY_STRING'];
If you want to get the individual pieces, use:
$_GET['page_id']; //etc...
You can get the whole query string with $_SERVER['QUERY_STRING'], but you would have to parse out page_id part. If you insist on doing things manually the function parse_str may come in handy.
A better choice would be to just use the predefined $_GET global variable. $_GET['page_id'] would give you value 52.
If you have it as a string, use parse_url. Documentation here. Get the query value of it. or if its current request use $_SERVER['QUERY_STRING']
echo parse_url($url,PHP_URL_QUERY);
This should do it:
echo $_SERVER['QUERY_STRING'];
Assign the url to string and then explode() it,
http://php.net/manual/en/function.explode.php, using the ? as a delimiter
I have done urlencode of the variable before passing to the URL
http://example.com/Restaurants?alias=F%26B
But when I try to print like in the page
$alias = rawurldecode($_GET['alias']);
echo $alias;
it prints only F. How to solve this?
I doubt that $_GET['alias'] exists when requesting a URL with the query aliasF%26B. It’s rather $_GET['aliasF&B'] that’s getting populated.
In this case you need to use $_SERVER['QUERY_STRING'] to get the full query.
It looks like you are not using the query string "correctly." It should be in key=value pairs. I would look at using $_SERVER['QUERY_STRING'] to get your information instead.
You don't need to urlencode the pair. You only need to urlencode name and a value as such:
Wrong:
urlencode('aliasF=B')
Correct:
urlencode('aliasF') . '=' . urlencode('B')
AFAIK $_GET are already decoded.
See php.net
The superglobals $_GET and $_REQUEST
are already decoded. Using urldecode()
on an element in $_GET or $_REQUEST
could have unexpected and dangerous
results.
It is possible to solve this problem by using a different encoding system specific for your situation:
function encode($string)
{
$result = str_replace("|","||",$string);
return str_replace("&","|20",$result);
}
function decode($string)
{
$result = str_replace("|20","&",$string);
return str_replace("||","|",$result);
}
This will basically create a separate escaping system using the '|' character. That character can be anything you normally don't use and isn't an field separator.
Here, Apache won't transform the URL to something different, thus voiding the conversion. Also browsers won't transform it.
Mind that you would decode($_GET['alias']) and encode() the url that the user is pressing or the script is following.