Skip to content

Compiling Server Software From Source

Introduction

  • The Internet is operating on the IP protocol. IP addresses, like this one: 74.125.225.115, mark each participating system.
  • Each website name can be translated into an IP address. This is called DNS (Domain Name Services)
  • The software used in Internet applications can be separated into client-side software and server-side software.
  • A web browser or SSH client are examples of client-side software.
  • Apache, MySQL are examples of server-side software. The server-side software can be accessed via the IP protocol anywhere in the world.

Web Components

CGI 3

  • CGI stands for Common Gateway Interface.
  • The CGI defines the abstract parameters, known as meta-variables that describe a client’s request.
  • meta-variable: A named parameter which carries information from the server to the script.
  • server: The application program that invokes the script in order to service requests from the client.
  • CGI and a concrete programmer interface specify a platform-independent interface between the script and the HTTP server.
  • The server is responsible for managing connection, data transfer, transport and network issues related to the client request.
  • CGI script handles the application issues, such as data access and document processing.
  • How CGI works:
    1. The client sends a request to the server. Then server acts as an application gateway.
    2. Server receives the request from the client.
    3. Server selects a CGI script to handle the request.
    4. Server converts the client request to a CGI request.
    5. Server executes the CGI script passing the CGI request as input. expects the CGI script to produce a CGI response.
    6. Server converts the CGI response into a response for the client (depending on the request http, udp, socket, json, text ..etc).
    7. Server responds to the client.

PHP Installation and Configuration 2

  • Way to use PHP:
    1. Server-side scripting: you need 3 things: a web server, a PHP interpreter (Parser), and a web browser. You can access the PHP script via the web browser.
    2. Command-line scripting: you need 2 things: a PHP interpreter (Parser), and a command-line interface. You can access the PHP script via the command-line interface typing php ./fileName.php.
    3. Desktop applications: PHP-GTK is a PHP extension that allows you to create desktop applications (and/or cross-platform applications) using PHP.
  • PHP works as either:
    1. a module: a shared library that can be loaded into a web server (Apache, IIS, etc.) to extend its functionality.
    2. a CGI processor: a program that runs as a separate process and communicates with the web server via standard input/output.
  • SAPI (Server Application Programming Interface):
    • is a layer between the web server and the PHP interpreter.
    • SAPI is a module that interfaces between the web server and the PHP interpreter.
    • SAPI -by default- supports all famous web servers (Apache, IIS, etc.).
    • If your web server is not supported by SAPI, you can use CGI executable of PHP (a file named php-cgi.exe on windows, similar files on other OSes) directly. In this case, PHP will be used as a CGI processor and it will execute the PHP script as a separate process, then returns back to the client directly -no server in the middle-.
    • see this

PHP Tutorial 1

  • PHP is an acronym for “PHP: Hypertext Preprocessor
  • current version: PHP 7 (released on 3 Dec 2015).
  • In PHP, keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are not case-sensitive. echo = ECHO = Echo = eChO = eCHo = etc.
  • Variable names are case-sensitive. $color = "red"; $COLOR = "blue"; $Color = "green";
  • comments: // or # or /* */
  • string concatenation: . (dot) or += (plus-equals) or “string interpolation” (PHP 5.3+) as in echo "Hello $name"; or echo "Hello {$name}"; or echo "Hello {$name}s";.
  • PHP is a Loosely Typed Language:
    • PHP automatically associates a data type to the variable, depending on its value.
    • In PHP 7, type declarations were added.
    • If PHP runs on strict it will not perform type juggling, and throw a TypeError exception instead.
  • PHP has three different variable scopes:
    1. local: declared inside a function, only accessible within that function
    2. global:
      • variable defined outside a function has global scope and can only be accessed outside a function.
      • global keyword: to access a global variable from within a function, use the global keyword before the variables (inside the function).
      • PHP also stores all global variables in an array called $GLOBALS[index]
    3. static:
      • declares a static variable, which retains its value between function calls.
  • Data Types: string, integer, float, boolean, array, object, NULL, resource.
  • PHP Resource:
    • The special resource type is not an actual data type.
    • It is the storing of a reference to functions and resources external to PHP.
  • constants:
    • defined by this syntax: define(variable_name: string, value: any, case-insensitive: boolean = false);
    • constants are global by default.
  • reference for built-in functions
  • strict mode:
    • turned on by declare(strict_types=1); at the beginning of the script.
    • if a function is called with the wrong number of arguments, an ArgumentCountError will be thrown…etc.
  • Passing arguments to functions:
    • usually PHP passes arguments by value, so the function will not change the value of the argument.
    • to pass arguments by reference, use the & operator in the function declaration.
  • PHP SuperGlobals: variables that are always available in all scopes.
    • $GLOBALS - global scope
    • $_SERVER - server and execution environment information
    • $_REQUEST - HTTP request variables
    • $_POST - HTTP POST variables
    • $_GET - HTTP GET variables
    • $_FILES - HTTP File Upload variables
    • $_ENV - environment variables
    • $_COOKIE - HTTP Cookies
    • $_SESSION - session variables
// ----------------------- strings -----------------------
$str = "Hello World!";
strlen($str); // 12
str_word_count($str); // 2 // (Hello, World!)
strrev($str); // !dlroW olleH // (reverse)
strpos($str, "World"); // 6 // search for "World" in $str
str_replace("World", "Dolly", $str); // Hello Dolly! // replace "World" with "Dolly" in $str
// php string functions: http://php.net/manual/en/ref.strings.php

// ----------------------- numbers -----------------------
$x = 5985;
is_int($x); // true // check if the type of a variable is integer
is_float($x); // false // (is_double)
is_integer($x); // true // alias of is_int()
is_long($x); // true // alias of is_integer()

$s1 = "10.5";
is_numeric($s1); // true // (is_int or is_float on a string)

$d = 10.5;
$int_cast = (int)$d; // 10 // cast float to int
$int_cast = intval($d); // 10 // cast float to int

$x = acos(8);
var_dump($x); // NAN // (Not a Number) since it is impossible to calculate the arc cosine of 8

// ----------------------- math -----------------------
pi(); // 3.1415926535898
min(0, 150, 30, 20, -8, -200); // -200
max(0, 150, 30, 20, -8, -200); // 150
abs(-6.7); // 6.7 // absolute value
sqrt(64); // 8 // square root
round(0.60); // 1 // round to nearest integer
rand(); // 456 // random integer
rand(0, 10) // random integer between 0 and 10

// -----------------------  operators -----------------------
$x = 10;
$y = 6;

// arithmetic
echo $x <> $y; // 1 // (not equal)
echo $x <=> $y; // 1 // (spaceship) returns 0 if values of $x and $y are equal, 1 if $x is greater than $y, -1 otherwise
++$x; // 11 //  pre-increment : increments $x by one, then returns $x
$x++; // 10 // post-increment : returns $x, then increments $x by one

// logical
$x and $y; // true // and same as &&
$x or $y; // true // or same as ||
$x xor $y; // false // xor

// arrays
$x = array("a" => "red",);
$y = array("c" => "blue",);

echo $x + $y; // { "a": "red", "c": "blue" } // union of $x and $y
echo $x == $y; // false // equality, returns true if $x and $y have the same key/value pairs
echo $x === $y; // false // identity, returns true if $x and $y have the same key/value pairs in the same order and of the same types
echo $x != $y; // true // inequality, returns true if $x is not equal to $y // same as <>

// conditional assignment
$x = NULL;
$y = $x ?? 200; // 200 // Null coalescing
$y = $x ?: 200; // 200 // Ternary operator // same as $y = ($x != NULL) ? $x : 200;

// -----------------------  loops -----------------------
// while
while (condition is true) {
 code to be executed;
}

// do...while
do {
  code to be executed;
} while (condition is true);

// for
for (init counter; test counter; increment counter) {
  code to be executed for each iteration;
  works as long as the test counter is true
}

// foreach
foreach ($array as $key => $value) {
  code to be executed;
}

// -----------------------  functions -----------------------
function addNumbers(int $a, int $b): int {
  return $a + $b;
}

// pass by reference
$x = 0;
function addFive(&$num) {
  $num += 5;
}
addFive($x); // $x is now 5

// -----------------------  arrays -----------------------
// associative array
$array = array("foo" => "bar", 12 => true); // { "foo": "bar", "12": true }
// indexed array
$arr = array ("a", "b", "c", "d", "e"); // { "0": "a", "1": "b", "2": "c", "3": "d", "4": "e" }
// multidimensional array
$multi = array(
  array(1,2,3),
  array(4,5,6),
  array(7,8,9)
);

$arr = array(1,2,3)
count($arr); // 5 // count elements in array
sort($arr); // { "0": 1, "1": 2, "2": 3 } // sort arrays in ascending order
rsort($arr); // { "0": 3, "1": 2, "2": 1 } // sort arrays in descending order
asort($arr); // { "0": 1, "1": 2, "2": 3 } // sort associative arrays in ascending order, according to the value
ksort($arr); // { "0": 1, "1": 2, "2": 3 } // sort associative arrays in ascending order, according to the key
arsort($arr); // { "0": 3, "1": 2, "2": 1 } // sort associative arrays in descending order, according to the value
krsort($arr); // { "0": 3, "1": 2, "2": 1 } // sort associative arrays in descending order, according to the key


// -----------------------  objects -----------------------
class Car {
    public $color;
    private $model;
    static $wheels = 4;
    function __construct($color, $model) {
        $this->color = $color;
        $this->model = $model;
    }

}
$carObj = new Car('red', 'myModel'); // create object

// -----------------------  regular expressions -----------------------
$re = '/[a-z]/'; // match any lowercase letter
$str = 'Hee';
$matches = array();
// preg_match(pattern, subject, matches)
preg_match($re, $str, $matches); // 1 // 1 match found $matches = array("e")
// preg_match_all(pattern, subject, matches)
preg_match_all($re, $str, $matches); // 8 // 8 matches found $matches = array("e", "e")
// preg_replace(pattern, replacement, subject)
preg_replace($re, 'X', $str); // 'HXX' // returns a new string with all matches replaced by the replacement string

References


  1. PHP 5 Tutorial. (n.d.). w3schools.com. Retrieved from https://www.w3schools.com/php/default.asp 

  2. Cowburn, P. (Ed.). (2018). PHP manual. PHP Documentation Group. Retrieved from http://php.net/manual/en/install.php 

  3. RFC 3875 https://www.rfc-editor.org/rfc/rfc3875.html