Jump to content

  •  

  • iBotModz CBox


    Photo

    Beginning PHP - Lesson 5 - GET and POST


    This topic has been archived. This means that you cannot reply to this topic.
    No replies to this topic

    #1 fattwam

    fattwam

      General Grade 2

    • Sub-Admins

    • 3,979 posts
    • Joined: 18-August 07

    Posted 27 March 2008 - 08:12 PM

    The $_GET and $_POST Superglobals.

    One of the most powerful features of Dynamic Coding is the ability to pass values to new pages! You may have seen a URL like this before:

    http://www.somesite.com/viewpage.php?art=24

    The last part of the URL("?art=24") is actually a variable being sent to the page! For example, the number "24" may be telling the page "viewpage.php" to show the article ("art=") number "24" on that page. Just like "viewpage.php?art=95" may be telling the page to show article (art) "95". (This will all make sense in a little bit.)


    In PHP this is called "Posting" and can be done with the "$_GET[]" and "$_POST[]" Superglobals. This is what "$_GET[]" might look like:



    $_GET['page_number']



    In our above URL example I said that the URL probably was to get a certain article number and show it("?art=24"). Let's imagine that this is the case and write an imaginary script to get the "?art=24" variable at the end of the string.




    $article = $_GET['art'];
    /*we are going to extract the "$_GET['art'];"
    Superglobal into a new variable called "$article".
    the value of the variable $article will be "24"
    because that is the value of the $_GET['art']
    Superglobal. (Remember: viewpage.php?art=24 ) */


    get_article($article);
    /*Then we submit the Variable
    "$article" to a function that goes and gets the value
    of "$article" - which, in this case, equals "24".
    So, in other words: you get the value of the posted "?art=" which is "24" and get the corresponding article! (article number 24!)*/
    ?>



    Here is the code without the comments:



    $article = $_GET['art'];
    get_article($article);
    ?>



    Now that wasn't to hard was it? ...was it??? . We got the value of 24 that was on the end of the ("viewpage.php?art=24") URL and got the corresponding article 24 from wherever it was.

    You could also write the code like this and skip putting the value of "$_GET" into a variable:



    get_article($_GET['art']);
    ?>






    I will give you one more example of using "$_GET". Suppose you are looking at a book online. There are 300 pages in that book and you are on page 12. This is what the URL might look like:

    http://www.booksite.com/book.php?page=12

    Each page might have some code like this on it:



    $book_page = $_GET['page']; /* "$book_page" is now equal to "12". (?page=12) */
    get_page($book_page);
    ?>




    Then you decide you want to skip 138 pages and go to page 150. Well, since book.php can be passed values why not edit the URL? If you go up to the address bar and rename the URL from

    http://www.booksite.com/book.php?page=12
    To:
    http://www.booksite.com/book.php?page=150

    You will be telling the page "book.php" to get page 150 (?page=150) instead of page 12 (?page=12).


    Note: This won't work on most sites because they have security measures to keep people from changing the posted values. However, it was useful to help you understand the "$_GET" variable better.




    Now I will teach you about the "$_POST" Superglobal. "$_POST" is different from "$_GET" in that "$_POST" does not show the values being passed in the URL. "$_POST" Hides them in the "Headers" that are sent to the page. In every request that a browser like Firefox or IE makes, they send some information to the site that they are requesting the page from. Such as what the browsers name is and what the IP address they are coming from is, etc... They can also "Post" additional information along with the "Headers". Like what page of the book they are on.

    a "$_POST" URL looks like this:

    http://www.booksite.com/book.php

    with the page number a person is on, sent hidden in the Headers. "$_POST" is a more secure form of passing variables because the average user doesn't know what is going on behind the scenes. The PHP code used to get the posted values is almost identical to the PHP code needed to get the "$_GET" value:


    $book_page = $_POST['page'];
    get_page($book_page);
    ?>



    If you want more information on both of these Superglobals. Please read these Tutorials:


    When you are done please proceed to the next lesson and we will make a script that gets passed different values!


    For more information please read these articles:


    PHP $_GET
    PHP $_POST
    Tizag - Get & Post
    Part 6 - PHP With Forms
    GET, POST and URL encoding
    Examples


    If you find a broken link please PM me, thanks!