PHP: Introduction CS7025: Network Technologies and Server Side Programming http://www.scss.tcd.ie/~luzs/t/cs7025/ Lecturer: Saturnino Luz January 24, 2011 1 PHP: Introduction How does a browser request a page from the server? 1. A connection is established over the internet 2. The client requests HTML 3. The server returns HTML 4. The client requests Image 1 5. The server returns Image 1 6. The client requests Applet 7. The server returns Applet 8. The connection is closed. HTTP The request the client makes may be for a document that resides on the server. This would be a static request. But we are interested in dynamic requests. The document is put together on the fly by the server, and returned. The page may contain data that are extracted from a database. User preferences, Identification: A cookie may have been set which uniquely identifies the user When the server receives a request that includes this cookie it recognizes the user and can tailor the page to the users requirements. 1
Web Applications The early web was information driven Largely static Posting of research information But now it is largely service driven The browser provides an interface to the service Largely dynamic Highly interactive Combining server and client side programming Usability becomes a serious concern Dynamic pages Mostly use the Three Tier Architecture: Presentation tier >GET SALES TOTAL >GET SALES TOTAL 4 TOTAL SALES Client Logic tier Web Server GET LIST OF ALL SALES MADE LAST YEAR ADD ALL SALES TOGETHER Data tier QUERY SALE 1 SALE 2 SALE 3 SALE 4 Database Server Database Storage Server side programming Within a web server we might: Identify a client (with a cookie) Retrieve relevant data (from a database) Process it based on the client query Build an HTML page around the processed information Record client feedback and update the database So we will need a server-side scripting module as well as an HTTP server 2
CGI Java servlets PHP ASP ColdFusion CGI - Common Gateway Interface PHP A standard for running external programs from a WWW HTTP server. CGI specifies how to pass arguments to the executing programs. Also defines a set of environment variables. Typically the program will compose some HTML which is passed back to the browser. The HTTP server is usually configured to treat certain URL s in a special way - instead of treating it as a file to be returned it is a program which should be run, and the output of the program is returned. Each time the CGI program is launched it is started afresh CGI programs can be quite resource intensive PHP stands for PHP: Hypertext Pre-processor Has evolved from a bag of hacks to a complete language for building dynamic web sites. Cross platform (Unix (including Linux, OS X etc), Windows) Runs on the server. PHP instructions should never reach the browser (it wouldn t know what to do with them anyway). Mixed content - PHP programs usually embedded in (X)HTML documents How does it work? Server-side language PHP is inserted into XML pages Interpreted by PHP engine, running on web server (script checked for correctness and then executed). Surrounding XML fragments along with output of script passed back to browser. Web server configured to recognise when XML documents should be passed through PHP engine (usually by having the file names end in.php or something similar). Unlike CGI scripts the PHP scripts are embedded within documents in the servers and processed by a separate PHP engine. 3
PHP scripts A PHP script is embedded within an (X)HTML document, using special markers: <html> <body> The area of the circle is: $value = 3.14159 * 76; echo $value; </body> </html> The echo command prints out a value (we can also use print for this). Variables in PHP Variables are used to store data within our PHP scripts Unlike JavaScript we do not need to declare variables before we use them: using a new name automatically creates a new variable. Variable names have the same rules as JavaScript names except that all variable names must begin with the $ symbol. A number of variables are pre-defined in PHP scripts How PHP works 1. The browser requests a resource from the server 2. The request is received by the server 3. The server recognises that the request is for a PHP document (and not, say, a normal HTML document) 4. The server calls the PHP engine which processes the script and returns the HTML to the server process 5. The server returns the HTML document to the browser Our first PHP script <html> <body> echo "Hello world"; </body> </html> 4
A second program We have some pre-defined programs in a PHP program that is being executed by a web server. The variable names are a bit odd: echo $_SERVER[ HTTP_USER_AGENT ]; Variable types in PHP In PHP, as in JavaScript, there are several variable types available. String, e.g. $name = "Hercules"; Numeric Integer, e.g. $integer = -32; Numeric floating point, or Double, e.g. $double = 2.0; Boolean, e.g. $rainy = true; Array, in two flavours: Numeric, e.g. $country[0] = "Ireland"; Associative, e.g. $country["ie"] = "Ireland"; Object (more about this later on) Variable types in PHP Normally PHP will decide automatically what type a variable has, choosing from the available types. But if you really want to specify it, then you can use an operation called a cast. If you do this, then PHP will convert the value of the variable for you, according to the rules for type conversion it has built in. $stringvariable = "14.2 Words"; $numericvariable = (double)$stringvariable; After this conversion we can do arithmetic on the variable $numericvariable. PHP will often do the conversion automatically for us, but sometimes we will want to specify it manually. This is a useful facility when taking data out of HTML form elements. References Davis, M. and Phillips, J. (2007). Learning PHP and MySQL. O Reilly Media, Inc., 2nd edition. 5