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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
| <?php
$error = false;
// Lib to enable support for json_encode for php < 5.2.0 - remove if your version is 5.2.0 or upper
require('_errors/libError.php');
// If login form submitted
if (isset($_POST['a']))
{
$valid = false;
$redirect = isset($_REQUEST['redirect']) ? $_REQUEST['redirect'] : 'essai.php';
// Check fields
if (!isset($_POST['login']) or strlen($_POST['login']) == 0){
$error = 'Saisissez votre login SVP';
}
elseif (!isset($_POST['pass']) or strlen($_POST['pass']) == 0){
$error = 'Saisissez votre mot de passe SVP';
}
else
{
/*
* Do whatever here to check user login
*/
$valid = ($_POST['login'] == 'xxx' and $_POST['pass'] == 'xxx');
if (!$valid){
$error = 'Wrong user/password, please try again';
}
}
// Check if AJAX request
$ajax = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) and strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
// If user valid
if ($valid){
// Handle the keep-logged option
if (isset($_POST['keep-logged']) and $_POST['keep-logged'] == 1){
// Set cookie or whatever here
}
if ($ajax) {
header('Cache-Control: no-cache, must-revalidate');
header('Expires: '.date('r', time()+(86400*365)));
header('Content-type: application/json');
echo json_encode(array(
'valid' => true,
'redirect' => $redirect
));
exit();
}
else {
header('Location: '.$redirect);
exit();
}
} else
{
if ($ajax) {
header('Cache-Control: no-cache, must-revalidate');
header('Expires: '.date('r', time()+(86400*365)));
header('Content-type: application/json');
echo json_encode(array(
'valid' => false,
'error' => $error
));
exit();
}
}
}
?><!DOCTYPE html>
<html lang="en">
<head>
<title>connexion</title>
<meta charset="utf-8">
<!-- Combined stylesheets load
<link href="css/mini.php?files=reset,common,form,standard,special-pages" rel="stylesheet" type="text/css"> -->
<link rel="stylesheet" media="all" type="text/css" href="css/reset.css" />
<link rel="stylesheet" media="all" type="text/css" href="css/common.css" />
<link rel="stylesheet" media="all" type="text/css" href="css/form.css" />
<link rel="stylesheet" media="all" type="text/css" href="css/standard.css" />
<link rel="stylesheet" media="all" type="text/css" href="css/special-pages.css" />
<!-- Favicon -->
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico">
<link rel="icon" type="image/png" href="favicon-large.png">
<!-- Combined JS load -->
<!-- html5.js has to be loaded before anything else -->
<script type="text/javascript" src="js/mini.php?files=html5,jquery-1.4.2.min,old-browsers,common,standard,jquery.tip.js"></script>
<!--[if lte IE 8]><script type="text/javascript" src="js/standard.ie.js"></script><![endif]-->
<!-- example login script -->
<script type="text/javascript">
$(document).ready(function()
{
// We'll catch form submission to do it in AJAX, but this works also with JS disabled
$('#login-form').submit(function(event)
{
// Stop full page load
event.preventDefault();
// Check fields
var login = $('#login').val();
var pass = $('#pass').val();
if (!login || login.length == 0){
$('#login-block').removeBlockMessages().blockMessage('Saisissez votre login SVP', {type: 'warning'});
}
else if (!pass || pass.length == 0){
$('#login-block').removeBlockMessages().blockMessage('Saisissez votre mot de passe SVP', {type: 'warning'});
}
else
{
var submitBt = $(this).find('button[type=submit]');
submitBt.disableBt();
// Target url
var target = $(this).attr('action');
if (!target || target == '')
{
// Page url without hash
target = document.location.href.match(/^([^#]+)/)[1];
}
// Request
var data = {
a: $('#a').val(),
login: login,
pass: pass
};
var redirect = $('#redirect');
if (redirect.length > 0){
data.redirect = redirect.val();
}
// Start timer
var sendTimer = new Date().getTime();
// Send
$.ajax({
url: target,
dataType: 'json',
type: 'POST',
data: data,
success: function(data, textStatus, XMLHttpRequest)
{
if (data.valid){
// Small timer to allow the 'cheking login' message to show when server is too fast
var receiveTimer = new Date().getTime();
if (receiveTimer-sendTimer < 500){
setTimeout(function()
{
document.location.href = data.redirect;
}, 500-(receiveTimer-sendTimer));
} else {
document.location.href = data.redirect;
}
} else {
// Message
$('#login-block').removeBlockMessages().blockMessage(data.error || 'An unexpected error occured, please try again', {type: 'error'});
submitBt.enableBt();
}
},
error: function(XMLHttpRequest, textStatus, errorThrown)
{
// Message
$('#login-block').removeBlockMessages().blockMessage('Erreur de connexion serveur', {type: 'error'});
submitBt.enableBt();
}
});
// Message
$('#login-block').removeBlockMessages().blockMessage('SVP patientez, vérification en cours...', {type: 'chargement'});
}
});
});
</script>
</head>
<!-- the 'special-page' class is only an identifier for scripts -->
<body class="special-page login-bg dark">
<!--<section id="message">
<div class="block-border"><div class="block-content no-title dark-bg">
<p class="mini-infos">For demo website, use <b>admin</b> / <b>admin</b></p>
</div></div>
</section>-->
<section id="login-block">
<div class="block-border">
<div class="block-content">
<div class="block-header">Connectez-vous</div>
<?php
if ($error){ ?>
<p class="message error no-margin"><?php echo htmlspecialchars($error); ?></p>
<?php } ?>
<form class="form with-margin" name="login-form" id="login-form" method="post" action="">
<input type="hidden" name="a" id="a" value="send">
<?php
// Check if a redirect page has been forwarded
if (isset($_REQUEST['redirect'])) { ?>
<input type="hidden" name="redirect" id="redirect"
value="<?php echo htmlspecialchars($_REQUEST['redirect']); ?>">
<?php } ?>
<p class="inline-small-label">
<label for="login"><span class="bigmodif">Identifiant</span></label>
<input type="text" name="login" id="login" class="full-width"
value="<?php if (isset($_POST['login'])) { echo htmlspecialchars($_POST['login']); } ?>">
</p>
<p class="inline-small-label">
<label for="pass"><span class="bigmodif">Mot de passe</span></label>
<input type="password" name="pass" id="pass" class="full-width" value="">
</p>
<button type="submit" class="float-right">Connexion</button>
<p class="input-height">
<input type="checkbox" name="keep-logged" id="keep-logged"
value="1" class="mini-switch"<?php if (!isset($_POST['keep-logged']) or $_POST['keep-logged'] == 1) { echo ' checked="checked"'; } ?>>
<label for="keep-logged" class="inline">Restez connecté</label>
</p>
</form>
<form class="form" id="password-recovery" method="post" action="">
<fieldset class="grey-bg no-margin collapse">
<legend><a href="#">Mot de passe perdu ?</a></legend>
<p class="input-with-button">
<label for="recovery-mail">Entrez votre adresse mail</label>
<input type="text" name="recovery-mail" id="recovery-mail" value="">
<button type="button">Send</button>
</p>
</fieldset>
</form>
</div>
</div>
</section>
<!--[if lt IE 8]></div><![endif]-->
<!--[if lt IE 9]></div><![endif]-->
</body>
</html> |
Partager