URL: http://example.com/?var=foo&var=bar
How to make PHP set $_GET['var'] as "foo" and not "bar", without parsing manually $_SERVER['QUERY_STRING']? Maybe there is some option in the php.ini file?
Unfortunately in PHP you cannot do this. You should use an array:
http://example.com/?var[]=foo&var[]=bar
You will get the following in PHP:
$_GET['var'] = ['foo', 'bar']
You will find this is different in other languages such a golang where your first example will actually get the same result.
You can find a brilliant answer on this answer.
No built in way to do this. PHP's $_GET wasn't designed for this practice, for it is an uncommon one. I recommend against it.
terrible idea, but i was bored:
$url="http://example.com/?var=foo&var=bar";
$x=parse_url($url, PHP_URL_QUERY); //get the query string
$x=explode('&',$x); //break on &
$x=explode('=',$x[0]); //break on =
echo $x[1]; // =foo
update link to
http://localhost/index.php?var[]=foo&var[]=bar
<?php
$foo=$_GET["var"][0];
echo $foo;//foo
print_r($_GET);//full array
?>
Related
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.
more out of interested...
$_GET['unique'] = blahblahblah=this_is_what_im_interested_in
I know I can get the second element like this:
$words = explode('=', $_GET['unique']);
echo $words[1];
Is there a way to get this in a single line? - that would then 'hopefully' allow me to add that to function/object call:
$common->resetPasswordReply(... in here I would put it....);
like
$common->resetPasswordReply(explode('=', $_GET['unique'])[1]);
I'm just interested to see if this is possible.
PHP supports the indexing on functions if they are returning arrays/objects; so the following would work too:
echo explode('=', $_GET['unique'])[1];
EDIT
This is termed as array dereferencing and has been covered in PHP documentations:
As of PHP 5.4 it is possible to array dereference the result of a
function or method call directly. Before it was only possible using a
temporary variable.
As of PHP 5.5 it is possible to array dereference an array literal.
This should do it for you.
substr($str, strpos($str, "=")+1);
Strangely enough you can 'almost' do this with list(), but you can't use it in a function call. I only post it as you say 'more out of interest' :-
$_GET['unique'] = "blahblahblah=this_is_what_im_interested_in";
list(, $second) = explode('=', $_GET['unique']);
var_dump($second);
Output:-
string 'this_is_what_im_interested_in' (length=29)
You can see good examples of how flexible list() is in the first set of examples on the manual page.
I think it is worth pointing out that although your example will work:-
$common->resetPasswordReply(explode('=', $_GET['unique'])[1]);
it does kind of obfuscate your code and it is not obvious what you are passing into the function. Whereas something like the following is much more readable:-
list(, $replyText) = explode('=', $_GET['unique']);
$common->resetPasswordReply($replyText));
Think about coming back to your code in 6 months time and trying to debug it. Make it as self documenting as possible. Also, don't forget that, as you are taking user input here, it will need to be sanitised at some point.
I have a bunch of methods of sorting date=desc,etc,etc and they are posted via a $_GET I want to take all the $_GET variables ($_GET['anythingatall']) and transform them from $_GET['variable]=blah to &variable=blah
Is there a simple way to do this?
You are interested in $_SERVER['QUERY_STRING'], I think. This will contain everything passed in $_GET but in the format you desire.
This might work for you
$string = http_build_query($_GET, null, '&')
Alex's solution should work too, and admittedly cleaner. If you wanted to create a query string from any other array using http_build_query should work fine.
If all you're wanting to do is pass on the existing query string (which is available in $_GET), you can use $_SERVER['QUERY_STRING'], which will be exactly what you're looking for, a query string representation of the $_GET array (assuming you didn't change it)
See the PHP documentation on the $_SERVER superglobal.
you may want to take a look at http://us.php.net/manual/en/function.http-build-query.php
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.
How can I make a link that justs adds or changes 1 GET var while maintaining all others?
I have a page that is created using different GET vars.
So it will be like mypage.php?color=red&size=7&brand=some%20brand
So I want to have a link that sets it to page=2 or size=8. Whats the easiest way to have a link do that without reseting all the other vars?
I hope that makes sense, let me know if I need to further explain anything
You can parse the url with parse_str to get the values of the url. You can then build a http query by using http_build_query:
$query_arr = $_GET; //or parse_str($_SERVER['QUERY_STRING'], $query_arr)
$query_arr["page"] = 2;
$query_arr["size"] = 8;
$query = http_build_query($query_arr);
EDIT: Sorry I mixed up the two functions ... its parse_str() of course.
http_build_query