// db.php - Database connection and configuration
// Place this file in the same directory as your main file
// --- Session Start & Authentication Check ---
session_set_cookie_params([
'lifetime' => 0,
'path' => '/',
'domain' => '',
'secure' => isset($_SERVER['HTTPS']),
'httponly' => true,
'samesite' => 'Lax'
]);
session_start();
date_default_timezone_set('Africa/Nairobi');
// Check if user is logged in
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) {
header("Location: login.php");
exit;
}
// If you want only admins to access a specific page, add:
if (isset($_SESSION['role']) && $_SESSION['role'] !== 'admin') {
header("Location: mobilefarm.php"); // or show error
exit;
}
// --- Logout functionality ---
if (isset($_GET['logout'])) {
session_destroy();
header("Location: login.php");
exit;
}
// --- DB Connection ---
$db_host = 'localhost';
$db_user = 'muhokoda_user';
$db_pass = 'Emillio1521#';
$db_name = 'muhokoda_farm';
$conn = new mysqli($db_host, $db_user, $db_pass, $db_name);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// --- AUTO-TAG GENERATION FUNCTION ---
function getNextTag($conn) {
$result = $conn->query("SELECT MAX(tag) as max_tag FROM Cows WHERE tag REGEXP '^[A-Za-z]*[0-9]+$'");
if ($result && $row = $result->fetch_assoc()) {
$max_tag = $row['max_tag'];
if (preg_match('/([A-Za-z]*)(\d+)/', $max_tag, $matches)) {
$prefix = $matches[1];
$number = intval($matches[2]);
return $prefix . str_pad($number + 1, 3, '0', STR_PAD_LEFT);
}
}
return 'M001'; // Default starting tag
}
// --- Helpers ---
function esc($v){
return htmlspecialchars($v ?? '', ENT_QUOTES, 'UTF-8');
}
function statusBadge($s){
$map = [
'Calf' => 'secondary',
'Heifer' => 'info',
'Bull' => 'dark',
'Pregnant' => 'success',
'Lactating' => 'primary',
'Dry' => 'warning',
'Sold' => 'danger'
];
$c = $map[$s] ?? 'secondary';
return "" . esc($s) . "";
}
function getAge($dob){
if(!$dob || $dob == '0000-00-00') return "Unknown";
try {
$from = new DateTime($dob);
$to = new DateTime("today");
$diff = $from->diff($to);
$y = $diff->y;
$m = $diff->m;
$d = $diff->d;
if($y > 0) return $y . " yr" . ($y > 1 ? "s" : "") . ($m > 0 ? " " . $m . " mo" : "");
if($m > 0) return $m . " mo" . ($m > 1 ? "s" : "");
return $d . " day" . ($d > 1 ? "s" : "");
} catch(Exception $e) {
return "Unknown";
}
}
// Consistent date functions
function todayDate() {
return date('Y-m-d');
}
function todaySQL() {
return date('Y-m-d');
}
?>
Fatal error: Uncaught Error: Call to undefined function todaySQL() in /home3/muhokoda/public_html/index.php:6
Stack trace:
#0 {main}
thrown in /home3/muhokoda/public_html/index.php on line 6