... Advanced Web Programming ...

C4. Server Side Scripting


Server Side Scripting technologies allow creation of complex Web applications. They support server side data processing and generate Web pages dynamically. Therefore, Web applications can interface with database servers, RSS servers or mail servers.

Server Side Script languages are able to access data coming from HTML forms and implements libraries to access external resources such as a database. Unlike older technologies that achieve the same goal (eg. CGI) server side scripting ensures better speed and security. Server Side Scripting languages interpreters are integrated usually as modules in Web servers. Sample Server Side languages: PHP, ASP, JSP, ColdFusion, Perl, Rubi.

PHP
The PHP language is a server side scripting language designed for Web scripting. As for JavaScript, the PHP code lines have to be embedded within a HTML page. Using PHP it is easily to build:
 users authentication mechanisms 
 content management systems
 Web based e-mail clients
 message list management
 Web forums
 automatic generation of PDF documents

The PHP code has to be processed by the Web server. Therefore, the Web server load the PHP page from the file system and sends it to the PHP interpreter. The PHP interpreter executes all PHP instructions found in the page. The main goal of the PHP code is to generate HTML. All the HTML code from the original file is simply pass to the client. PHP parser forwards to the server the result of PHP code interpretation. This stream will contain only HTML (and maybe client side scripting code), but not PHP code.

To include a PHP sequence into an HTML page the corresponding tag is:

<?php
...
?>

The PHP syntax is very close to C. Specific to PHP: all variable names begins with $.

The basic PHP data types are:
Integer - for integer numbers
Double - for real numbers
String - for character strings
Array - for non-homogeneous arrays 
Object - for PHP objects
Specific libraries also brings additional data types like PDFlib or gd_info.

To access data from HTML forms two predefined associative array variables could be used: $ _POST and $ _GET (depending on sending method used by the form).

To increase the efficiency of application development ready-written code reuse is preferred. The first way for reusing is by using function libraries. These libraries are written in external .php files that can be included in the current application using include or require. Unlike include, the require load the referred page whether the code execution reaches this statement or not. The second way for reusing is objects and OOP features included in PHP starting with version 5.0. More information could be found at PHP Objects page.

PDFLib
PDFlib is a development tool for PDF generation from a PHP application. It offers a simple-to-use API for creating PDF files.
The library could be downloaded from Pecl PDFlib page.

Main PDFLib functions are:
> int PDF_add_textflow(resource $pdfdoc, int $textflow, string $text, string $optlist);
> bool PDF_begin_page_ext(resource $pdfdoc, float $width, float $height, string $optlist);
Obs: Standard dimensions: A4 595 x 842, A3 842 x 1190.
> bool PDF_circle(resource $pdfdoc , float $x , float $y , float $r);
> bool PDF_continue_text(resource $p, string $text);
> bool PDF_end_document(resource $pdfdoc, string $optlist);
> bool PDF_end_page(resource $p);
> bool PDF_fit_image(resource $pdfdoc, int $image, float $x, float $y, string $optlist);
> bool PDF_lineto(resource $p, float $x, float $y);
> int PDF_load_font(resource $pdfdoc, string $fontname, string $encoding, string $optlist);
> int PDF_load_image(resource $pdfdoc, string $imagetype, string $filename, string $optlist);
> bool PDF_moveto(resource $p, float $x, float $y);
> resource PDF_new(void);
> bool PDF_save(resource $p);
> bool PDF_setcolor(resource $p, string $fstype, string $colorspace, float $c1, float $c2, float $c3, float $c4);
> bool PDF_show(resource $pdfdoc, string $text);
> bool PDF_show_xy(resource $p, string $text, float $x, float $y);

PDFLib example (a brief CV):
$pdf = pdf_new();
if (!pdf_open_file($pdf, ""))
       die("Error: Fail to open output file.");
pdf_set_info($pdf, "Author", $name." <". $email.">");
pdf_set_info($pdf, "Title", "CV -".$name);
pdf_set_info($pdf, "Subject", "The CV of ".$name.".");
define("PAGE_HEIGHT", 842); /* A4 */
define("PAGE_WIDTH", 595);
define("VERT_SPACING", 14);
pdf_begin_page($pdf, PAGE_WIDTH, PAGE_HEIGHT);
// Print name
$font = pdf_findfont($pdf, "Times-Bold", "winansi", 0);
pdf_setfont($pdf, $font, 14.0);
$stringwidth = pdf_stringwidth($pdf, $nume, $font, 14.0);
$xpos = (PAGE_WIDTH / 2) - ($stringwidth / 2);
pdf_show_xy($pdf, $name, $xpos, 700);
$xpos = pdf_get_value($pdf, "textx", 0);
$ypos = pdf_get_value($pdf, "texty", 0) - VERT_SPACING;
// Print personal information.
$font = pdf_findfont($pdf, "Times-Roman", "winansi", 0);
pdf_setfont($pdf, $font, 12.0);
$headerdata = array($street, $town, $country, $zip, $tel, $email);
pdf_end_page($pdf);
pdf_close($pdf);
$buf = pdf_get_buffer($pdf);
$len = strlen($buf);
header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=cv.pdf");
print $buf;

Symfony

Symfony is a full-stack MVC framework for PHP applications. It provides an architecture, components and tools for developers to build complex web applications faster.

A brief introduction could be found at

symfony Screencast

Symfony screencast.

A usefull tutorial was proposed by Fabien Potencier.

CodeIgniter
CodeIgniter is an ADF (Application Development Framework) for people who build web sites using PHP. Its goal is to enable you to develop projects much faster than you could if you were writing code from scratch. It has a small footprint, broad compatibility and ensures simple solutions.

For more informations please consult CodeIgniter Web page.

Slides:
C4. Server Side Scripting.