Below is a simple example of a PHP script that demonstrates how to implement a basic user ban system. This example will include a database table for users, and you’ll see how to ban a user by updating their status in the database.
First, create a database and a table to hold the user data. Here’s an example SQL statement to create a simple users table:
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) NOT NULL,
is_banned TINYINT(1) DEFAULT 0
); Next, here’s a ban_user.php script that takes a username and bans that user by updating the is_banned field in the database.
<?php
// Database connection details
$host = 'localhost';
$db = 'your_database_name';
$user = 'your_database_user';
$pass = 'your_database_password';
$charset = 'utf8mb4';
// Set up the PDO connection
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
$pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
// Function to ban a user
function banUser($username, $pdo) {
$sql = "UPDATE users SET is_banned = 1 WHERE username = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$username]);
if ($stmt->rowCount() > 0) {
echo "User '$username' has been banned successfully.";
} else {
echo "User '$username' not found or already banned.";
}
}
// Check if a username is provided
if (isset($_POST['username'])) {
$username = $_POST['username'];
// Call the function to ban the user
banUser($username, $pdo);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Ban User</title>
</head>
<body>
<h1>Ban a User</h1>
<form method="post">
<label for="username">Username:</label>
<input type="text" name="username" required>
<button type="submit">Ban User</button>
</form>
</body>
</html> banUser function takes a username and updates the is_banned field to 1 (true) in the database if the user exists.banUser function.This guide provides a detailed walkthrough of installing and configuring an Apache web server and…
WordPress development has evolved significantly, and modern tooling plays a crucial role in creating efficient…
I. Project Overview The goal is to automate the process of notifying search engines (like…
1. Database Structure (MySQL) We'll need a database table to store information about our website's…
This explanation aims to provide a solid foundation for understanding the process and implementing your…
Okay, here's a comprehensive guide on building a real-time website chat script using PHP, HTML,…