Day 10 – Web Programming

Zend PHP Certification

Okay, so I’m a day late, I’m sorry.  Last night was very busy for me.  Anyway, the next part is all about Web Programming.  I’m going to assume you know basic HTML and form programming (as I do).


Get/Post Data

  • The most basic of parameter passing in Web Development is by the GET method.  It appends encoded data at the end of the URL string:
    http://www.stjohnjohnson.com/test.php?variable=value&more=value2
  • You can also create arrays by using brackets:
    http://www.stjohnjohnson.com/test.php?var[a]=1&var[b]=2&tar[]=3
  • This GET data is accessible in PHP via the $_GET superglobal:
    echo $_GET['variable']; // Displays: value
    1. echo $_GET['var']['a']; // Displays: 1
    2. echo $_GET['var']['b']; // Displays: 2
    3. echo $_GET['tar'][0]; // Displays: 3
  • When writing URLs with GET data, the values of the variables need to be safely encoded using urlencode($value)
  • POST data works the same way as GET data, except there is no need to urlencode it as it doesn’t append to the end of the URL.  You can access it via the $_POST superglobal.
  • Remember, GET data is visible to the user (in the URL), so passwords and long amounts of data should be done via POST.
  • If you don’t know where the data is coming from, you can check the $_REQUEST superglobal variable (also contains Cookie data).

Uploading Files

  • All uploaded files are required to be done via a "multi part" POST transaction via the following HTML code:

    1.   <input type="hidden" name="MAX_FILE_SIZE" value="5120" />
    2.   <input name="uploaded" type="file" />
    3.   <input type="submit" />
    4. </form>
  • Note that the MAX_FILE_SIZE is required and is designed to tell the client not to waste their time uploading anything larger than that.  You can set the server-side version by using the upload_max_filesize configuration directive.
  • Once the file is uploaded, it’s moved to a temporary location and destroyed when the script ends, so you have to use or move the file before the end of the script.
  • After the upload, you can access the file information in the $_FILES superglobal.  Each uploaded file (in that upload) is an array with the following keys:
    1. name: Original name of the file.
    2. type: Mime type of the file (not always trustable).
    3. size: Size in bytes.
    4. tmp_name: File’s temporary name in the temporary folder.
    5. error: Error code, it’s good if it equals UPLOAD_ERR_OK
  • Files should be checked to ensure no hacker is maliciously uploading things.  You can use is_uploaded_file($_FILES[$uploadname]['tmp_name'])
  • And finally, if you want to store the file anywhere, use the move_uploaded_file($_FILES[$uploadname]['tmp_name'], $newlocation)
    No Comments

    Leave a Reply

    Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



    • Donate

      If my work has helped you and you want to return the favor, you could purchase something for me from my Amazon Wish List or send me a donation via PayPal.

    • My Lifestream

    • License

      Unless otherwise noted, all source code and compiled files published on this website are released under the terms of the GNU Lesser General Public License.