Security Theater
SecTheater is an online teaching community that targets the IT department. We do our best to produce for you high quality and well-edited screen casts about web development.
ISP
Browser
Client (You)
Request
Forwards the request to ISP
What is the IP for the request server?
192.168.1.1
ISP replies to the browser with the IP
Pings the IP with the request
Responds with the web page content
DBMS
192.168.1.1's server
Web page components
Compiler
Interpreter
Compiler
Profiler
Interpreter
Compiler
Lexer
AST
Output
Optimized code
Parsed tokens
Generated bytecode
<?php
$name = "Ahmed";
$age = 19;
$profession = "web developer";
const PI = 3.14;
const SECONDS_IN_DAY = 86400;
Memory address | Label | Value | permission |
---|---|---|---|
0x6dfed4 | name | "ahmed" | rw |
0x7abcd5 | age | 19 | rw |
<?php
$name = "ahmed";
$age = 19;
$profession = "web development";
<?php
const SECONDS_IN_DAY = 86400;
const PI = 3.14;
Memory address | Label | Value | Permission |
---|---|---|---|
0xfcdebe | SECONDS_IN_DAY | 86400 | r |
0xfac28e | PI | 3.14 | r |
Strings
Bools
Numbers
NULL
Integers
Floats
True
False
"ahmed"
1,2,3,4
3.14 , 2.78
Arrays is a collection of data types that could contain any other data type within it
<?php
$names = ['ahmed', 'mohamed', 'mahmoud'];
$numbers = [1, 2, 3, 4, 5, 6];
$users = [
[
'username' => 'ahmed.osama0',
'full_name' => 'Ahmed Osama',
'id' => '19121640',
'phone_number' => '01153820570',
'card_number' => '4242 4242 4242 4242',
'languages' => ['arabic', 'english', 'spanish']
],
];
elements in an array are indexed from the 0th index to nth
<?php
// 0 1 2
$names = ['ahmed', 'mohamed', 'mahmoud'];
$numbers = [1, 2, 3, 4, 5, 6];
Unless they're given some sort of special identifier known as the key
<?php
$user = [
'username' => 'ahmed',
'age' => 19,
// 0 1
'languages' => ['php', 'javascript']
];
Accessing elements is done through its key or index
<?php
$user = [
'username' => 'ahmed',
'age' => 19,
// 0 1
'languages' => ['php', 'javascript']
];
var_dump($user); // returns the whole array
Accessing elements is done through its key or index
<?php
$user = [
'username' => 'ahmed',
'age' => 19,
// 0 1
'languages' => ['php', 'javascript']
];
var_dump($user['username']); // returns ahmed
var_dump($user['languages']); // returns the array of languages
var_dump($user['languages'][0]); // returns php
Changing elements within an array
<?php
$user = [
'username' => 'ahmed',
'age' => 19,
// 0 1
'languages' => ['php', 'javascript']
];
$user['username'] = 'Mohamed';
$user['age'] = 28;
$user['languages'] = ['golang', 'python'];
Array destructuring
The process of extracting values out of an array
<?php
$user_info = [
'first_name' => 'ahmed',
'last_name' => 'osama',
'languages' => ['arabic', 'english', 'spanish'],
'skills' => [
'techincal' => [
'languages' => ['php', 'javascript'],
'frameworks' => ['laravel', 'nest.js', 'react.js', 'vue.js']
],
'public speaking',
'instructing'
]
];
// Extract these information to corresponding variables
<?php
// Arithmetic operators (+ - * / ** ^ ++ -- %)
var_dump(5 ** 2); // 25
var_dump(4 % 2); // 2
var_dump(5 % 2); // 1
$x = 1;
var_dump($x++); // 1
$y = 1;
var_dump($y); // 1
$w = 1;
var_dump(++$w); // 2
$z = 1;
var_dump(--$z); // 0
// Assignment operators ( = += -= %= /= *= .= )
$x = 'ahmed osama';
$x .= 'el-morsy';
$x = 1;
$x += 2; // $x = $x + 2;
// Comparison operators ( == === != <> <= >= < > <=> )
var_dump(1 == 1); // true
var_dump(3 > 2); // true
var_dump(5 <= 4); // false
var_dump(3 != 3); // false
var_dump(4 <> 4); // false
var_dump(3 <=> 4); // -1
var_dump(4 <=> 4); // 0
var_dump(5 <=> 4); // 1
// Spread operator ( ... )
$arr1 = [1,2,3,4,5];
$arr2 = [...$arr1];
// Conditional and logical operators ( [and &&] [OR ||] [not !] [ternary ?:] [null ??] )
var_dump(2 && 1); // true
var_dump(0 && 1); // false
var_dump(-0 || 0); // false
var_dump(!0); // true
var_dump(!-0); // true
$x = 0 ? 1 : 2; // $x = 2
$x = 1 ? 0 : 2; // $x = 0
$x = true ?: false; // $x = true
$y = 0 ?? 1; // $y = 0
$y = true ?? null; // $y = null
$y = null ?? false; $y = false
Type coercion is when the language attempts to change the type of a variable to another type to suit the flow of the program.
<?php
var_dump(true == 1); // true
var_dump(false == 0); // true
var_dump('42' == 42); // true
var_dump(0 == -0); // true
var_dump(true == '1'); // true
var_dump(3 > 2 > 1); // parse error
var_dump((3 > 2) > 1); // false
var_dump(true === 1); // false
var_dump(false === 0); // false
var_dump('42' === 42); // false
var_dump(0 === -0); // true
Typecasting is taking the shape of a data type and hardly shape another type to the desired data type same as casting iron.
<?php
var_dump((int) 'ahmed'); // 0
var_dump((int) '42ahmed'); // 42
var_dump((array) 'ahmed'); // ['ahmed']
var_dump((object) 'ahmed'); // class stdClass { public $scalar = 'ahmed'; }
var_dump((string) -0); // "0" [Wrong answer]
<?php
if ($condition) {
# code...
}
switch ($variable) {
case 'value':
# code...
break;
default:
# code...
break;
}
$value = match ($variable) {};
for ($i=0; $i < 5; $i++) {
# code...
}
foreach ($variable as $key => $value) {
# code...
}
while ($a <= 10) {
# code...
}
do {
# code...
} while ($a <= 10);
<?php
function add($x, $y)
{
return $x + $y;
}
$add = function ($x, $y) {
return $x + $y;
};
$add = fn ($x, $y) => $x + $y;
A function is a process that is done on some certain data that the function receives through its inputs (parameters) and return them to the outside world as outputs.
Parameters of the function
<?php
function add($x, $y)
{
return $x + $y;
}
$add = function ($x, $y) {
return $x + $y;
};
$add = fn ($x, $y) => $x + $y;
A function is a process that is done on some certain data that the function receives through its inputs (parameters) and return them to the outside world as outputs.
Parameters of the function
Normal fn
<?php
function add($x, $y)
{
return $x + $y;
}
$add = function ($x, $y) {
return $x + $y;
};
$add = fn ($x, $y) => $x + $y;
<?php
function add(int $x, int $y): int
{
return $x + $y;
}
Types within functions
Type hinting
Return Types
function add(int $x, int $y): int
{
return $x + $y;
}
Local
Memory
Execution
Return Value
$x
$y
$x
$y
function add(int $x, int $y): int
{
return $x + $y;
}
Local
Memory
Execution
Return Value
$value = add(2, 3);
2
3
return $x + $y;
$x
$y
function add(int $x, int $y): int
{
return $x + $y;
}
Local
Memory
Execution
Return Value
$value = add(2, 3);
2
3
=
=
return $x + $y;
}
5
$x
$y
function add(int $x, int $y): int
{
return $x + $y;
}
Local
Memory
Execution
Return Value
$value = 5;
5
function add(int $x, int $y): int
{
return $x + $y;
}
function subtract(int $x, int $y): int
{
return add($x, -$y);
}
function increase(int $x): int
{
return add($x, 1);
}
function decrease(int $x): int
{
return add($x, -1);
}
<?php
var_dump(
increase(
decrease(
add(
2,
add(
3,
increase(
decrease(
subtract(5, 3)
)
)
)
)
)
)
);
Subtract(5, 3)
add(5, -3)
<?php
$x = [1, 2, 3];
$y = $x;
$y[] = 4;
var_dump($x); // [1, 2, 3];
$x = [1, 2, 3];
$y =& $x;
$y[] = 4;
var_dump($x); // [1, 2, 3, 4];
function counter()
{
static $counter = 1;
return ++$counter;
}
var_dump(counter());
var_dump(counter());
var_dump(counter());
var_dump(counter());
function sum(...$numbers)
{
static $result = 0;
if (count($numbers) === 0) {
return $result;
}
$result = $result + $numbers[0];
return sum(...array_slice($numbers, 1));
}
var_dump(sum(1, 2, 3, 4, 5)); // 15
The most used array functions to manipulate your arrays.
Output buffering functions are used to control the output of your PHP file before sending it to the browser to view.
File operations
Read
File operations
fopen()
<?php
$f = fopen( // Opens a file and stores the resource in $f
);
$f = fopen(
string $filename, // path to the file
string $mode, // the mode with which you'll open the file
[
bool $use_include_path = FALSE,
resource $context
]
);
File operations
$mode
Access mode | What it means |
---|---|
r / r+ | Read (+write) at the beginning of the file |
File operations
$mode
Access mode | What it means |
---|---|
r / r+ | Read (+write) at the beginning of the file |
w / w+ | Write (+read), starts at the beginning |
File operations
$mode
Access mode | What it means |
---|---|
r / r+ | Read (+write) at the beginning of the file |
w / w+ | Write (+read), starts at the beginning |
a / a+ | Write (+read), at the end |
File operations
$mode
Access mode | What it means |
---|---|
r / r+ | Read (+write) at the beginning of the file |
w / w+ | Write (+read), starts at the beginning |
a / a+ | Write (+read), at the end |
x / x+ | Creates and write (+ read), fails if the file exists |
File operations
$mode
Access mode | What it means |
---|---|
r / r+ | Read (+write) at the beginning of the file |
w / w+ | Write (+read), starts at the beginning |
a / a+ | Write (+read), at the end |
x / x+ | Creates and write (+ read), fails if the file exists |
c / c+ | Open for write (+read), without truncating the file |
file functions
Reading
Writing
Creating
Global Scope
All the variables that are accessible anywhere in your file
<?php
$array = [1, 2, 3];
function foo()
{
foreach ($array as $item) {
echo $item;
}
}
Global Scope
All the variables that are accessible anywhere in your file
<?php
$array = [1, 2, 3];
function foo()
{
global $array;
// $arr = $GLOBALS['array'];
foreach ($array as $item) {
echo $item;
}
}
Local Scope
The variables defined within a function and only accessible within the function
<?php
function sayHello(string $name): string
{
$title = 'Mr.';
return "Hello, {$title} {$name}";
}
Lexical scope
The variables defined within a function and only accessible within the function
<?php
function greet($greeting)
{
return function ($name) use ($greeting) {
return "${greeting} ${name}";
// return fn ($name) => "${greeting} ${name}";
};
}
$_POST, $_GET, $_REQUEST
<!DOCTYPE html>
<html lang="en">
<head>
<title>Form request</title>
</head>
<body>
<form action="index.php" method="POST">
<input type="text" name="first_name">
<input type="text" name="last_name">
<input type="submit" />
</form>
</body>
</html>
<?php
var_dump($_POST);
var_dump($_GET);
var_dump($_REQUEST);
$_SERVER
<?php
var_dump($_SERVER);
Stores data about the server and the client sent the request.
<?php
['REMOTE_ADDR' => $ip] = $_SERVER; // getting the client IP address
COOKIES
COOKIES
Websites
Me randomly browsing the web
COOKIES PROPERTIES
Stored in the client side
Stores up to 4KB of data
Can be accessed by the domains created them only
You can store at least 50 cookie per domain
Improves user experience but has security concerns
SETTING COOKIES
<?php
setcookie(
string $name,
string $value = "",
int $expires = 0,
string $path = "",
string $domain = "",
bool $secure = false,
bool $httponly = false
): bool
setcookie(string $name, string $value = "", array $options = [
int $expires = 0,
string $path = "",
string $domain = "",
bool $secure = false,
bool $httponly = false,
string $samesite = "Strict" // None || Lax || Strict
]);
COOKIE MANIPULATION
<?php
// To update a cookie you just reset the value of a cookie
setcookie('name', 'ahmedosama');
// The next request
setcookie('name', 'mahmoud');
// To delete a cookie, just add time() - x to the expiry field
setcookie('name', 'ahmedosama', time() + 86400); // a cookie for 24 hours
// the next request
setcookie('name', 'ahmedosama', time() - 3600); // Deletes a cookie as you're setting
// it backwards, NOTE: 3600 is just an arbitrary number.
SESSIONS
Stored in the server side
Sessions are internal cookies
$_SESSION is defined by default
SESSIONS cannot store resources
SESSIONS have some security concerns must be considered
By Security Theater
The slides used in the Denizey Platform backend track and our course for PHP mastery
SecTheater is an online teaching community that targets the IT department. We do our best to produce for you high quality and well-edited screen casts about web development.