-
Notifications
You must be signed in to change notification settings - Fork 0
/
save.php
42 lines (33 loc) · 1.19 KB
/
save.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
require 'config.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Create connection
$conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Generate random ID with a random length between 1 and 24
$randomLength = rand(6, 24);
$randomId = generateRandomId($randomLength);
// Prepare and execute the INSERT statement
$stmt = $conn->prepare("INSERT INTO pastes (id, content, syntax, pastename) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssss", $randomId, $_POST['content'], $_POST['syntax'], $_POST['name']);
$stmt->execute();
// Close the connection
$stmt->close();
$conn->close();
// Redirect to the paste
header("Location: view.php?id=" . $randomId);
die();
}
// Function to generate random ID
function generateRandomId($length) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomId = '';
for ($i = 0; $i < $length; $i++) {
$randomId .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomId;
}
?>