Created
October 11, 2015 10:07
-
-
Save jerome-labidurie/5323fccdfbf3f4a37c33 to your computer and use it in GitHub Desktop.
simple one file php session for login example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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