I am writing code for a simple WAMP server
. It would receive a query string from another machine and simply display the parameters after parsing.
How would the server know that a query string has been received? And when received, how would it perform desired action on the query string?
Edit: The parameters are being passed as URL.
You're looking for the superglobal variable $_GET
.
For example, at the URL http://example.com/foo.php?bar=BAM
, the variable $_GET['bar']
has the value BAM
.
You can check whether there are any query string variables by seeing if count($_GET) > 0
.
To check whether a parameter is set, you can do something like this:
if (isset($_GET['bar'])) {
// do something cool
} else {
echo "Hey, you didn't give me a value for bar!";
}
Edit: If you are passing it in some other way, not in the URL, then please clarify your question. How you access the data will obviously depend on how you receive it.
Lets say you have two URLs:
www.example.com
www.example.com?param=Jonas
Now in first url you have no parameters, while second has parameter param
that value is Jonas
.
In PHP parameters usually are passed via POST or GET requests. They are stored to $_POST
or $_GET
(depending on request type). So to get something form GET request (every parameter in URL) you use:
if (!empty($_GET['param'])) {
echo $_GET['param']; // will echo 'Jonas'
}