There are 3 pages in my script 1. header.php 2. auth.php 3. home.php
The header file has the login code and the functions and database connections are in the auth.php.
Now login is working, the only thing is cookie, i mean I want to know how to add a remember function in the script please help. This is my code 1. header.php
include 'auth.php';
if(empty($_POST['submitlogin']) === false)
{
$username = $_POST['username'];
$password = $_POST['password'];
if(empty($username) === true)
{
$errors[] = '<font color="#FFFFFF">You need to enter an username!</font>';
}
if(empty($password) === true)
{
$errors[] = '<font color="#FFFFFF">You need to enter password!</font>';
}
else if(user_exists($username) === false )
{
if(empty($username) === true){}
else
$errors[] = '<font color="#FFFFFF">No such username has been found.</font>';
}
else if(user_active($username) === false)
{
$errors[] = '<font color="#FFFFFF">You haven\'t activated your account.</font>';
}
else
{
$login = login($username, $password);
if($login === false)
{
$errors[] = '<font color="#FFFFFF">The Username/Password Combination is incorrect</font>';
}
else
{
$_SESSION['user_id'] = $login;
header('Location: home.php');
exit();
}
}
}
2. auth.php code
session_start();
error_reporting(0);
require 'connect.php';
if(logged_in() === true){
$session_user_id = $_SESSION['user_id'];
$user_data = user_data($session_user_id, 'user_id', 'username', 'password', 'email', 'active', 'type');
if(user_active($user_data['username']) === false)
{
session_destroy();
header('Location: home.php');
exit();
}
}else{
}
$errors = array();
function user_data($user_id)
{
$data = array();
$user_id = (int)$user_id;
$func_num_args = func_num_args();
$func_get_args = func_get_args();
if($func_num_args > 1)
{
unset($func_get_args[0]);
$fields = '`' . implode('`, `', $func_get_args) . '`';
$data = mysql_fetch_assoc(mysql_query("SELECT $fields FROM `edge_users` WHERE `user_id` = $user_id"));
return $data;
}
}
function user_active($username)
{
$username = sanitize($username);
return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `edge_users` WHERE `username` = '$username' AND `active` = 1"), 0)==1)?true: false; }
function login($username, $password)
{
$user_id = user_id_from_username($username);
$username = sanitize($username);
$password = md5($password);
return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `edge_users` WHERE `username` = '$username' AND `password` = '$password'"), 0) == 1) ? $user_id : false;
}
Html Form
<form method="post">
Username
<input type="text" name="username" style="padding: 5px; width:228px; height:15px;" autocomplete="off" value="<?php echo $_POST['username'];?>"/>>
Password
<input type="password" name="password" style="padding: 5px; width:228px;height:15px;" />
<input type="checkbox" name="rememberme" /> Remember Me
<input type="submit" name="submitlogin" style="background: #00c800; border:1px solid #00c800; color:#FFFFFF; padding:5px 10px 5px 10px; font-size:14px; float: right;" value="Login" />
</form>