I'm making a login script with a simple manage session feature like Facebook
As a start I have created a table called "users_sessions"
This is the code :
$database->query("
CREATE TABLE `users_sessions` (
`session_id` int(10) UNSIGNED NOT NULL,
`session_date` datetime NOT NULL,
`user_id` int(10) UNSIGNED NOT NULL,
`user_browser` varchar(64) COLLATE utf8mb4_bin NOT NULL,
`user_os` varchar(64) COLLATE utf8mb4_bin NOT NULL,
`user_ip` varchar(64) COLLATE utf8mb4_bin NOT NULL,
PRIMARY KEY (`session_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
");
So what I want to do is when users fill in the box the right username and password automatically the script will insert into this table the right information of the successful user log.
and this is the index.php :
$_POST['password'] = User::hash_password($_POST['password']);
if(!empty($_POST) && empty($_SESSION['error'])) {
/* If remember me is checked, log the user with cookies for 30 days else, remember just with a session */
if(isset($_POST['rememberme'])) {
setcookie('username', $_POST['username'], time()+60*60*24*30);
setcookie('password', $_POST['password'], time()+60*60*24*30);
setcookie('user_id', User::login($_POST['username'], $_POST['password']), time()+60*60*24*30);
} else {
$_SESSION['user_id'] = User::login($_POST['username'], $_POST['password']);
}
redirect();
}
And this is my database config :
<?php
/* CONNECTION */
$database_connection = new StdClass();
$database_connection->server = 'localhost';
$database_connection->username = 'root';
$database_connection->password = '';
$database_connection->name = 'wearesocio';
$database = new mysqli($database_connection->server, $database_connection->username, $database_connection->password, $database_connection->name);
if($database->connect_error) {
header('Location: '.$_SERVER['PHP_URL']."install");
}
/* DB CLASS */
Database::$database = $database;
/* DEBUGGING */
define('DEBUGGING', true);
?>