I have an URL like so:
http://local.mywebsite.com/index.php?p=edit_entry?id=3
and I want to retrieve only the p argument which value in this case above is "edit
_entry".
I have written:
$p = $_GET['p'];
echo $p;
But when I do so I get the value edit_entry?id=3
What should I do? I just want to retrieve edit_entry
That's an invalid URL. ? signifies the START of the query parameters, it's not a seperator BETWEEN parameters. You use & for that:
http://local.mywebsite.com/index.php?p=edit_entry&id=3
^---
Because the url should be:
http://local.mywebsite.com/index.php?p=edit_entry&id=3
^ ampersand
It should be & instead of ?
? is the delimiter which separates the URL from the PARAMETERS.
The parameters are separated by an ampersand &.
So you have to use index.php?param1=something¶m2=something&etc=etc
Related
I'm passing simple query string:
index.php?a=orange&apple
Getting value with:
$_GET['a'];
it only shows orange value. i have tried
$a = urlencode($_GET['a']);
$rs = urldecode($a);
echo $rs; // orange;
but didn't work. Found similar question here stackoverflow but seems not useful. How do i get complete value orange&apple?
That is because & makes apple as another value in GET request.
Like this -
/test/demo_form.php?name1=value1&name2=value2
So use '%26'(encoding URI Component) in place of & like this -
index.php?a=orange%26apple
And get the value with $_GET['a'];. This should work.
Your query string need to encode ampersand(&) as percent-encode as %26. Your url will be like :
index.php?a=orange%26apple
Write it like this:
index.php?a=orange&b=apple
// Values
$_GET['a'];
$_GET['b'];
i'm trying to inject an extra param at the end of the query string of a url being passed as a value.
i have tried
<?PHP preg_replace('/(http:\/\/www.site.com)/i','$1',$_GET['url']."&EXTRA-PARAM");
but sometimes URL does not contain any query string. how can i make it start with ? instead of & if there is no query string presant
The preg_replace you have in your question has no effect:
preg_replace('/(http:\/\/www.site.com)/i','$1',$_GET['url']."&EXTRA-PARAM");
Whatever the last argument is, it will be returned by that preg_replace call. Either you don't have a match and nothing is replaced, or you have a match, but then you replace what you found with... what you found. So in both cases you get the same result.
$1 is a back-reference to http:\/\/www.site.com, so actually the quoted statement is equivalent to this:
preg_replace('/http:\/\/www.site.com/i','http:\/\/www.site.com',
$_GET['url']."&EXTRA-PARAM");
Your final value is always:
$_GET['url']."&EXTRA-PARAM"
So, you could just have used that, and skip the preg_replace.
What I answer here is how to better write that part ($_GET['url']."&EXTRA-PARAM"), i.e. the question: "inserting an extra param at the end of the URL".
This concatenation can lead to missing ?. Worse, it will not lead to a functional parameter if $_GET['url'] has a hash (#).
Here is a solution for that with preg_replace_callback. It takes into account that a given URL may or may not have:
a hash (#) component;
a question mark ?
some query after the ?
some & to separate query parts
The function accepts as second argument what needs to be added to the query part of the provided URL, which may be a string (must be URL encoded), or an associative array with items to add to the URL. This array will then be fed to the function http_build_query:
function addUrlQuery($url, $qry) {
if (!is_string($qry)) $qry = http_build_query($qry);
return preg_replace_callback("/^(.*?)(\?(.*?))?(#.*)?$/",
function ($matches) use ($qry) {
return $matches[1] . "?" .
(empty($matches[3]) ? "" : $matches[3] . "&") .
$qry .
(empty($matches[4]) ? "" : $matches[4]);
}, $url);
}
Example use:
$url = addUrlQuery($_GET['url'], "EXTRA-PARAM");
echo addUrlQuery("http://myserver.com?x#title", ["EXTRA-PARAM" => 1]);
echo addUrlQuery("http://myserver.com/" . $relative_url, "EXTRA-PARAM");
You can see the function run on several test cases on eval.in.
The problem with $_GET['url']
If you have a URL passed as a query argument to the current URL, like this:
http://myserver.com/index.php?url=http://test.com/index.php?a=1
Then you must realise this is not a good URL. Some characters need to be escaped. Certainly the ampersand (&) needs to be escaped. If you have this:
http://myserver.com/index.php?url=http://test.com/index.php?a=1&b=2
Ask yourself: is b a query parameter of the current URL, or of the URL passed as url argument?
The answer: it is a query parameter of the current URL. You would access it with $_GET['b']. And the value of $_GET['url'] is now just:
http://test.com/index.php?a=1
So... you need to escape & when you want b to be a query argument of the url parameter, and not of the current query:
http://myserver.com/index.php?url=http://test.com/index.php?a=1%26b=2
Note the %26: it is the code for &. Now if you look at $_GET['url'] you will get:
http://test.com/index.php?a=1&b=2
What if URL is provided without proper encoding?
In this case, you have to make assumptions. If you know for sure that the url parameter is the only parameter you will get on the current URL, then you can conclude that any other you get, really belong to the URL that is passed via that url parameter.
You would then take all the parameters from $_GET, except url, and add them (with the function above) to the value of $_GET['url'].
How to replace a query string like
page=3&searchform=1&cityS=-1&category=24& to page=[0-9] ?
I'm using the following
preg_replace('/page=d*&?/','',$_SERVER['QUERY_STRING']);
but in the end, &3 appears.
You can use:
preg_replace('/^.*?(\bpage=[0-9]*)&.*$/','$1', $_SERVER['QUERY_STRING']);
Or else you can simply use
$_GET['page']
To get this query parameter value.
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);
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