Skip to content

Instantly share code, notes, and snippets.

@arount
Created February 1, 2018 11:14
Show Gist options
  • Save arount/2354383006c4a56451b857f1e5251710 to your computer and use it in GitHub Desktop.
Save arount/2354383006c4a56451b857f1e5251710 to your computer and use it in GitHub Desktop.
DWM9: Authentification
<?php
/*
Function that handle identification form,
Check if form is sent, then check if passed credentials are correct.
Returns two values:
1 form_sent, true if form was sent, else false
2 connected, true if credentials are good, else false
*/
function handle_authentification_form($users) {
$form_sent = '...' // to edit
$connected = '...' // to edit
// code here
return array($form_sent, $connected);
}
/*
function that authentificate user.
$username username sent by user from form
$password password sent by user from form
$users_db users database to check
*/
function authentification($username, $password, $users_db) {
$connected = false;
// Check if username exists in db first, then check if passwords match
if(isset($users_db[$username]) && $users_db[$username] == $password) {
$connected = true;
}
return $connected;
}
?>
<?php
include_once('_users.php');
include_once('_libraries.php');
$auth_values = handle_authentification_form($users);
?>
<pre style="padding:10px; background: #eee"><?php var_dump($_POST); ?></pre>
<?php if(!$connected && $form_sent): ?>
<p><b style="color:red">Wrong password or username</b></p>
<?php endif; ?>
<?php if($connected): ?>
<div><b>You are connected</b></div>
<?php endif; ?>
<form method="POST">
<input type="text" name="username" required/>
<input type="password" name="pass" required/>
<input type="submit" value="Validate"/>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment