Day 04 at rtCamp

PHP Core Concepts

Input Sanitization & Validation

function test_input($input) {
$input = trim($input);
$input = stripslashes($input);
$input = htmlspecialchars($input);
return $input;
}

Validation Examples:

// Email
filter_var($email, FILTER_VALIDATE_EMAIL);

// Name
preg_match("/^[a-zA-Z-' ]*$/", $name);

// URL
preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9...]/i", $website);

Database with PDO

Connection:

$conn = new PDO("mysql:host=$servername", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

Insert Records Using Prepared Statements:

$stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email)
VALUES (:firstname, :lastname, :email)");

$stmt->bindParam(':firstname', $firstname);
// ...
$stmt->execute();

API Integration with GitHub

A working code sample is available here view Full PHP Code on GitHub

Write code for the Collecting data from the Gihub Timeline using this code:

$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, 'https://github.com/timeline');
curl_setopt($curl, CURLOPT_CAINFO, '/etc/ssl/cert.pem');
$contents = curl_exec($curl);

Threads in PHP

<?php
if (!class_exists('Thread')) {
class Thread {
protected $threadId;
public function __construct() {
$this->threadId = uniqid('thread_', true);
}

public function getCurrentThreadId() {
return $this->threadId;
}

public function getCurrentThread() {
return $this;
}

public function start() {
echo "Thread started with ID: " . $this->getCurrentThreadId() . "\n";
$this->run();
}

public function join() {
echo "Thread with ID: " . $this->getCurrentThreadId() . " has joined.\n";
}

public function run() {
echo "Running thread with ID: " . $this->getCurrentThreadId() . "\n";
}
}
}

class CustomThread extends Thread {
public function run() {
echo "Custom thread running with ID: " . $this->getCurrentThreadId() . "\n";
}
}

$thread1 = new CustomThread();
$thread2 = new CustomThread();

$thread1->start();
$thread2->start();

$thread1->join();
$thread2->join();

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *