We Engineering Revision Notes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Require and require_once

- Require: Allows multiple includes of the same file

require 'file.php';
require 'file.php'; //Allowed

- Require_once: Ensures file is included only once

require_once 'file.php';
require_once 'file.php'; //Only included once

Print and print_r

- Print: Outputs a string

print "Hello World";

- Print_r: Prints array in human readable format

print_r([1, 2, 3]);

SOAP and REST

- SOAP is a protocol, REST is an architectural style


- SOAP uses XML messaging, REST simpler formats like JSON
- SOAP has strict formats, REST is flexible

SOAP vs REST Architecture Diagramq


Salt and Stretched Algorithm

- Salt: Random data added before hashing passwords


- Stretched: Performs multiple hash iterations to slow down brute force attacks. Eg. bcrypt.

Sessions and Cookies


- Sessions: Store sensitive user data on server
- Cookies: Save preferences on client side
Sessions more secure than cookies for critical data.

SOAP and REST Based Applications

SOAP Based:
- Healthcare Information Systems
- Banking Applications
- Finance Applications
Government Applications
REST Based:
- Content Management Systems
- Weather Forecasting Systems
- Blogging Websites
- Flight Booking Websites

Ways of Interacting in PHP


- HTML Forms
- GET/POST Requests
- AJAX Requests
- Cookies/Sessions

Features of REST APIs:


- Stateless
- Cacheable
- Uniform Interface
- Client-Server separation
- Layered System

Web Service Diagram


Constructing Semantic HTML Forms:
(Html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Survey Form</title>
</head>

<body>
<h1>My Survey</h1>

<form id="survey-form" method="POST" action="/submit-answers">


<div>
<label for="name">Name:</label>
<input type="text" id="name" name="name" maxlength="20">
</div>

<div>
<label for="age">Age:</label>
<input type="number" id="age" name="age" max="99">
</div>
<button type="submit">Submit Responses</button>
</form>
</body>
</html>

Added more semantics with language attribute, descriptive IDs/names, labels etc.

Javascript Form Validation:


function validateSurvey() {

var name = document.getElementById("name");


var age = document.getElementById("age");

if(name.value.length === 0) {
alert("Please enter your name");
return false;
}

if(age.value === "") {


alert("Please enter your age");
return false;
}
return true;
}

Checks specifically for empty inputs before submitting.


Inserting Survey Records into Database:
(Php)
$insertSql = "INSERT INTO survey_responses (name, age) VALUES (?, ?)";

$stmt = $dbConnection->prepare($insertSql);

$stmt->bind_param("si", $name, $age);

$result = $stmt->execute();

if(!$result) {
throw new Exception("Error inserting survey response");
}

Laravel Code Concepts


The Laravel PHP framework promotes clean code organization through models, views and
controllers. Creating new projects sets up folder structure and dependencies. Generating controllers
houses app logic for data flows. Routes connect URLs to controllers/actions. Views render frontend
content.
code samples:

(php)
// Create new app skeleton
Laravel new MyApp

// Make controller
Php artisan make:controller FormController

// Basic route
Route::get(‘/form’, ‘FormController@show’);

// Render view
Return view(‘form’);

Laravel utilizes many other powerful features like Eloquent ORM database interfacing,
authentication, queues and caches to construct robust applications.

Building Dynamic Client Experiences


Modern web expectations demand snappy experiences without full page reloads. Client-side
JavaScript leverages asynchronous techniques like AJAX to call backend APIs, process updates and
selectively render updated content into the existing page DOM. This facilitates faster partial updates.

// AJAX request (js)


Var xhr = new XMLHttpRequest();
Xhr.open(“GET”, “/new_data.php”);

Xhr.onload = function() {
// Handle response
updatePage(xhr.response);
}
Xhr.send();

Function updatePage(data) {
// Render into DOM
}

Benefits include improved perceived performance and flexibility. Tradeoffs include more client-side
work and considerations around JavaScript support.

SQL Injection Vulnerabilities

Developers often construct database queries by combining hard-coded SQL with unfiltered user
inputs. This allows attackers to manipulate the execution by injecting malicious code. For example:

$sql = “SELECT * FROM users WHERE name = ‘” + $input + “’” ;

If $input contains crafted strings like ‘; DROP TABLE users;’, it would alter query execution arbitrarily.

Instead, parameterized queries separate code from input:

$sql = “SELECT * FROM users WHERE name = ?”;

$stmt->execute([$input]);

Binding variables eliminates injection risks. Input validation is still required to filter unexpected
values.

Securely Storing Passwords

Cryptographic hashes generate fixed length fingerprints of arbitrary data like passwords. Early
algorithms such as MD5 were once considered decent for non-critical uses but found vulnerable to
new attacks.

Modern techniques apply additional security measures:

- Adaptive functions like BCrypt designed against custom hardware


- Randomly generated salts to combat rainbow tables
- Multiple compute-intensive rounds to slow cracking

Regular audits critical as hash strengths weaken over time against evolving hardware.
Code samples:
// Salted bcrypt password hash
$hash = password_hash($password, PASSWORD_BCRYPT, [‘cost’ => 10, ‘salt’ => $salt]);

You might also like