a URL ending with something like portal.php?key2=hello how can I get the value of "key2"?
$_GET['key2']
will get you the value from a query string.
$_POST['key2']
will get you the value from a form posted.
$_REQUEST['key2']
will get you either of the above if it exists.
var_dump( $_GET['key2'] );
GET data is decoded into the $_GET super-global array. See http://php.net/manual/en/language.variables.external.php
Related
I am tring to send values from one page to others from following url in php
result.php?1=C&2=C&3=C
But i am not able to access values on second page.
you can get value from following url result.php?value=1&val=2 using
$_GET['value'];
$_GET['val'];
Because you cannot assign a variable to start with integer
To receive values across pages we usually use $_GET['key']
to receive the values you sent
just use
echo $_GET['1'];
so everything before the = sign(LHS) ie is 1,2,3 are the keys in your case and the value would be stored in $_GET['1'], $_GET['2'], $_GET['3']
PHP can access query string parameters with the superglobal array $_GET.
For example;
If URL is http://example.com/script.php?var1=hello
echo $_GET['var1']; //hello is echoed.
When I try the following:
$post_string='data='.urlencode(json_encode($data)); //data is a big nested array
$post_string='&password='.urlencode($password);
$post_string='&username='.urlencode($username);
The $_POST data I received on the other server becomes corrupted - either password or username is missing. I suspect I did not encode the data into JSON in the correct way. What have I done wrong?
You are using = to assign a new value. Each line discards the previous value. You want to use .= for a concatenating assignment.
I'm using GET to process my search form and working with pagination I need to resubmit the $_GET params as my target URL. Is there an easy way to build my new target URL from the contents of $_GET or do I need to use something like explode or simply iterate through the $_GET array?
Basically I'm looking for a shortcut or a better method of doing this.
Any ideas?
$_SERVER['QUERY_STRING'] contains the query string submitted to the script.
Simply loop through the $_GET by using the syntax below:
foreach ($_GET as $key=>$val) {
// build your URL here
}
If you want to manipulate the query string, then http_build_query() would be handy to create another query string out of present one using altered $_GET array (or it's copy)
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 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.