0

I'm just getting started with PHP and I've found that no matter where I look online, I can't find any in-depth explanation of how the $_POST superglobal actually works.

I get the basics that the $_POST superglobal is an associative array that can be accessed anywhere within a script, and that it holds values received through post requests. My questions are: is it universal to all PHP files on a website? If not, how do you specify which file on the website you want to post data to, or transfer something received by $_POST to another file? How do you make a code that waits for data to be sent to a certain index in $_POST (ie, $_POST["name"]) before doing something with that data, so that your code doesn't immediately set a variable equal to the aforementioned post index which is null before receiving the request?

I ask these questions because I'm finding that although I have no problem using $_POST when it comes to form handling, I can't get anything to work with $_POST when I send the post request externally, such as from a program like Fiddler. I've tried sending post data in the "application/x-www-form-urlencoded" format to my testing website hosted by a free website hosting service and no matter what I do, I always end up with $_POST["name"] (yes, the data I'm sending is properly written as being "name=test") being of type NULL in every file on my site, making it therefore not possible for me to store this sent data in my SQL database.

staple
  • 1
  • 1

3 Answers3

1

is it universal to all PHP files on a website?

Of course. It wouldn't do much good if it wasn't.

how do you specify which file on the website you want to post data to

By having it handle the URL that the request is made to

or transfer something received by $_POST to another file?

Generally by includeing that file.

How do you make a code that waits for data to be sent to a certain index in $_POST (ie, $_POST["name"]) before doing something with that data

Generally speaking, you don't.

Websites that use PHP don't write the entire web server in PHP.

You put a PHP script on a webserver to give it a URL. The client makes an HTTP request to that URL. The webserver starts the PHP program in response to that request. When the PHP program finishes, the output from it is sent back to the client.

I have no problem using $_POST when it comes to form handling, I can't get anything to work with $_POST when I send the post request externally, such as from a program like Fiddler.

There is absolutely no difference between the two cases. Either way, an HTTP request is made to the server, which runs the PHP is response.

Presumably, you are making a malformed request when you try to create it manually.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

Step back one layer to the HTTP request. Any time you interact with server-side PHP, it's in the context of an HTTP request.* "Clicking a link" causes an HTTP GET request to the server, which may be handled by a PHP script. Submitting a form causes an HTTP POST request to be sent to the server, where it may be handled by a PHP script.

* Unless it isn't, but we'll ignore less common cases…

Each request which is handled by a PHP script causes one PHP instance to run. It will start with whatever .php file is being requested, and this file can include/require more .php files in the process. All this is still running within the context of the same HTTP request. Whatever you echo or otherwise output will be returned as the HTTP response. When your script is done and all output is returned as response, the PHP instance shuts down and the script execution ends. Until the next incoming HTTP request…

Any body data sent with an HTTP request (typically with POST HTTP requests) which is encoded in the application/x-www-form-urlencoded format will automatically be parsed by PHP and the parsed data will be made available in the $_POST global variable. This variable/data is available to your entire PHP instance throughout its life. It's a convenience API to access the HTTP body data. Once the PHP instance ends, which means once the HTTP request ends, this data is discarded. The data is unique to each request, to each PHP instance.

If the body data is not application/x-www-form-urlencoded encoded, say it's JSON encoded instead, PHP cannot parse it and won't populate $_POST. In this case you'll need to read and parse the body manually, e.g.:

$body = file_get_contents('php://input');
$data = json_decode($body);

The $_GET global variable works very similar, but it contains the parsed URL query parameters (e.g. http://…/example.php?foo=bar&baz=42). No, it doesn't work only for GET HTTP requests, it works for all HTTP requests; that's a common misunderstanding due to frankly bad naming for this variable.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
deceze
  • 510,633
  • 85
  • 743
  • 889
  • Thanks for this! That cleared a lot up. Just a few things - if a website had 2 files, index.php, and extra.php, and a post request was sent to its URL, would that request be received by only index.php, or by both files? And when this request is actually received, does the php code simply run itself the way it would if someone was actually attempting to view it on the website, except now it actually has values for $_POST["whatever"]? – staple Aug 31 '16 at 07:38
  • Only one of those files will be executed by the web server. They can't both be executed by the web server. "Its URL" will point to one of those files, not both. The file that is executed in response to the HTTP request will have the context of the request, meaning it will have access to any HTTP body data in `$_POST`. – Again, should index.php `include 'extra.php'`, then extra.php will also run in the context of the same HTTP request and have access to the same `$_POST` data. However, if you make a separate request to `…/extra.php` without POST data, then it obviously won't. – deceze Aug 31 '16 at 07:44
  • So to send the request to extra.php rather than index.php, would the request have to be directed toward "http://www.examplesite.com/extra.php", given that that's how the URL displays the file location? – staple Aug 31 '16 at 07:51
  • Yes. If you want to send data to extra.php, you will have to send it to `http://…/extra.php`. (You can configure your web server to match arbitrary URLs to arbitrary files, but let's keep it simple first…) – deceze Aug 31 '16 at 08:00
  • Thanks a lot for the help! I'll see what I can do now. – staple Aug 31 '16 at 08:13
-2
In form declaration should use method post.
<form name="form_name" action="your file path to post" method="post">
<input type="text" name="name" value="">
<input type="submit" name="submit" value="submit">
</form>

Code for post file

$sql = "insert into table set col = '.$_POST['name'].'";
mysql_query($sql);
Rana Aalamgeer
  • 702
  • 2
  • 8
  • 22
  • **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) that has been [removed](http://php.net/manual/en/mysql.php) from PHP. You should select a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Aug 31 '16 at 09:54