How does POST in web server (localhost) work exactly?

邮差的信 提交于 2019-12-13 07:17:07

问题


I am new to the web server project and I seem to be misunderstanding basic concept of POST, please help.

I thought of POST as posting data onto file in the server but apparently thats not the case according to other people. What am I getting wrong?

Where does the POST data (eg. ajax post) go to exactly? what file should the post url lead to? and how is it saved so that you can close the browser and access it again the next time?


回答1:


What is POST?

The POST method does not have any restriction on data size to be sent.

The POST method can be used to send ASCII as well as binary data.

The data sent by POST method goes through HTTP header so security depends on HTTP protocol. By using Secure HTTP you can make sure that your information is secure.

The PHP provides $_POST associative array to access all the sent information using POST method.

Starting with the page you are on, POST is used to sends data through HTTP header

So you have a form on your home.php page

 <form action ="script.php" method = "POST">
     Name: <input type = "text" name ="name" />
     Age: <input type = "text" name ="age" />
     <input type = "submit" />
  </form>

And you want to do something neat with the data which will be POST with the form. This will happen when the submit button is clicked. The action the form will take will run the script.php file. What that means exactly is we will put a php script on the script.php page so when our form is submitted the script page will be activated and the script on it will do whatever we want!

In our case we are going to take the POST data and turn them into variables.

 $myName = $_POST['name']; //this is the name ="name" in form
 $myAge = $_POST['age']; //this is the name ="age" in form

now if we want to use this data we can echo it to the screen

echo $myName;
echo $myAge;

Now lets say we want to save this data so we can use it again... We have options, one way would be to save it to a database. But we will do that another time.

This time we can start a session and then save the data to a session. Start session

session_start();

$name = $_POST['name'];

array_push($_SESSION['name'], $name);

print_r($_SESSION['name']);

At this point any page we go to as long as we have a

  session_start();

We can call on anything we have stored in our SESSION. you can even push more data to the SESSION and call on it in the same way.

I hope this helps!

references

this and This

MySql with PDO

Example with table to install in database.




回答2:


A POST is not always a file upload. In fact it's rarely that in my experience.

A POST request is used to send data to a web server in the body rather than in the URL as parameters like a GET request. This body content could be parameters you would rather not put into the URL (e.g. Username and Password for a login form) or larger items that wouldn't be best placed in URL (e.g. a file upload).

Either way you need something on the web server to listen for the request, process it, then return a result. This is the same if it's localhost or a real server. The processing might involve confirming the username and password and returning a cookie, or it might involve saving the file somewhere so it can be retrieved later - that's entirely up to you application.




回答3:


POST is just one of the methods (or 'verbs') to send data to a server.

Apart from the commonly used GET and POST, there are also PUT and DELETE and some other methods as well. Officially you should use PUT to store information and DELETE to delete information, although in practice, people tend to use GET to send all kinds of commands through query string parameters in the url, and use POST when they want to 'hide' that data (that is, not show it in the url), or when they need to post larger chunks of data, like an uploaded file.

So officially, to delete document1, you would officially make a DELETE call:

DELETE http://example.com/document1

But most of the time, people would implement it as

GET http://example.com/document1?action=delete

or

POST GET http://example.com/document1
action=delete
.

But even when you POST or PUT a file, that doesn't mean that the file is automatically stored on the server. These methods are just about ways to send information to the server and to tell the server what you would like to happen. How the server handles those requests fully depends on how it is implemented. In general, you would need some server side script or application to handle the request and -for instance- store the posted data in a file or a database.

For a list of methods and their explanation, see HTTP request methods on Wikipedia.



来源:https://stackoverflow.com/questions/34479094/how-does-post-in-web-server-localhost-work-exactly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!