Skip to content

Instantly share code, notes, and snippets.

View victorezec's full-sized avatar

Victor victorezec

View GitHub Profile
@victorezec
victorezec / JsonResponse.php
Created October 17, 2025 15:38
JSON Response Formatter
<?php
/**
* JsonResponse - Clean, consistent JSON API responses
*/
class JsonResponse {
public static function send($status, $message, $data = [], $code = 200) {
http_response_code($code);
header('Content-Type: application/json');
echo json_encode([
@victorezec
victorezec / Router.php
Created October 17, 2025 15:38
Simple PHP Router
<?php
/**
* Router - Lightweight PHP route handler
* Description: Minimal routing system for small PHP APIs or websites.
*/
class Router {
private $routes = [];
public function get($path, $callback) {
@victorezec
victorezec / Logger.php
Created October 17, 2025 15:36
PHP Logger Class (File Logging)
<?php
/**
* Logger - Simple file logger for PHP
* Description: Logs messages or errors into a daily log file.
*/
class Logger {
private static $logDir = __DIR__ . '/logs/';
public static function write($message, $type = 'INFO') {
@victorezec
victorezec / EnvLoader.php
Created October 17, 2025 15:25
PHP Environment Config Loader
<?php
/**
* EnvLoader - Simple .env file loader for PHP
* Author: Victor E.
* Description: Loads environment variables from a .env file into $_ENV
*/
class EnvLoader {
public static function load($filePath = '.env') {
if (!file_exists($filePath)) {
@victorezec
victorezec / generate_transaction_id.php
Last active October 16, 2025 17:38
Generate a Random Transaction ID
<?php
function generateTransactionID($prefix = 'TXN') {
$time = microtime(true);
$random = bin2hex(random_bytes(4));
return strtoupper($prefix . '-' . dechex($time) . '-' . $random);
}
// This file is intended to generate a transaction ID.
// Ensure that the logic for generating the transaction ID is implemented within this file.
@victorezec
victorezec / curl_api_request.php
Created October 16, 2025 17:33
Simple PHP cURL API Request
<?php
$url = "https://api.example.com/data";
$data = ["username" => "Victor", "token" => "123456"];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$response = curl_exec($ch);
@victorezec
victorezec / file-upload.php
Created October 15, 2025 10:02
Handle File Upload
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$target = "uploads/" . basename($_FILES["file"]["name"]);
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target)) {
echo "File uploaded: " . htmlspecialchars($_FILES["file"]["name"]);
} else {
echo "Upload failed.";
}
}
@victorezec
victorezec / tokem-auth.php
Created October 15, 2025 10:01
Token-Based Authentication Example
<?php
function generateToken($userId) {
$secret = 'my_secret_key';
$payload = [
'user_id' => $userId,
'iat' => time(),
'exp' => time() + (60 * 60) // 1 hour
];
return base64_encode(json_encode($payload)) . '.' . md5($secret);
}
@victorezec
victorezec / db.php
Last active October 15, 2025 09:58
Simple PDO MySQL Connection
<?php
$host = 'localhost';
$db = 'my_database';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
try {