4

I haven't officially started learning PHP, just skimming through a couple tutorials and I have a question. Why would some one choose to use Get vs Post? Why would you ever want the data shown in the url bar? I understand post is used for passwords and important info but I don't understand why you would use get instead of just post all the time?

Thanks for any insight.

Michael Robinson
  • 29,278
  • 12
  • 104
  • 130
user377419
  • 4,681
  • 13
  • 42
  • 56

6 Answers6

29

$_GET is useful for pages where users are requesting data - such as a search page, and pages that a user might want to bookmark and share with others. Actions that should be readonly.

$_POST is useful for pages where users are "posting" data - such as a signup form. $_POST should be used when you don't want your visitors to be able to bookmark page. Actions that write data.

As prodigitalson added: you may use $_POST or $_GET for any operation, but it is good practice to use them as described above.

Community
  • 1
  • 1
Michael Robinson
  • 29,278
  • 12
  • 104
  • 130
  • 4
    I would add that is is also useful to think of `GET` as read-only operations, where as `POST` is read/write. This isnt limitation inherent to `GET`/`POST` but rather a good practice to get in to. – prodigitalson Aug 14 '10 at 01:36
19

If you want people to be able to share the link with their friends...for eg http://example.com/products.php?product_id=12

MitMaro
  • 5,607
  • 6
  • 28
  • 52
Neil Sarkar
  • 6,834
  • 8
  • 32
  • 30
9

GET requests are idempotent. POST requests change server state.

This is an HTTP question, not a PHP question.

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
3

are you planning to fill your website with forms and buttons on each link?? every link you see in this site is sending GET variables.. maybe your question is related to the "method" attribute in a form, if that's the case, well 90% of the cases post is a better choice dont worry about the security :) just because you dont see the information in the navigation bar doesnt mean that its secured, watching the information sent by post is only two clicks away ;)

pleasedontbelong
  • 19,542
  • 12
  • 53
  • 77
  • The issue is primarily related to browser history / browser state. By using `$_POST` you prevent data from being exposed when links are sent in e-mails (etc.) or saved in browser history, so `$_POST` is limited to an individual browser's session. – cbednarski Aug 14 '10 at 01:53
  • Yes.. i totally agree, if we are talking about information sent using a form, but for links is totally different. The question was "why you would use get instead of just post all the time?".. because you cant replace links with forms :) (and some browsers also save the information sent by forms for autocompletion) – pleasedontbelong Aug 14 '10 at 02:18
1

Some times you have to pass params(data) to a script without form submit OR want to share that script to someone. In that case $_GET is useful.

GET method may result in long URLs, and may even exceed some browser and server limits on URL length.

Naveed
  • 41,517
  • 32
  • 98
  • 131
0

GET can be used for multiply reasons.. If you want to share a URL with your friend, like http://site.com/share.php?id=123 <- Often used.

Its often used to do dynamic actions.

POST is often used when sensetive information should not be shared.

You can look it up on google to learn more =)

Stian
  • 649
  • 3
  • 7
  • 15