Explore

Explore snippets

Discover reusable code snippets, examples, fixes, and ideas shared by developers, students, and teams.

Public snippets by CodShot user
Clear
17 public snippets
Open
PHP
Public

cURL GET Request in PHP

Call an external API with cURL and decode the JSON response.

PHP Preview
<?php

$ch = curl_init('https://api.example.com/items');

curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 10,
]);

$responseBody = curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
#php #curl #api #json
CodShot user
CodShot user
Updated: 2026-06-08 14:33
Public snippets
0 #128
Open
PHP
Public

PHP Read JSON Request Body

Read and validate a JSON request body in PHP.

PHP Preview
<?php

$rawBody = file_get_contents('php://input');
$data = json_decode($rawBody, true);

if (!is_array($data) || json_last_error() !== JSON_ERROR_NONE) {
    http_response_code(400);
    echo json_encode(['error' => 'Invalid JSON body.']);
    exit;
}
#php #api #json #validation
CodShot user
CodShot user
Updated: 2026-06-08 14:33
Public snippets
0 #127
Open
PHP
Public

PHP JSON Response Helper

Return a JSON response from a small PHP endpoint.

PHP Preview
<?php

function json_response(array $payload, int $status = 200): void
{
    http_response_code($status);
    header('Content-Type: application/json; charset=utf-8');
    echo json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    exit;
}
#php #api #json #response
CodShot user
CodShot user
Updated: 2026-06-08 14:33
Public snippets
0 #126
Open
PHP
Public

Basic Upload Validation

Validate upload error, size, and MIME type before accepting a file.

PHP Preview
<?php

$file = $_FILES['avatar'] ?? null;

if (!$file || $file['error'] !== UPLOAD_ERR_OK) {
    throw new RuntimeException('Upload failed.');
}

if ($file['size'] > 2 * 1024 * 1024) {
    throw new RuntimeException('The file is too large.');
}
#php #upload #validation #security
CodShot user
CodShot user
Updated: 2026-06-08 14:33
Public snippets
0 #113
Open
PHP
Public

Safe Redirect Allow List

Redirect only to known internal paths instead of trusting user input.

PHP Preview
<?php

$allowedRedirects = [
    '/my-dashboard',
    '/my-workspace',
    '/my-billing',
];

$next = $_GET['next'] ?? '/my-dashboard';

if (!in_array($next, $allowedRedirects, true)) {
    $next = '/my-dashboard';
}

header('Location: ' . $next);
exit;
#php #redirect #security #validation
CodShot user
CodShot user
Updated: 2026-06-08 14:33
Public snippets
0 #112
Open
PHP
Public

Regenerate Session After Login

Regenerate the PHP session ID after a successful login.

PHP Preview
<?php

session_start();

// After checking the username and password successfully:
session_regenerate_id(true);

$_SESSION['client_id'] = $client['id'];
$_SESSION['client_email'] = $client['email'];
$_SESSION['logged_in_at'] = time();
#php #session #auth #security
CodShot user
CodShot user
Updated: 2026-06-08 14:33
Public snippets
0 #111
Open
PHP
Public

Password Hash and Verify

Hash a password and verify it later with PHP built-in helpers.

PHP Preview
<?php

$password = $_POST['password'] ?? '';
$hash = password_hash($password, PASSWORD_DEFAULT);

// Later, during login:
$valid = password_verify($password, $hash);

if ($valid) {
    echo 'Password is correct';
} else {
    echo 'Invalid password';
}
#php #password #auth #security
CodShot user
CodShot user
Updated: 2026-06-08 14:33
Public snippets
0 #110
Open
PHP
Public

PDO Insert with Named Parameters

Insert a row safely with PDO named parameters.

PHP Preview
<?php

$sql = "INSERT INTO contacts (name, email) VALUES (:name, :email)";
$stmt = $pdo->prepare($sql);

$stmt->execute([
    ':name' => trim($_POST['name'] ?? ''),
    ':email' => trim($_POST['email'] ?? ''),
]);

$contactId = (int) $pdo->lastInsertId();
#php #pdo #security #sql
CodShot user
CodShot user
Updated: 2026-06-08 14:33
Public snippets
0 #109
Open
PowerShell
Public

Find Text in Files

Search for a word or phrase inside files recursively.

PowerShell Preview
Select-String -Path "*.php" -Pattern "TODO" -Recurse |
    Select-Object Path, LineNumber, Line
#powershell #search #files #todo
CodShot user
CodShot user
Updated: 2026-06-03 15:31
Public snippets
0 #102
Open
Bash
Public

Reset One File to Last Commit

Discard local changes from one file and restore the committed version.

Bash Preview
git restore path/to/file.php
#git #restore #file #undo
CodShot user
CodShot user
Updated: 2026-06-03 15:31
Public snippets
0 #93
Open
Batch
Public

Find Text in Files

A Windows CMD command that searches for text inside files.

Batch Preview
@echo off

findstr /s /i /n "TODO" *.php

pause
#cmd #windows #search #files
CodShot user
CodShot user
Updated: 2026-06-03 14:05
Public snippets
0 #77
Open
PHP
Public

Safe HTML Escape Helper

A small PHP helper for escaping output in HTML templates.

PHP Preview
<?php

function e(string $value): string
{
    return htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
}

$title = '<script>alert("xss")</script>';

echo '<h1>' . e($title) . '</h1>';
#php #security #xss #helper
CodShot user
CodShot user
Updated: 2026-06-03 14:05
Public snippets
0 #73
Open
PHP
Public

Group Array Items by Key

A useful PHP helper for grouping database rows by a selected key.

PHP Preview
<?php

function groupByKey(array $items, string $key): array
{
    $groups = [];

    foreach ($items as $item) {
        $groupKey = (string) ($item[$key] ?? '');

        if ($groupKey === '') {
            continue;
        }
#php #array #utility #beginner
CodShot user
CodShot user
Updated: 2026-06-03 14:05
Public snippets
0 #72
Open
PHP
Public

CSRF Token Input

A simple PHP pattern for rendering and validating a CSRF token.

PHP Preview
<?php

session_start();

if (empty($_SESSION['csrf_token'])) {
    $_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}

function csrfInput(): string
{
    $token = htmlspecialchars($_SESSION['csrf_token'], ENT_QUOTES, 'UTF-8');
#php #csrf #security #form
CodShot user
CodShot user
Updated: 2026-06-03 14:05
Public snippets
0 #71
Open
PHP
Public

Simple Slug Generator

A compact PHP slug generator for URLs and filenames.

PHP Preview
<?php

function makeSlug(string $text): string
{
    $text = strtolower(trim($text));
    $text = preg_replace('/[^a-z0-9]+/i', '-', $text);
    $text = trim((string) $text, '-');

    return $text !== '' ? $text : 'item';
}
#php #slug #string #utility
CodShot user
CodShot user
Updated: 2026-06-03 14:05
Public snippets
0 #70
Open
PHP
Public

PDO Select One Row

A safe PDO example for selecting a single row with a prepared statement.

PHP Preview
<?php

function findUserByEmail(PDO $pdo, string $email): ?array
{
    $stmt = $pdo->prepare(
        'SELECT id, email, firstname, lastname
         FROM users
         WHERE email = :email
         LIMIT 1'
    );

    $stmt->execute([
#php #pdo #sql #security
CodShot user
CodShot user
Updated: 2026-06-03 14:05
Public snippets
0 #69
Open
JavaScript
Public

Filter an Array of Objects

A beginner-friendly JavaScript example for filtering objects by search text.

JavaScript Preview
const snippets = [
  { title: 'PHP Slug Generator', language: 'php' },
  { title: 'CSS Loading Spinner', language: 'css' },
  { title: 'JavaScript Debounce', language: 'javascript' }
];

function searchSnippets(items, query) {
#javascript #array #filter #beginner
CodShot user
CodShot user
Updated: 2026-06-03 14:05
Public snippets
0 #68
CodShot AI Help
Ask about CodShot features, plans, workspace, AI limits, referrals, and public help topics.
Hi! I can help you understand how CodShot works. What would you like to know?
For account-specific or sensitive issues, please open a support ticket. Open support