Receiving Query String in PHP - php

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'
}

Related

insert an URL in mysql database using php script with GET method

http://192.168.1.7/cars_store/insert_annoce.php?urlFace=firebasestorage.googleapis.com/v0/b/carsstore-1c8c5.appspot.com/o/photos%2Fimage%3A60Sat+Dec+24+10%3A39%3A10+EST+2016?alt=media&token=908e383e-1901-4c04-855d-d6c7280b40a1
$urlFace=$_GET['urlFace'];
echo $urlFace;
and this is the result of the script:
firebasestorage.googleapis.com/v0/b/carsstore-1c8c5.appspot.com/o/photos/image:60Sat Dec 24 10:39:10 EST 2016?alt=media
I always get the url missing the last part (after '&token=' ) method
Since you're passing the URL as a GET parameter within another URL, you'll have to somehow escape it so that it is distinguishable from the url that contains it.
an easy solution would be to use POST, where the data doesn't get sent as part of the URL, and therefore doesn't have this problem.
Otherwise, you'll have to encode your URI within a URI: http://php.net/rawurlencode
You can use urlencode()
This function is convenient when encoding a string to be used in a query part of a URL, as a convenient way to pass variables to the next page
Eg:
$my_url = "http://192.168.1.7/cars_store/insert_annoce.php?
urlFace=".urlencode("firebasestorage.googleapis.com/v0/b/carsstore-
1c8c5.appspot.com/o/photos%2Fimage%3A60Sat+Dec+24+10%3A39%3A10+EST+2016?
alt=media&token=908e383e-1901-4c04-855d-d6c7280b40a1");
You can use $my_url in <a> tags , form actions etc.
Then $_GET[urlFace] in annoce.php will give the full url you needed

Strange Error with Complex PHP Form Processing

NEW INFORMATION:
I used the print_r function on the $_REQUEST and something very strange is happening there too. Some values are being correctly passed by the GET such as a value on another form which passes in "TRUE". This can be seen in the print_r output but isn't written to the file... Still no closer to finding a solution to my problem however.
I'm working on a page with a lot of forms which are loaded in as needed by AJAX. This all works fine as does parsing the name:value pairs and storing them appropriately.
My error happens when the PHP parses the GET request sent by AJAX when the user is finished, it only seems to retrieve the values from certain fields.
The idea is that the user can add data from any number of forms, which are then turned into a GET request and sent to the server.
The JavaScript is building my request perfectly and all forms are sent correctly.
Depending on the forms the user submits, the data is processed by a large switch statement which passes the relevant names to a variadic function which grabs the values, and creates a string for writing to a file.
The strange error is that only some values get written to the file with others only having a blank line. No error reported by Apache or PHP, no error reported in the JavaScript console either.
I'll use the Colour form for example as this is one of the more complex.
So I add a colour action and click the button to submit all forms (this time, it's just the colour form though)
My get request looks like this:
actionIDs=Colour&coOptionSelect=Tinting&coColourEffect=Sepia&coRemoveColour=#000000&coRemoveFuzzNumber=0&coRemoveHueSelect=None&coReplaceColour=#000000&coReplaceFuzzNumber=0&coReplacementColour=#000000&coReplacementAlphaNumber=0&coReplaceHueSelect=None&coReplacementHueSelect=None
Next, the PHP parses the actionIDs part as sometimes, there will be many actions. This works fine.
We now jump to the Colour part of the switch statement.
case "Colour":
$config = processAction("coOptionSelect", "coColourEffect", "coRemoveColour", "coRemoveFuzzNumber", "coRemoveHueSelect", "coReplaceColour", "coReplaceFuzzNumber", "coReplacementColour", "coReplacementAlphaNumber", "coReplaceHueSelect", "coReplacementHueSelect");
file_put_contents($confpath . "colour.conf", $config);
break;
That writes to the correct file, but strangely, only coOptionsSelect and coColourEffect have their values written to the file. It isn't their input type as they are select statements similar to the other selects on the form. On other forms, it may be a number input or a text input that submits properly instead.
It isn't random either, the same ones will always write out properly. It also isn't positional as I moved around the values and it's still the same ones that write correctly, their position doesn't affect anything.
Finally here is processAction function.
function processAction()
{
$config = "";
foreach(func_get_args() as $field)
{
$temp = isset($_REQUEST[$field]) ? $_REQUEST[$field] : null;
$config = $config . $temp . "\n";
}
return $config;
}
The end result should be all values should write to their relevant files correctly, rather than the current issue where only a few values from each form are written, with the rest of the values being written as blank lines.
You probably need to encode your # sign to a encoded method %23
you could also use urlencode to do it before passing it to your variable.
Reference: http://php.net/manual/en/function.urlencode.php
Update:
If you are going to try to encode through javascript I would try and use this method
var newURL =
"http://example.com/index.php?url=" + encodeURIComponent(actionIDs);
or
var newURL =
"http://example.com/index.php?url=" + escape(actionIDs);
Reference: Encode URL in JavaScript?
You have three options:
escape() will not encode: #*/+
encodeURI() will not encode: ~!##$&*()=:/,;?+'
encodeURIComponent() will not encode: ~!*()'
But in your case, if you want to pass a URL into a GET parameter of other page, you should use escape or encodeURIComponent, but not encodeURI.
See Stack Overflow question Best practice: escape, or encodeURI / encodeURIComponent for further discussion.

if ($_REQUEST) in Coldfusion

I am trying to use an if statement to check if the user has been directed to this page with from the correct location with correct data.
in php we would use
if ($_REQUEST)
{
......
}
but how can I do it in coldfusion?
According to this, $_REQUEST is:
An associative array that by default contains the contents of $_GET,
$_POST and $_COOKIE.
In which case, there is not an equivalent in Coldfusion. The $_GET would be equivalent to CF's URL scope. The $_POST would be equivalent to CF's FORM scope. The $_COOKIE would be equivalent with CF's COOKIE scope. You would need to check the appropriate scope depending on what you are looking for.
There are, however, frameworks for Coldfusion, such as Coldbox, that manage a "Request Collection" for you.
With more information this answer could be more complete.
You can check where the user came from using CGI.HTTP_REFERER. Example:
if (CGI.http_referer contains 'www.good-domain.com') { ... do something here ... }
You'll need to check the URL or FORM scopes to inspect the data to see if it is the "right data". You can inspect these values with something simple like this (which checks the form scope--used when data is posted in the body of a request):
requestIsGood = true;
requiredKeys = ['keyA','keyB','keyC'];
for (key in requiredKeys) {
if (!structKeyExists(FORM,key) {
requestIsGood = false;
break;
}
}
if (requestIsGood) { ... do something here ...}
Lastly, the difference between the URL and FORM scope in ColdFusion (CFML) is that values in the url's querystring will be populated to the URL scope. Values posted by the form will exist in the FORM scope.
The code examples should be accurate for Adobe ColdFusion 9+ http://adobe.com/go/coldfusion and the open source CFML engine Railo http://getrailo.org. It may work on another open source CFML engine OpenBD http://www.openbluedragon.org/ but I am not sure.
Hope this information helps you.

PHP mixing POST and GET Requests to one page

After reading:
When do you use POST and when do you use GET?
Mixing GET with POST - is it a bad practice?
I understand, that GET is used to retrieve a page without changing the server and POST is used for things (insert, update, delete), that change the server.
Now I have written a page which is called with a GET request with parameter StationNr set. The user can fill a form and makes a POST request to the same page with parameter Filter set. But I don't want to miss the parameter StationNr thus I thought I give it into a hidden input field. But then the parameter StationNr is either in the $_GET variable (first call) or in the $_POST variable (second call). I can do something like:
if (isset($_GET['StationNr']))
$snr = $_GET['StationNr'];
else if (isset($_POST['StationNr']))
$nr = $_POST['StationNr'];
But I don't like this. Also I don't want to use $_REQUEST['StationNr'] because of: When and why should $_REQUEST be used instead of $_GET / $_POST / $_COOKIE?
I think this is a common issue but I haven't faced it yet because I'm a beginner in writing php pages. How did you solve this problem?
Thanks!
Although you can use ?foo=bar to push GET values in a POST request, I'd suggest checking the request method instead:
if($_SERVER['REQUEST_METHOD'] == 'POST') { ... }
just use
<form method="post" action="script.php?get=variables">
<input name="your_inputs" />
</form>
Correct Syntax Would Be:
if (isset($_GET['StationNr'])) {
$snr = $_GET['StationNr'];
}else if (isset($_POST['StationNr']))
$nr = $_POST['StationNr'];
}

Code Igniter - Get Hash Value With window.location.hash and Use in set_value()?

I need to get the hash # value from the current window url with javascript (can't get it with PHP), and then use that to set the value attibute of a form input with CI's set_value() function.
Any ideas how I can grab the value in javascript then put it into the PHP function? :-S
Thanks in advance!
I can't quite tell if you fully understand the relationship between Javascript and PHP, so I'm going to assume that you don't. PHP runs on the server and outputs HTML to the browser (along with some headers and other stuff). Once the HTML is sent to the browser, what the user does with your site is out of your control until his browser performs another GET or POST request (or opens a persistent web socket connection). As you mentioned, it's impossible for PHP to outright read the value of the hash, as it's meant to be confined to the browser, accessible only via Javascript. Since your PHP script for that particular request has stopped running and the page data has already been output to the browser, what sense does it make to grab the value using Javascript, send it back to PHP with another http request, use the set_value() function, and then what?
If you use window.location.hash and set the value of your input box equal to it, you will be able to use set_value() when the user submits the form. Take a look at CI's set_value() helper function:
function set_value($field = '', $default = '')
{
if (FALSE === ($OBJ =& _get_validation_object()))
{
if ( ! isset($_POST[$field]))
{
return $default;
}
return form_prep($_POST[$field], $field);
}
return form_prep($OBJ->set_value($field, $default), $field);
}
It first checks to see if you've set up form validation object. If you have, it runs the form_prep() function using that object's set_value (presumably to manage any rules you've set up in your form validation. If you haven't, it sees if that field exists in the $_POST array. If it doesn't, it returns the default value. If it does, it returns the value from the $_POST array. So if you a) set up a form validation rule for it and b) give that input box the name you want to use in your set_value() function, you will be able to retrieve the value of the hash in PHP which you have entered into that input box using Javascript.
In Javascript you can achieve this vis
hash = window.location.hash;
In PHP you can access it using parse_url()
parse_url() : Return Values
On seriously malformed URLs, parse_url() may return FALSE. Otherwise an associative array is returned, whose components may be (at least one):
* scheme - e.g. http
* host
* port
* user
* pass
* path
* query - after the question mark ?
* fragment - after the hashmark #

Categories