Commit bc307129 authored by Aleksander Paravac's avatar Aleksander Paravac
Browse files

Implemented some basic session handling

parent cbd68e6b
<?php
// =============================================================================================
// Load MCOS Class
// =============================================================================================
include_once ('libs/mcos.php');
if (!class_exists('mcos2')) {
die ("Could not load mcos2 ..\n");
}
// =============================================================================================
// Init Site
// =============================================================================================
$site = new mcos2;
$site->title = "MAGIC Conference System"; // Long Title
$site->name = "MCOS2"; // Short Title
// =============================================================================================
// Default settings
// =============================================================================================
$debug = true;
if ($debug) {
error_reporting(E_ALL);
ini_set("display_errors", 1);
}
else {
error_reporting(E_ALL);
ini_set("display_errors", 0);
}
?>
\ No newline at end of file
/* ============================================================================================= */
/* Load fonts*/
/* ============================================================================================= */
@font-face {
font-family: 'Open Sans';
font-style: normal;
......@@ -5,6 +8,9 @@
src: local('Open Sans'), local('OpenSans'), url(fonts/OpenSans/OpenSans-Regular.ttf) format('truetype');
}
/* ============================================================================================= */
/* MCOS desgin*/
/* ============================================================================================= */
body {
font-family: 'Open Sans', 'Helvetica Neue', Helvetica, sans-serif;
}
......@@ -15,4 +21,47 @@ body {
/*.mcos h1 {
text-shadow: 4px 4px 4px #aaa;
}*/
\ No newline at end of file
}*/
/* ============================================================================================= */
/* Signin form*/
/* ============================================================================================= */
.form-signin {
max-width: 330px;
padding: 15px;
margin: 0 auto;
}
.form-signin .form-signin-heading,
.form-signin .checkbox {
margin-bottom: 10px;
}
.form-signin .checkbox {
font-weight: normal;
}
.form-signin .form-control {
position: relative;
height: auto;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
padding: 10px;
font-size: 16px;
}
.form-signin .form-control:focus {
z-index: 2;
}
.form-signin input[type="email"] {
margin-bottom: -1px;
border-bottom-right-radius: 0;
border-bottom-left-radius: 0;
}
.form-signin input[type="password"] {
margin-bottom: 10px;
border-top-left-radius: 0;
border-top-right-radius: 0;
}
<?php
include ('config/config.inc.php');
if (isset($_COOKIE['PHPSESSID']) && !empty($_COOKIE['PHPSESSID'])) {
if(isset($_COOKIE['mcos2']) && ($_COOKIE['mcos2'] == $_COOKIE['PHPSESSID'])) {
header ('Location: welcome.php');
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>MCOS 2</title>
<title><?php echo htmlspecialchars($site->title);?></title>
<!-- Bootstrap -->
<!-- Load Bootstrap CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Load MCOS CSS -->
<link href="css/mcos.css" rel="stylesheet">
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="navbar navbar-default navbar-static-top" role="navigation">
<div class="container">
<div class="navbar-header">
<!-- Buttons for mobile applications -->
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">MCOS 2</a>
<!-- Set site short name-->
<a class="navbar-brand" href="index.php"><?php echo htmlspecialchars($site->name);?></a>
</div>
<!-- Navbar -->
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#contact">Contact</a></li>
<li class="active"><a href="index.php"><span class="glyphicon glyphicon-home"></span> Home</a></li>
<li><a href="about.php"><span class="glyphicon glyphicon-info-sign"></span> About</a></li>
<li><a href="contact.php"><span class="glyphicon glyphicon-envelope"></span> Contact</a></li>
</ul>
</div>
</div>
</div>
<div class="container mcos">
<h1>Welcome</h1>
<!-- Main content -->
<div class="container mcos" role="main">
<h1>Welcome to the <?php echo htmlspecialchars($site->title);?></h1>
<form class="form-signin" action="login.php" method="post" accept-charset="utf8" role="form">
<h2 class="form-signin-heading">Please sign in</h2>
<input type="email" name="email" class="form-control" placeholder="Email address" required autofocus>
<input type="password" name="password" class="form-control" placeholder="Password" required>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
</div>
<pre>
<?php
echo "POST:\n";
echo htmlspecialchars(print_r($_POST, true));
echo"\n";
echo "SESSION:\n";
echo htmlspecialchars(print_r($_SESSION, true));
echo"\n";
echo "COOKIE:\n";
echo htmlspecialchars(print_r($_COOKIE, true));
echo "SERVER:\n";
echo htmlspecialchars(print_r($_SERVER, true));
?>
</pre>
<!-- Load Bootstrap JavaScript (requires jquery) -->
<script src="js/jquery.1.11.0.min.js"></script>
<script src="js/bootstrap.min.js"></script>
......
<?php
// =============================================================================================
// Load session management
// =============================================================================================
include_once ('session.php');
// =============================================================================================
// Define the MCOS2 class
// =============================================================================================
class mcos2 {
public $name = "";
public $title = "";
// Check if user exists
function check_user($email = '', $password = '') {
return true;
}
// Autoconstuct class
function __construct() {
return true;
}
}
?>
\ No newline at end of file
<?php
// =============================================================================================
// Check if there's a cookie with an active session_id and start the session
// =============================================================================================
if (isset($_COOKIE['PHPSESSID']) && !empty($_COOKIE['PHPSESSID'])) {
// set session_id
$_SESSION['PHPSESSID'] = $_COOKIE['PHPSESSID'];
session_id($_COOKIE['PHPSESSID']);
}
else {
// get a new session_id
$session_id = session_id();
// start a new session with this session_id
session_start($session_id);
$_SESSION['PHPSESSID'] = $session_id;
}
// =============================================================================================
// Destroy a session
// =============================================================================================
function destroy_session() {
// empty the $_SESSION variable
$_SESSION = array();
$_COOKIE = array();
// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(
session_name(),
'',
time() - 42000,
$params["path"],
$params["domain"],
$params["secure"],
$params["httponly"]
);
}
// Destroy all cookies
$cookiesSet = array_keys($_COOKIE);
for ($x=0; $x < count($cookiesSet); $x++) {
setcookie($cookiesSet[$x],"",time()-1);
}
// Destroy the session
session_destroy();
}
?>
<?php
include ('config/config.inc.php');
$login_failed = true;
if (isset($_POST['email']) && !empty($_POST['email'])) {
if (isset($_POST['password']) && !empty($_POST['password'])) {
$email = $_POST['email'];
$password = $_POST['password'];
if ($site->check_user($email, $password)) {
$_SESSION['mcos2'] = session_id();
$_SESSION['email'] = $email;
setcookie('mcos2', session_id(), time() + strtotime("+1 hour"));
$login_failed = false;
}
}
}
if ($login_failed) {
header('Location: index.php');
}
else {
header('Location: welcome.php');
}
?>
\ No newline at end of file
<?php
include ('libs/session.php');
if (function_exists('destroy_session')) {
destroy_session();
}
header('Location: index.php');
?>
\ No newline at end of file
<?php
include ('config/config.inc.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?php echo htmlspecialchars($site->title);?></title>
<!-- Load Bootstrap CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Load MCOS CSS -->
<link href="css/mcos.css" rel="stylesheet">
</head>
<body>
<div class="navbar navbar-default navbar-static-top" role="navigation">
<div class="container">
<div class="navbar-header">
<!-- Buttons for mobile applications -->
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<!-- Set site short name-->
<a class="navbar-brand" href="index.php"><?php echo htmlspecialchars($site->name);?></a>
</div>
<!-- Navbar -->
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="index.php"><span class="glyphicon glyphicon-home"></span> Home</a></li>
<li><a href="about.php"><span class="glyphicon glyphicon-info-sign"></span> About</a></li>
<li><a href="contact.php"><span class="glyphicon glyphicon-envelope"></span> Contact</a></li>
<?php
if (isset($_COOKIE['PHPSESSID']) && !empty($_COOKIE['PHPSESSID'])) {
echo ' <li><a href="logout.php"><span class="glyphicon glyphicon-off"></span> Logout</a></li>'."\n";
}
?>
</ul>
</div>
</div>
</div>
<!-- Main content -->
<div class="container mcos" role="main">
<h1>Welcome to the <?php echo htmlspecialchars($site->title);?></h1>
<?php
// Set session
$_SESSION['mcos2'] = session_id();
$_COOKIE['mcos2'] = session_id();
if ($debug) {
echo ' <pre>'."\n";
echo "POST:\n";
echo htmlspecialchars(print_r($_POST, true));
echo"\n";
echo "SESSION:\n";
echo htmlspecialchars(print_r($_SESSION, true));
echo"\n";
echo "COOKIE:\n";
echo htmlspecialchars(print_r($_COOKIE, true));
echo "SERVER:\n";
echo htmlspecialchars(print_r($_SERVER, true));
echo ' </pre>'."\n";
}
?>
</div>
<!-- Load Bootstrap JavaScript (requires jquery) -->
<script src="js/jquery.1.11.0.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment