Explore

Explore snippets

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

Public snippets by CodShot user
Clear
9 public snippets
Open
TypeScript
Public

Next.js Proxy Authentication Guard

Redirect unauthenticated requests from protected routes with the Next.js proxy convention.

TypeScript Preview
// proxy.ts

import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';

export function proxy(request: NextRequest) {
  const sessionId = request.cookies.get('session')?.value;

  if (!sessionId) {
#nextjs #proxy #auth #security
CodShot user
CodShot user
Updated: 2026-08-06 18:36
Public snippets
0 #223
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
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

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

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
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