Skip to content

Instantly share code, notes, and snippets.

View muathendirangu's full-sized avatar

Charles Ndirangu muathendirangu

View GitHub Profile
@muathendirangu
muathendirangu / dataaccessscriptphp
Last active March 3, 2024 21:09
retrieve data using sql complex join using php
<?php
// Database configuration
$hostname = "your_hostname";
$username = "your_username";
$password = "your_password";
$database = "your_database_name";
try {
// Establish the connection using PDO
@muathendirangu
muathendirangu / authscriptphp
Created March 3, 2024 21:04
access control php script using ouath library
Leverage guzzlehttp that abstracts a lot of oauth functionality
```bash
composer require guzzlehttp/guzzle
```
Now, let's create the PHP script (`oauth_example.php`):
```php
<?php
@muathendirangu
muathendirangu / compressanddecompressphp
Created March 3, 2024 20:58
a PHP script to compress, save and decompress data
<?php
// Define the data to be saved (replace with your actual data)
$data = array(
"name" => "John Doe",
"age" => 30,
"address" => array(
"street" => "123 Main St",
"city" => "Anytown",
"state" => "CA",
@muathendirangu
muathendirangu / asyncprocessingphp
Created March 3, 2024 20:53
Async processing in php using redis
// Breakdown of the script
```bash
composer require predis/predis
```
1. **Update the configuration file (`config.php`):**
```php
<?php
@muathendirangu
muathendirangu / fetchusersphp
Created March 3, 2024 20:31
A php script that retrieve a list of users and displays the name and email
<?php
// Define the API URL
$apiUrl = "https://jsonplaceholder.typicode.com/users";
try {
// Create a cURL resource
$curl = curl_init($apiUrl);
// Set cURL options
@muathendirangu
muathendirangu / countwordsinfilephp
Created March 3, 2024 20:26
Here's a PHP script that reads a text file, counts the words in the file
<?php
// Define the filename
$filename = "file_name.txt";
try {
// Open the file for reading
$file = fopen($filename, "r");
// Check if the file was opened successfully
@muathendirangu
muathendirangu / sumEvenNumbersphp
Created March 3, 2024 20:22
Here is a PHP function that takes an array of integers and returns the sum of all even numbers in the array
<?php
function sumEvenNumbers(array $numbers): int {
$sum = 0;
foreach ($numbers as $number) {
if ($number % 2 === 0) {
$sum += $number;
}
}
return $sum;
@muathendirangu
muathendirangu / VMIACTerraform
Created March 3, 2024 20:14
Here's an example of how you might define a basic virtual machine (VM) on AWS using Terraform
# Configure AWS provider
provider "aws" {
region = "us-east-1" # Specify your desired region
}
# Define a resource for the VM instance
resource "aws_instance" "web_server" {
ami = "ami-0XXXXXXXXXXXXXX" # Amazon Machine Image (pre-built OS)
instance_type = "t2.micro" # VM instance type
tags = {
@muathendirangu
muathendirangu / pdosqlinjectionprotectionphp
Created March 3, 2024 19:52
code example using prepared statements to prevent from SQL injection
<?php
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING); // Basic sanitization
$sql = "SELECT * FROM users WHERE name = :name";
$stmt = $db->prepare($sql);
$stmt->bindValue(':name', $name, PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetchAll();
@muathendirangu
muathendirangu / pdoqueryphp
Created March 3, 2024 19:41
The following code shows how to query all rows from the authors table using PDO
<?php
$pdo = require 'connect.php';
$sql = 'SELECT author_id, name
FROM authors';
$statement = $pdo->query($sql);
// get all authors