Explore

Explore snippets

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

Public snippets by CodShot user
Clear
105 public snippets
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
C#
Public

Simple Class with Property

Create a small class with properties and instantiate it.

C# Preview
using System;

Product product = new()
{
    Name = "Keyboard",
    Price = 49.99m
};

Console.WriteLine($"{product.Name}: {product.Price:C}");

class Product
{
    public string Name { get; set; } = "";
    public decimal Price { get; set; }
}
#csharp #class #oop #beginner
CodShot user
CodShot user
Updated: 2026-06-08 14:33
Public snippets
0 #108
Open
C#
Public

Read All Lines from File

Read a text file into lines and print each line with its index.

C# Preview
using System;
using System.IO;

string path = "notes.txt";

if (!File.Exists(path))
{
    Console.WriteLine("File not found.");
    return;
}

string[] lines = File.ReadAllLines(path);

for (int i = 0; i < lines.Length; i++)
{
#csharp #file #text #beginner
CodShot user
CodShot user
Updated: 2026-06-08 14:33
Public snippets
0 #107
Open
C#
Public

Count Words with Dictionary

Count repeated words with a Dictionary in a simple console example.

C# Preview
using System;
using System.Collections.Generic;

string text = "csharp is useful and csharp is fun";
string[] words = text.ToLower().Split(' ');

Dictionary<string, int> counts = new();

foreach (string word in words)
{
    if (!counts.ContainsKey(word))
    {
#csharp #dictionary #string #count
CodShot user
CodShot user
Updated: 2026-06-08 14:33
Public snippets
0 #106
Open
C#
Public

Filter a List with LINQ

Filter a list of numbers using a LINQ Where expression.

C# Preview
using System;
using System.Collections.Generic;
using System.Linq;

List<int> numbers = new() { 3, 8, 12, 15, 20, 21 };

List<int> evenNumbers = numbers
    .Where(number => number % 2 == 0)
    .ToList();

Console.WriteLine(string.Join(", ", evenNumbers));
#csharp #linq #list #filter
CodShot user
CodShot user
Updated: 2026-06-08 14:33
Public snippets
0 #105
Open
C#
Public

Read Console Input Safely

Read a number from the console without crashing on invalid input.

C# Preview
using System;

Console.Write("Enter your age: ");
string? input = Console.ReadLine();

if (int.TryParse(input, out int age))
{
    Console.WriteLine($"You are {age} years old.");
}
else
{
    Console.WriteLine("Please enter a valid number.");
}
#csharp #console #input #beginner
CodShot user
CodShot user
Updated: 2026-06-08 14:33
Public snippets
0 #104
Open
PowerShell
Public

Remove Empty Folders

Remove empty folders under the current directory.

PowerShell Preview
Get-ChildItem -Directory -Recurse |
    Sort-Object FullName -Descending |
    Where-Object { -not (Get-ChildItem -Path $_.FullName -Force) } |
    Remove-Item -Force
#powershell #folders #cleanup #windows
CodShot user
CodShot user
Updated: 2026-06-03 15:31
Public snippets
0 #103
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
PowerShell
Public

Create Project Folders

Create a simple frontend project folder structure.

PowerShell Preview
$folders = @(
    "src",
    "public",
    "assets",
    "assets/css",
    "assets/js",
    "assets/img"
)

foreach ($folder in $folders) {
    New-Item -ItemType Directory -Path $folder -Force | Out-Null
}

Write-Host "Project folders created."
#powershell #folders #project #beginner
CodShot user
CodShot user
Updated: 2026-06-03 15:31
Public snippets
0 #101
Open
PowerShell
Public

List Large Files

Find large files in the current folder and subfolders.

PowerShell Preview
Get-ChildItem -Recurse -File |
    Where-Object { $_.Length -gt 100MB } |
    Sort-Object Length -Descending |
    Select-Object FullName, @{Name="SizeMB"; Expression={[math]::Round($_.Length / 1MB, 2)}}
#powershell #windows #files #cleanup
CodShot user
CodShot user
Updated: 2026-06-03 15:31
Public snippets
0 #100
Open
PowerShell
Public

Rename Images with Counter

Rename JPG images in a folder with a clean numbered filename.

PowerShell Preview
$counter = 1

Get-ChildItem -Filter *.jpg | ForEach-Object {
    $newName = "image-{0:D3}.jpg" -f $counter
    Rename-Item -Path $_.FullName -NewName $newName
    $counter++
}
#powershell #windows #rename #images
CodShot user
CodShot user
Updated: 2026-06-03 15:31
Public snippets
0 #99
Open
Regex
Public

Match Date YYYY-MM-DD

Match a simple ISO-style date format.

Regex Preview
^\d{4}-\d{2}-\d{2}$
#regex #date #validation #format
CodShot user
CodShot user
Updated: 2026-06-03 15:31
Public snippets
0 #98
Open
Regex
Public

Find HTML Tags

Find simple opening or closing HTML tags in text.

Regex Preview
<\/?[a-zA-Z][a-zA-Z0-9-]*(?:\s[^>]*)?>
#regex #html #search #beginner
CodShot user
CodShot user
Updated: 2026-06-03 15:31
Public snippets
0 #97
Open
Regex
Public

Validate Hex Color

Match CSS hex colors like #fff or #a1b2c3.

Regex Preview
^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$
#regex #css #color #validation
CodShot user
CodShot user
Updated: 2026-06-03 15:31
Public snippets
0 #96
Open
Regex
Public

Extract Numbers from Text

Find integer and decimal numbers inside a string.

Regex Preview
\d+(?:\.\d+)?
#regex #numbers #text #extract
CodShot user
CodShot user
Updated: 2026-06-03 15:31
Public snippets
0 #95
Open
Regex
Public

Match Email Address

A practical beginner regex for basic email-like text matching.

Regex Preview
^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$
#regex #email #validation #beginner
CodShot user
CodShot user
Updated: 2026-06-03 15:31
Public snippets
0 #94
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
Bash
Public

Show Changed Files

Show which files changed before you commit.

Bash Preview
git status --short

git diff --name-only
#git #diff #status #files
CodShot user
CodShot user
Updated: 2026-06-03 15:31
Public snippets
0 #92
Open
Bash
Public

Stash Work in Progress

Temporarily save unfinished changes and restore them later.

Bash Preview
git stash push -m "work in progress"

git stash list

git stash pop
#git #stash #workflow #student
CodShot user
CodShot user
Updated: 2026-06-03 15:31
Public snippets
0 #91
Open
Bash
Public

Undo Last Commit Keep Changes

Undo the last commit but keep the changed files in your working directory.

Bash Preview
git reset --soft HEAD~1
#git #undo #commit #beginner
CodShot user
CodShot user
Updated: 2026-06-03 15:31
Public snippets
0 #90
Open
Bash
Public

Create and Switch Branch

Create a new Git branch and switch to it in one command.

Bash Preview
git switch -c feature/login-page
#git #branch #workflow #beginner
CodShot user
CodShot user
Updated: 2026-06-03 15:31
Public snippets
0 #89
Open
SQL
Public

Search Multiple Columns

Search several text columns with LIKE placeholders.

SQL Preview
SELECT id, title, description
FROM snippets
WHERE title LIKE :search_title
   OR description LIKE :search_description
   OR language_code LIKE :search_language
ORDER BY updated_at DESC;
#sql #search #like #prepared-statements
CodShot user
CodShot user
Updated: 2026-06-03 15:31
Public snippets
0 #88
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