Skip to content

Instantly share code, notes, and snippets.

@jerome-labidurie
Created October 11, 2015 10:07
Show Gist options
  • Save jerome-labidurie/5323fccdfbf3f4a37c33 to your computer and use it in GitHub Desktop.
Save jerome-labidurie/5323fccdfbf3f4a37c33 to your computer and use it in GitHub Desktop.
simple one file php session for login example
<?php
// valid credentials
$valid_user='user';
$valid_password='pwd';
if (isset($_POST['login']) && isset($_POST['pwd'])) {
// trying to log
if ( ($_POST['login'] == $valid_user) and
($_POST['pwd'] == $valid_password) ) {
// login ok
session_start();
$_SESSION['login'] = $_POST['login'];
$_SESSION['pwd'] = $_POST['pwd'];
header('location:index.php');
}
else {
// login ko
exit();
}
}
session_start();
if (isset($_SESSION['login']) && isset($_SESSION['pwd'])) {
// session is ok
if (isset($_POST['btn']) && $_POST['btn'] == 'unlog') {
// deconnexion
session_unset ();
session_destroy ();
header('location:index.php');
}
echo "<h2> Hello, ". $_SESSION['login'] ." </h2>";
echo '<form action="index.php" method="post">
<button name="btn" type="submit" value="unlog">Deconnexion</button>
</form>';
} else {
// not logged
session_destroy();
echo '<h2>Login</h2>';
echo '<form action="index.php" method="post">
login : <input type="text" name="login">
<br />
password : <input type="password" name="pwd"><br />
<input type="submit" value="Connexion">
</form>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment