HTTP Requests and PHP Pages

Video loading...

  • 0:15
    PHP for Beginners Part 2
  • 0:18
    HTTP Requests and PHP Pages
  • 0:20
    with Leanna Pelham
  • 0:26
    Learning to be a PHP programmer is a lot of work,
  • 0:30
    but totally worth it.
  • 0:31
    So keep going.
  • 0:33
    You're getting more dangerous with each minute you spend practicing.
  • 0:37
    We're going to keep coding on the project from Episode One.
  • 0:40
    If you don't have that code, just download it
  • 0:42
    from the screencast page and unzip it.
  • 0:51
    Once you've done that, we just need to start the PHP web server.
  • 0:55
    Open a terminal and move into the directory
  • 0:57
    where the unzipped files live.
  • 1:02
    I already unzipped the files into a Sites/php directory.
  • 1:06
    Start the PHP web server by typing the following.
  • 1:13
  • 1:14
    Now just put that URL in your browser.
  • 1:22
    And voila.
  • 1:23
    In Episode One, we learned that the web
  • 1:25
    works via requests and responses.
  • 1:28
    Our browser sends an HTTP request message
  • 1:31
    into the interwebs for
  • 1:35
    This eventually finds our server, where it scratches on the door.
  • 1:39
    And with any luck, some web server software like Apache
  • 1:42
    will be listening, open the door, look at a certain directory
  • 1:45
    for the contact file, and process all the PHP treats.
  • 1:50
    I mean tags.
  • 1:53
    The final HTML is called HTTP Response,
  • 1:56
    and is sent from the server back to our browser.
  • 1:58
    So it all starts when we send the HTTP request message.
  • 2:03
    But actually that message has a whole lot more information
  • 2:07
    than just the hostname and page we want.
  • 2:09
    It also has our IP address, info on which browser we're using,
  • 2:13
    and the values of fields when we submit a form.
  • 2:16
    Let's peek at these by going to the debugger on our browser.
  • 2:20
    Click on the Network tab and refresh.
  • 2:23
    Each line here is an HTTP request that was just made by our browser.
  • 2:27
    The top request is for
  • 2:29
  • 2:30
    The other requests are for the CSS, JS, and images on the page.
  • 2:35
    Yep, on every page load, your browser
  • 2:37
    is actually making a bunch of requests into the internet tubes.
  • 2:42
    If you click on contact.php, we can actually
  • 2:45
    see how this HTTP request message looks.
  • 2:49
    Yep, it's a lot of stuff.
  • 2:51
    That user agent is what browser you're using.
  • 2:54
    And Accept-Language is how the browser tells
  • 2:56
    the server what languages you speak.
  • 2:59
    It's not important now, but we can get any of this information
  • 3:02
    from inside PHP.
  • 3:05
    I want to build a form so a user can rent
  • 3:07
    out their pet's love to others.
  • 3:10
    Let's start by creating a new page.
  • 3:13
    How do we do that?
  • 3:14
    Just create a new file called pets_new.php
  • 3:18
    and scream some text out.
  • 3:30
    To see us screaming at us, change the URL to /pets_new.php.
  • 3:36
    So every new page is just a new file.
  • 3:40
    In a future episode, I'll teach you a way
  • 3:42
    to create URLs and pages that's much fancier than this,
  • 3:46
    but don't worry about that quite yet.
  • 3:48
    Let's make this page properly fancy by copying in the header.php
  • 3:53
    and footer.php require lines.
  • 4:06
    Refresh to see a page that is only a little ugly.
  • 4:09
  • 4:11
    And a little Twitter Bootstrap markup
  • 4:13
    makes this look a tiny bit better.
  • 4:35
  • 4:36
    And wouldn't it be nice if we had a link to this page
  • 4:38
    from our top menu?
  • 4:40
    Let's change About to say Post and link to this page.
  • 4:45
    This code lives in header.php.
  • 4:46
    We can also make Home actually go to the home page.
  • 5:02
  • 5:04
    Go team.
  • 5:05
    We have a working nav like a real site.
Loading ...

HTTP Requests and PHP Pages


In this second part of PHP for Beginners, you'll learn all about forms, form submissions, how this translates in the HTTP Request/Response world, and saving files to the disk. This series continues from PHP for Beginners Part 1, and we'll be expanding on that same project in this series.

In this tutorial we'll get things set up and ready to go by setting up our sample code, doing a quick review of the HTTP request, and creating a new page for the site with some navigation.

Log in or sign up to download companion files.