Jump to content

How Does Content-type Affect Php Processing Of Post Variables


nachumk
 Share

Recommended Posts

I am creating an XMLHttpRequest object in javascript and sending POST variables to my php script. The Content-Type has to be set to x-www-form-urlencoded. Why is this? Can someone explain how the Content-Type affects the way php sees the data?The $HTTP_POST_RAW_DATA had my variables before I set the content-type but the $_POST variables were empty. With the content-type set correctly the $_POST variables look good, and $HTTP_POST_RAW_DATA is blank. This is fine with me, but I'd like to understand why this happens.... the sequence of events. I'm using Apache with PHP.Thank you,

Link to comment
Share on other sites

You've pretty much figured it out, when PHP sees a request come in with that content type it knows that it's a submitted form and automatically populates the $_POST array. Without that content type, PHP does not assume that a post form was submitted. You can also use a content-type of multipart/form-data for a file upload form, and PHP will populate both $_POST and $_FILES. I'm not sure why the raw data would be empty though, but make sure you're using the correct variable. The correct name is $HTTP_RAW_POST_DATA.Never mind, see here:http://www.php.net/manual/en/ini.core.php#...e-raw-post-data

Link to comment
Share on other sites

Duh! That clears that up. So the content-type just helps php figure out how to parse the data for me, but regardless all the data will come through. I was writing the HTTP_RAW_POST_DATA from memory, but i did have it right when I tested it. It actually becomes blank once the content type is set up correctly.Thank you,

Link to comment
Share on other sites

If I create XMLHttpRequest and open it as "POST", why do I still have to set the content-type? Along the same lines why do I have to set it to "GET" in order for me to be able to include ? parameters in the URL? What is the use of the open vs the Content-type?Shouldn't I just be able to create a URL with GET parameters in it, and use the Content-type to indicate to PHP if I'm including POST data?

Link to comment
Share on other sites

If I create XMLHttpRequest and open it as "POST", why do I still have to set the content-type?
I don't know the technical details, but I assume the XHR object just uses a default content type if it's not set, regardless of the request type. Sending a post request with a content-type of text/plain doesn't necessarily violate any specification.
Shouldn't I just be able to create a URL with GET parameters in it, and use the Content-type to indicate to PHP if I'm including POST data?
Yeah, there should be no problems submitting a post request with querystring parameters. You can even do that with a regular form:<form method="post" action="form.php?var=yes">
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...