SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Thread: GET and POST

  1. #1
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    GET and POST

    Hi there,

    I am really new to PHP and I was wondering where the difference between
    Code:
    $_GET['var']
    and
    Code:
    $_POST['var']
    is ... Can someone explain that to me, please?

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    $_POST vars are sent through a form in the background whereas $_GET are sent via the URL in the browser. $_POST and forms should be used where the information sent should be kept hidden eg: Login forms, registration forms etc

    Have a look on here for more info
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Member therealfiredog69's Avatar
    Join Date
    Aug 2006
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When submitting a form with HTML you have the option to send it to your script via two methods. GET and POST.

    GET:
    sending with the GET method will send your form data via the URL.

    example:
    HTML Code:
    <form name="myForm" method="GET" action="./myphpscript.php">
         Enter your name: <input type="text" name="myname">
         <br /><br />
         <input type="submit" />
    </form>
    When the form sends, the URL will look like this:
    http://www.mydomain.com/myphpscript.php?myname=Steve

    POST:
    I really don't know how to explain this one, but all in all, it does not send the data via the URL.

    There are different reasons why you would use one versus the other.

    But, to answer your question:
    $_GET['myname'] will get the data from the myname element from the URL if you sent the form via the GET method.

    $_POST['myname'] will get the data if you sent your form using the POST method.

    Both of these I believe are depricated.

    I like to use the $_REQUEST['myname']. This will get the data no matter which way you sent it.

    Visit this link: http://us2.php.net/manual/en/languag...predefined.php

    Hope this helps...

  4. #4
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your replay. That really helps me to understand it Thank ypu very muuuch

  5. #5
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The Difference between GET and POST

    When the user enters information in a form and clicks Submit , there are two ways the information can be sent from the browser to the server: in the URL, or within the body of the HTTP request.

    The GET method, which was used in the example earlier, appends name/value pairs to the URL. Unfortunately, the length of a URL is limited, so this method only works if there are only a few parameters. The URL could be truncated if the form uses a large number of parameters, or if the parameters contain large amounts of data. Also, parameters passed on the URL are visible in the address field of the browsernot the best place for a password to be displayed.

    The alternative to the GET method is the POST method. This method packages the name/value pairs inside the body of the HTTP request, which makes for a cleaner URL and imposes no size limitations on the forms output. It is also more secure.

  6. #6
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by therealfiredog69

    Both of these I believe are depricated.

    I like to use the $_REQUEST['myname']. This will get the data no matter which way you sent it.
    $_GET and $_POST arent deprecated. you actually should be using a specific superglobal in most cases. if you intend the data to be sent via post, why would you also accept it via _GET or _COOKIE? theres rarely a good reason to be so lenient in where you allow the data to come from.

    _REQUEST contains get, post, and cookie variables. when you do
    echo $_REQUEST['foo']
    you have no idea if foo came from the url, a form post, or a cookie.

    there are definately times where $_REQUEST is very usefull. but if youre not careful, someone will come along and use this to thier advantage to make your application behave in a way you did not intend it to. i recomend you start using the specific arrays.

  7. #7
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I agree with clamcrusher. An example can be: you don't want to allow users logging in using URL, but only by submitting a form. But, if you use $_REQUEST instead of $_POST, specifying password in the URL (GET method) will work as well.

  8. #8
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,933
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $_GET should be used solely for data retrieval while $_POST should be used for everything else but should not be used for pure data retrieval. If there is a permanant change of state, e.g. send an email, upload a file or alter a database $_POST should be used.

    $_GET: Look up a word in an online dictionary.
    $_POST: Order a new Mercedes Benz.

    The $_POST method produces a warning if the user presses refresh. This is one reason why it should not be used for data retrieval. It would make the users desensitised to these types of warnings and casual about accepting them, meaning when the lorry turns up from Mercedes it would be dropping off two instead of one.

    Quote Originally Posted by simplecode
    Unfortunately, the length of a URL is limited, so this method only works if there are only a few parameters.
    According to RFC 2616 that is not the case. The only previso is that webservers shall be able to process any query string they serve.

  9. #9
    SitePoint Member therealfiredog69's Avatar
    Join Date
    Aug 2006
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As far as the deprecation of $_GET and #_POST, the php.net site states that they are deprecated.

    $_GET
    Variables provided to the script via URL query string. Analogous to the old $HTTP_GET_VARS array (which is still available, but deprecated).

    $_POST
    Variables provided to the script via HTTP POST. Analogous to the old $HTTP_POST_VARS array (which is still available, but deprecated).
    http://us2.php.net/manual/en/languag...predefined.php

    Not trying to argue.. just posting what I saw..

  10. #10
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,933
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by therealfiredog69
    As far as the deprecation of $_GET and #_POST, the php.net site states that they are deprecated.

    Not trying to argue.. just posting what I saw..
    Maybe you should go to English classes to help you learn to read it properly! Those which clauses refer to the noun phrase of the first clause in each particular sentence which reads "Analogous to the old $HTTP_GET_VARS array". In other words the deprecated items are: $HTTP_GET_VARS and $HTTP_POST_VARS, not $_GET and $_POST.

  11. #11
    SitePoint Evangelist dscriptor's Avatar
    Join Date
    Oct 2005
    Location
    in front of my computer
    Posts
    571
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    off topic
    if $_GET and $_POST are deprecated, what are we going to use now?
    peace.
    happy is the man that finds wisdom....wisdom in {PHP}.


  12. #12
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    off topic
    if $_GET and $_POST are deprecated, what are we going to use now?
    snail mail.......
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  13. #13
    SitePoint Member therealfiredog69's Avatar
    Join Date
    Aug 2006
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I do deeply apologize, $_GET and $_POST are NOT deprecated. I did not read the information correctly.

    Thank you Mr. bokehman, the English Professor for pointing this out. I will be sure to enroll myself into the next ENG 101 class. Will you be teaching it?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •