-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_database.php
42 lines (34 loc) · 1.08 KB
/
session_database.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
// Start PHP session
session_start();
include('MysqliDb.php');
include('db_cred.php');
$db = new MysqliDb($db_host, $db_user, $db_pass, $db_database);
// Echo server hostname, and PHP session cookie ID
echo 'This server is ' . gethostname() . '</br>';
echo 'cookie is: ' . $_COOKIE['PHPSESSID'];
echo '<br><br>';
// If the user has submitted the login form, log them in
if(isset($_POST['username']) && $_POST['username'] != '') {
$data = Array();
$data['session_id'] = $_COOKIE['PHPSESSID'];
$data['username'] = $_POST['username'];
$db->insert('sessions', $data);
}
//Get the session from the database
$db->where('session_id', $_COOKIE['PHPSESSID']);
$session = $db->getOne('sessions');
// Tell the user if they're logged in or not
if(isset($session) && $session['username'] != '') {
echo 'You are logged in as <b>' . $session['username'] . '</b>';
die();
} else {
echo 'You are <b>not</b> logged in';
}
echo '<br>';
?>
<form action="" method="POST">
<input type="text" name="username"></input>
</br>
<input type="submit" value="Login"></input>
</form>