I know fundamentals of PHP, Please give me an example for the underlined part of this instruction:
When performing a GET request, the query string is the part of the URL after the question mark, and is used to pass key/value pairs; the responding script can then access these with $_GET
. For example, try creating two files in a folder, say foo.php and bar.php like
// foo.php
<?php
$queryString = urlencode('Hello world!');
header('Location: http://localhost:8888/bar.php?greeting=' . $queryString);
?>
and
// bar.php
<?php
if (isset($_GET['greeting'])) {
echo $_GET['greeting'];
} else {
echo 'Got nothing...';
}
?>
Now run a local PHP server in that directory with
php -S localhost:8888
from the shell. Then open a browser and visit http://localhost:8888/foo.php – the script will redirect to bar.php, passing the greeting as a query string, which bar.php will output to the browser.
Of course, this is not suitable for passing sensible data such as passwords – you’d use POST for that – but enables you to bookmark or share a certain request, such as a search on a shopping site.
Thanks for your complete response, but I again have a problem, why is the “greeting” in your example a key for $querystring?
I know that if we are going to make a key/value pair we have to use this sign “=>” inside an array function.
The key is simply given on the left side of the equal sign, like
http://some.url/foo.php?key=value&someotherkey=someothervalue
which can then be accessed as if you had used the =>
assignment within the script itself.
I think your question is along the lines of,
$str = 'hello there, good to see you';
With this you cannot do,
$url = 'http://mysite.com?greeting='.$str.'';
greeting would just be hello (you can’t use spaces in a querystring).
to fix that you would need to do,
$str = urlencode($str);
Now you can get your entire querystring in $url.
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.