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'];
Related
I have a URL like this:
abc.com/my+string
When I get the parameter, it obviously it replaces the + with a space, so I get my string
I replaced the + in the url with %2B, then I use rawurldecode(), but the result is the same. Tried with urldecode() but I still can't get the plus sign in my variable, it's always an empty space.
Am I missing something, how do I get exactly my+string in PHP from the url abc.com/my%2Bstring ?
Thank you
In general, you don’t need to URL-decode GET parameter values manually, since PHP already does that for you, automatically. abc.com?var=my%2Bstring -> $_GET['var'] will contain my+string
The problem here was that URL rewriting was in play. As http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_b explains,
mod_rewrite has to unescape URLs before mapping them, so backreferences will be unescaped at the time they are applied.
So mod_rewrite has decoded my%2Bstring to my+string, and when you rewrite this as a query string parameter, you effectively get ?var=my+string. And when PHP applies automatic URL decoding on that value, the + will become a simple space.
The [B] flag exists to make mod_rewrite re-encode the value again.
Like this:
echo urldecode("abc.com/my%2Bstring"); // => abc.com/my+string
echo PHP_EOL;
echo rawurldecode("abc.com/my%2Bstring"); // => abc.com/my+string
Further if you want to get the actual my+string, you can utilize the powers of parse_url function which comes with PHP itself, although you have to provide a full URL into it.
Other way is just to explode the value by a / and get it like this:
$parts = explode('/', 'abc.com/my+string'); // => Array(2)
echo $parts[1] ?? 'not found'; // => string|not found
Also read the documentation on both: urldecode and rawurldecode.
Example here.
How can I read the values of get header variables which are from $_SERVER["HTTP_REFERER"] such as if I had index.php?col=example&order=example2 how could I read the values of col and order from the string which is received from $_SERVER["HTTP_REFERER"] and would this be safe?
I thought of using strpos() but that would mean I would have to make a function to find the position of col for example and then read the value starting from thr = and stop at the next & or null value if only one get header is set...
Based on a dupe, use parse_str, like this:
$str = 'index.php?col=example&order=example2';
$qs = parse_url($str, PHP_URL_QUERY);
if(!empty($qs)){
parse_str($qs, $output);
// TODO check for key existence
echo $output['col']; // example
echo $output['order']; // example2
}
The difference is that it won't work with index.php?, so we get just the query string part from the url.
I suggest you do some checking to make the script more reliable.
In a PHP application, $_SERVER['HTTP_REFERER'] has the following value:
http://testing.localhost/userdashboard/test/fc
I have try this $value= striurl($_SERVER['HTTP_REFERER'], 'test');, the value I get is test/fc.
My question is what is the proper way to extract the value of "fc"?
Thanks a lot for any help.
Laravel's Request class has a function called segments() which returns an array of all segments in the url.
here it would be = to ['userdashboard', 'test', 'fc']
So with that in mind, you can grab the last piece with...
$lastSegment = last(request()->segments());
try this
echo end(explode("/",$_SERVER['HTTP_REFERER']));
The function you're looking for is basename().
$base = basename('test/fc');
echo $base; // fc
You might also want to look at parse_url(), which will extract all the elements of a URL into a nice array structure.
Consider a php script visited with URL of foo?q=some&s=3&d=new. I wonder if there is a paractical method for parsing the url to create links with new variable (within php page). For example foo?q=**another-word**&s=3&d=new or foo?q=another-word&s=**11**&d=new
I am thinking of catching the requested URL by $_SERVER['REQUEST_URI'] then parsing with regex; but this is not a good idea in practice. There should be a handy way to parse variables attached to the php script. In fact, inverse action of GET method.
The $_GET variable contains an already parsed array of the current query string. The array union operator + makes it easy to merge new values into that. http_build_query puts them back together into a query string:
echo 'foo?' . http_build_query(array('q' => 'another-word') + $_GET);
If you need more parsing of the URL to get 'foo', use parse_url on the REQUEST_URI.
What about using http_build_query? http://php.net/manual/en/function.http-build-query.php
It will allow you to build a query string from an array.
I'd use parse_str:
$query = 'q=some&s=3&d=new';
parse_str($query, $query_parsed);
$query_parsed['q'] = 'foo-bar';
$new_query = implode('&', array_map(create_function('$k, $v',
'return $k."=".urlencode($v);'),
array_keys($query_parsed), $query_parsed));
echo $new_query;
Result is:
q=foo-bar&s=3&d=new
Although, this method might look like "the hard way" :)
http://localhost:8888/test.php
I typed in the above URL to navigate to a blank php file I'd created, and my friend typed the following in my URL bar:
?one=3&two=18
(after /test.php)
He said that when I refresh the page, he wants to see the number 21 show up. I'm not entirely sure where to start. Any help for this PHP beginner is appreciated.
Thanks!
Try this...
<?php
if (isset($_GET['one']) AND isset($_GET['two'])) {
$one = (int) $_GET['one'];
$two = (int) $_GET['two'];
echo $one + $two;
}
? is the operator used to indicate the start of the parameters passed to a webpage. You can pass many of them separated by the character &. Based on that you need your website to get those parameters and perform the only operation that will get you the result 21 from 18 and 3.
So you need your webpage to display the sum of your first parameter and your second parameter.
You can get those by using $_GET.
Bottom line what you need is:
Learn how to get the parameters passed
Learn how to do operations with them (sum)
Learn how to display the result.
Learn about UrlEncode
Good luck on the php world!
to obtain something from your query string (?... part of the URL) you have to use $_GET[] i.e.
$one = $_GET['one'];
$two = $_GET['two'];
echo $one+$two; //print it
You need to GET the variables from that URL and manipulate them.
http://www.w3schools.com/php/php_get.asp
But you first and foremost need to learn from the ground up. I suggest this tutorial:
http://www.tizag.com/phpT/
<?php
echo $_GET['one'] + $_GET['two'];
?>
print_r($_GET) will show you the anwser.