tags:

views:

212

answers:

5

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.

+19  A: 

$_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. 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. read/write

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

Michael Robinson
`+1` for a great answer.
Delan Azabani
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
Good addition, added it.
Michael Robinson
+13  A: 

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

Neil Sarkar
Oh makes sense.. I think i get it.
+1 Beat me to it =)
Russell Dias
Why the downvote? This was a completely valid answer.
musicfreak
Mod-rewrite does a much better job tho in my opinion.
Iznogood
@Iznogood: You could post that as an answer, but that wasn't the OP's question.
musicfreak
@musicfreak wich is why I did not submit it as one :)
Iznogood
Hey dude. Your the winner!!!! congratz! (accepted answer)
@Iznogood: Fair enough. :)
musicfreak
@shorty876 glad i could help u out
Neil Sarkar
@Iznogood `mod_rewrite` *uses* `$_GET`--it modifies the way query parameters are passed between the client and script.
banzaimonkey
@banzaimonkey he he indeed. But I so often construct it myself I forget it would work with ?var=stuff
Iznogood
+1 to offset stupid downvoting
slebetman
A: 

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
Doesn't answer the question.
musicfreak
+7  A: 

GET requests are idempotent. POST requests change server state.

This is an HTTP question, not a PHP question.

Sinan Ünür
+1  A: 

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
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.
banzaimonkey
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