Categories:

    $_GET Lesson 5

    The $_GET Superglobal

    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. A Superglobal just means that it is a variable with a "global server scope", i.e. you can access the variable from any part of the script. php.net defines it as...

    Quote:
    From version 4.1.0 onward, PHP provides an additional set of predefined arrays containing variables from the web server... the environment, and user input. These new arrays are rather special in that they are automatically global--i.e., automatically available in every scope. For this reason, they are often known as 'autoglobals' or 'superglobals'.

    Don't worry if you are still unclear about global variables - you don't need to worry about the scope of a variable until later. :)

    $_GET

    This is what "$_GET[]" might look like:

    $_GET['page_number'];
    $_GET['username'];
    //etc...
    ?>

    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 ) */


    /*
    We could do lots of things with the value that was
    posted to the script. Like we could print the value
    to the screen
    */
    echo $article;


    /*
    Or we could pass the value of "$article" to a made-up function
    that would show the corresponding article number,
    which in this case is "24".
    */
    get_article($article);
    ?>

    Here is the code without the long comments:

    //Get the value
    $article = $_GET['art'];
    //Print the value
    echo $article;
    //Send the value to a function
    get_article($article);
    ?>

    Now that wasn't to hard was it? :P We got the value of 24 that was on the end of the ("viewpage.php?art=24") URL, then we echoed it to the screen 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:

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

    A Book Site

    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.

    $_GET Script

    Now that you understand posting values a little better - lets make a script! We are going to make a simple 2 line script that takes the value we post the page and prints it to the screen. Open up you text editor and type the following into it.

    $text = $_GET['text'];
    echo
    '
    '. $text. '
    '
    ;
    ?>

    Then save it as "lesson5.php" and then upload it to your webserver. When you run the page by going to it in your browser, you will see a blank screen. So now we have to pass the page some kind of value to print! So in the address bar type the following:

    http://localhost/the/path/to/file/lesson5.php?text=Hello World!
    //Example:
    http://www.mezatech.com/lesson5.php?text=Hello World!

    Now when you visit the page with the extra text on the end you will see that the script echo the value of the text you type. Go ahead and type a bunch of different things onto the end of the url! If you want you can even change the script to your liking!

    $input = $_GET['mywordfortext'];
    echo
    '
    '. $input. '
    '
    ;
    //Change the URL to:
    http://www.mezatech.com/lesson5.php?mywordfortext=This site ROCKS!!!!!!
    ?>

    0 Responses