Recent Changes - Search:

Homepage for the PHP course PmWiki edit SideBar

PHP /

Get introduced to the PHP Programming Language, part 1..

Lecture 3, Get introduced to the PHP Programming Language, part 1.. March 7..

By now, everyone should have a working server. This week, we are going to dive into the language proper. We are going to be looking at:

  • opening and closing PHP tags, though we did see some last week.
  • Some rules of the PHP language, including how (and why) to comment your code and find and fix errors introduced by breaking the rules of language
  • Basic variables
  • Basic function calls
  • Basic branches

We'll use a Attach:l3code.zip Set of code example files

during this, and the next lecture. You can download them from the above link, and should use them as a basis to explore and play around. You'll be walked through them, one character at a time to begin with, to understand right from the ground up why they work and how they do what they do. You will then be encouraged to look at the files yourself, to experiment with them, and to complete a short assignment to cement your understanding of what you've learned. Note that there are 10 files included, the second half are for next week though you can play with them if you feel happy about it beforehand.

But before we can get into the language itself, we'll need to have an understanding of how http requests work, since they are literally how everything communicates on the web, including your PHP scripts.

What Is an HTTP Request?

Hyper Text Transfer Protocol (HTTP) is the networking protocol that is no less than the foundation of data communication for the World Wide Web.

HTTP Request Concepts

Even if you cannot name or explain the following concepts yet, you have already experienced them in your everyday life online, using a web browser: HTTP is a request/response protocol in the client/server computing model.

Client/server — An application (the client) talks to another application (the server) that itself can respond to many clients at the same time. In the HTTP model, a client is, for example, a web browser, such as Firefox running on your computer, and the server is a web server powered, for instance, by Apache, which is also running PHP. A client can also be a web indexing spider robot or a PHP script that fetches and parses a web page to retrieve information. Request/response protocol — The client submits an HTTP request (basically “Hello, I’m Firefox, please send me file example.html”) and the server sends back a response (“Hello, I’m the Apache running PHP; here is the file, it is 4kb in size,” followed by the file itself). Both requests contain potentially interesting information you learn to decipher and use.

Dissecting an HTTP Transaction

An HTTP transaction is a simple and clear text communication between the client and the server.

The Client Sends a Request

The client request typically consists of a few lines sent in clear text to the server. Using Firefox as a web browser and trying to load http://example.com/file.html from a Google result page would translate into the following query: GET /file.html HTTP/1.1 Host: www.example.com User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0) Firefox/3.6 Referer: http://www.google.com/search?q=example.com Cookie: lastvisit=235456684

The first line starts with GET: A GET session is how you tell the server you want to retrieve a document, here file.html from host example.com. Other main requests methods you can use are HEAD (to just receive the server response headers) and POST (to submit data to a form).

Notice also how information such as the referrer URL or the user agent string is also sent by the client. When writing your PHP code, it is very important that you remember that this data cannot be trusted without verification, because it can be easily forged. The Server Sends a Response

The server response consists of three parts: the headers, with information about the response, a blank line, and then the response body.

The headers are a few lines of information and can be something like this: HTTP/1.1 200 OK Date: Mon, 23 May 2012 22:38:34 GMT Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux) Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT Set-Cookie: lastvisit=235456951 Content-Length: 438 Content-Type: text/html; charset=UTF-8

The first interesting information is the status code, here 200. Each server response should have a status code giving details on how the transaction was handled by the server: 200 means OK, 404 means not found. If you're really interested in all of the codes, (and you should have a basic familiarity with the important ones, like 200, 404, 500), you can find them all here: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes

Status Code Signification 200 OK 301 Moved Permanently 302 Moved Temporarily 403 Forbidden 404 Not Found 500 Internal Server Error 503 Service Unavailable

Of course, you don’t have to memorize all these status codes, but with some experience you can quickly remember the main classes.

HTTP Status Code Classes

Status Code Signification 2xx Request was successful. 3xx Request was redirected to another resource (like in the case of a URL shortener). 4xx Request failed because of a client error (for instance, a wrong username/password combination). 5xx Request failed because of a server error (like a bad configuration or a broken script).

The server response also generally discloses information about the software running on the server, the content-type of the document it serves, and its length.

Possibilities for Crafting HTTP Requests

The first obvious use of HTTP requests is to retrieve a remote document or particular information within a remote document: a Twitter user’s last message, the current value of share stock, or JSON encoded data from a remote API service.

You can also send information to a remote document, such as a form or an HTTP API, and modify data from a client script.

These requests would be done using either GET or POST methods, sometimes with credentials (a login and password or another authentication mechanism) or other parameters. Another interesting application, using HEAD requests, is to check the state of a remote document without bothering downloading its content. For instance, a broken link checker script could make sure your bookmarks don’t return a 404 header.

There are multiple ways to send http requests using PHP, and each way has drawbacks and advantages over others: Some are simple and quicker to write, and some allow more parameters for finer control, support different request methods, or are faster to execute. The problem is this: Depending on the server setup and configuration, PHP version, or security settings, some methods won’t be allowed or even available. When working for a specific client, you could adapt to its specific server architecture and use a method you know will work, but this is simply impossible when authoring a script you intend to release for broad use.

We'll discuss all of this in more depth later in the course, but this section should give you a brief overview of http requests. And given that a lot of your work is going to be all http requests, all the time, it's something you'll want to keep handy, even after you finish this course.

Assignment for lecture 3.

Answer the following questions in an email to submit-php@ciscovision.org

1. Which symbol is used to identify a PHP variable? And why is a symbol necessary?

2. Why should you always open your PHP tags the long way?

3. What is wrong with the following code?

<?php
print ("Hello World")
?>

4. What is wrong with the following code?

<?php
print ("Hello World');
?>

7. The expressions that are evaluated in conditional statements are usually enclosed in parentheses, (). Which characters enclose the code that runs depending on that evaluation?

8. What would the following code print to the browser?

<?php
$orders=100;
$orders++;
print("There have been $orders orders.");
?>

9. Extend the code above so that, assuming one order makes £14.95, the total amount of money made so far is printed along with the order total. You may use your own local currency symbol (US Dollar, Euro, etc).

Go back to the course overview page

Edit - History - Print - Recent Changes - Search
Page last modified on March 05, 2016, at 07:24 AM