Click to See Complete Forum and Search --> : whats the difference


oo7ml
06-04-2007, 09:27 AM
what's the difference between the two below when you are getting data from a form

$_POST['username'];

$_REQUEST['username'];

Charles
06-04-2007, 09:51 AM
$_POST is just the data sent by the post method from the form. $_REQUEST aggregates get, post and cookie. See http://us2.php.net/manual/en/language.variables.predefined.php .

bokeh
06-04-2007, 09:55 AM
$_REQUEST is a combination of $_POST, $_GET and $_COOKIE. That means if you also have a cookie named 'username' as well as a POST variable the dominant variable will overwrite the recessive one.

oo7ml
06-04-2007, 10:13 AM
K cool, so which one should i use to just get data from a form

bokeh
06-04-2007, 10:39 AM
Post

Charles
06-04-2007, 11:17 AM
I always use "Request". That way I can change the form to get or store form values in cookies.

bokeh
06-04-2007, 03:34 PM
I always use "Request". That way I can change the form to get or store form values in cookies.Sounds like bad coding practice to me.

Charles
06-04-2007, 04:15 PM
Sounds like bad coding practice to me.To you, which is why you don't do it. To me it sounds like good coding practice which is why I do.

bokeh
06-04-2007, 05:15 PM
To me it sounds like good coding practice which is why I do.Charles, in another thread you were recommending: extract($_REQUEST); These sort of coding practices return us to the days before register globals was disabled by default. Personally I want to keep track of where external variables are coming from rather than allowing users to inject variables into my script from unexpected locations. The chances are this variable will be abandoned in a future version or disabled by default.

By the way why would you need to convert a form between GET and POST; a request is either idempotent or it is not.

Sheldon
06-04-2007, 05:41 PM
I agree with Bokeh, This is bad coding practice. Using Request is leaving the door open for injection.

pcthug
06-05-2007, 08:41 PM
Using the Request super-global over the Post super-global does make your script more susceptible to CSRF attacks. Yes POST requests can be emulated via javascript, though GET requests (the inclusion of a supposed image is a great example) are much more common.

Charles
06-06-2007, 07:36 AM
I'll own that I erred when I posted that example extracting the $_REQUEST SG. However:

Consider the case of a nice idempotent form, perhaps it does some site navigation or preference setting, and it's a perfect candidate for a GET request. But for what ever reason you prefer to use POST with the form. Perhaps you kinda like the URLs simplified. But you also want, on occasion, to communicate with the form with a link.

And further, let's say you want to save one or more of those features in a cookie and use that value unless overridden by the form.

$_REQUEST is, in those cases, just what the doctor ordered.

bokeh
06-06-2007, 08:07 AM
Perhaps you kinda like the URLs simplified.That's not a valid reason to use a POST request although the concept is fair enough.But for what ever reason you prefer to use POST with the form.Sometimes I do use POST for idempotent requests for the opposite reason. I don't want people to be able to link to my output without having used the form. I have to use $_POST in these cases though otherwise I woudn't know from where the input had arrived.

Charles
06-06-2007, 09:11 AM
Exactly. $_REQUEST is just another tool in your chest. Sometimes it's just the right tool for the job and sometimes it's not.