Skip to content

Instantly share code, notes, and snippets.

@maxixo
Created January 13, 2026 23:02
Show Gist options
  • Select an option

  • Save maxixo/023e6f453e7d48adeb5c52bc2b8c4c0f to your computer and use it in GitHub Desktop.

Select an option

Save maxixo/023e6f453e7d48adeb5c52bc2b8c4c0f to your computer and use it in GitHub Desktop.
Sanitizing inputs in php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>User Input</title>
</head>
<body>
<?php
// Define the function first
function sanitize_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$name = $email = $website = $comment = "";
if($_SERVER['REQUEST_METHOD'] == "POST") {
$name = sanitize_input($_POST["name"]);
$email = sanitize_input($_POST["email"]);
$website = sanitize_input($_POST["website"]);
$comment = sanitize_input($_POST["comment"]);
echo "<h3>Sanitized Data:</h3>";
echo "Name: $name <br>";
echo "Email: $email <br>";
echo "Website: $website <br>";
echo "Comment: $comment <br>";
}
?>
<form method="POST" action="">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email"><br>
Website: <input type="text" name="website"><br>
Comment: <textarea name="comment"></textarea><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment