How to Restrict Expired Users from Accessing Your PHP System (Full Code Guide)
Leo nakupa full working solution ambayo:
✔️ Inakagua kama user ame-expire
✔️ Inamzuia asifike kwenye page yoyote
✔️ Inamrudisha dashboard pekee
✔️ Inamwonyesha ujumbe wa kulipia
✔️ Ni simple, fast, secure, na production-ready
🗄️ Database Structure Requirement
Katika table yako ya watumiaji (mfano: users), hakikisha una column:
id
username
password
date_to_expire
Mfano wa siku ya ku-expire:
2025-01-30
🧠 Logic ya Kuzuia Page Zote
👉 Ukaguzi unafanyika mara tu user anapoingia kwenye ukurasa wowote
👉 Kama date_to_expire < leo, basi:
Hawezi kufungua pages zingine
Ataishia kwenye dashboard
Anaonyeshwa ujumbe wa kulipia
🧩 FULL PHP EXPIRATION CHECK (Reusable)
Hiki kitakuwa file unachoki-include kwenye pages ZOTE:
file: check_expire.php
<?php
session_start();
require 'config.php';
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit;
}
$user_id = $_SESSION['user_id'];
try {
$pdo = new PDO("mysql:host=$servername;dbname=$dbname;charset=utf8mb4", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $e) {
die("DB Error: " . $e->getMessage());
}
// Fetch user expiration data
$stmt = $pdo->prepare("SELECT username, date_to_expire FROM users WHERE id = ?");
$stmt->execute([$user_id]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
// Safety check
if (!$user) {
header("Location: logout.php");
exit;
}
$today = date("Y-m-d");
$isExpired = false;
// Check expiration
if (!empty($user['date_to_expire']) && $today > $user['date_to_expire']) {
$isExpired = true;
$_SESSION['expired_message'] = "
<div style='text-align:center;padding:25px;border-radius:10px;background:#ffdddd;border:1px solid #ff8888;'>
<h2 style='color:#c00;'>⛔ Akaunti Yako Ime-Expire</h2>
<p style='font-size:17px;'>
Ili kuendelea kutumia huduma, tafadhali fanya malipo upya au wasiliana na
<b>FAULINK</b> kupitia
<a href='https://faulink.com' target='_blank'>Website</a>
au piga: <b>0693 118 509</b>
</p>
<a href='dashboard.php' style='padding:10px 20px;background:#007bff;color:white;border-radius:6px;text-decoration:none;font-weight:bold;'>Rudi Dashboard</a>
</div>
";
}
?>
🛑 HOW TO BLOCK OTHER PAGES
Katika kila file jingine (students.php, reports.php, contributions.php, etc.) weka juu:
<?php
require 'check_expire.php';
// Block expired users
if ($isExpired) {
echo $_SESSION['expired_message'];
exit;
}
?>
Hii inazuia user asiingie page zingine.
🏠 Dashboard.php (User Allowed + Message Shown)
Dashboard itaonyesha ujumbe kama user ame-expire:
<?php
require 'check_expire.php';
?>
<!DOCTYPE html>
<html>
<head>
<title>Dashboard</title>
</head>
<body>
<h2>Karibu, <?= htmlspecialchars($user['username']) ?></h2>
<?php if ($isExpired): ?>
<?= $_SESSION['expired_message']; ?>
<?php else: ?>
<!-- dashboard normal content hapa -->
<p>System Status: Active</p>
<?php endif; ?>
</body>
</html>
🎯 What This System Achieves
✔️ Users wenye ku-expire hawawezi kuingia kwa mafile yoyote
✔️ Wanabaki dashboard tu
✔️ Wanapata ujumbe wa malipo
✔️ Pages zingine hazifunguki bila kuwa active