I'm trying to set cookies. The problem is if I change in browser the cookie username I can log as any user without the password.
login.php
<?php
if (empty($_POST) === false) {
$username = $_POST ['username'];
$password = $_POST ['password'];
if (empty($username) === true || empty ($password) === true) {
$errors [] = 'er1';
} else if (user_exists($username) === false) {
$errors [] = 'er2';
} else {
$login = login($username, $password);
if ($login === false) {
$errors [] = 'er3';
} else {
setcookie("username", $username, time()+3600*24*30);
$_SESSION['user_id'] = $login;
header('Location: logged_in.php');
exit();
}
}
}
if (empty($errors) === false) {
echo output_errors($errors);
}
?>
function user.php
function logged_in () {
return (isset($_SESSION['user_id']) || isset($_COOKIE['username'])) ? true : false;
}
function login($username, $password) {
user_id = user_id_from_username($username);
$username = sanitize ($username);
$password = md5($password);
$query = mysql_query("SELECT COUNT('user_id') FROM `users` WHERE username = '$username' AND password = '$password'");
return (mysql_result($query, 0) == 1) ? $user_id : false;
}
core init.php
<?php
ob_start();
session_set_cookie_params(3600*24*30, "/");
session_start();
//error_reporting(0);
require 'database/connect.php';
require 'functions/general.php';
require 'functions/users.php';
if (logged_in() === true) {
$session_user_id = $_SESSION['user_id'];
$user_data = user_data($session_user_id, 'user_id', 'password', 'username', 'email', 'age', 'gender', 'country', 'city', 'image_id');
}
$errors = array();
?>
Can anyone help me to secure the cookie? If the user changes the cookie username in the browser, redirect him to index and logout?