Created
July 2, 2012 15:18
-
-
Save vaishaks/3033739 to your computer and use it in GitHub Desktop.
Trying out sessions in PHP
This file contains hidden or 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 | |
session_start(); | |
?> | |
<html> | |
<head></head> | |
<body> | |
<?php | |
if (!isset($_SESSION['name']) && !isset($_POST['name'])) { | |
?> | |
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"> | |
<input type="text" name="name" /> | |
<input type="submit" name="submit" /> | |
</form> | |
<?php | |
} | |
elseif (!isset($_SESSION['name']) && isset($_POST['name'])) { | |
if (!empty($_POST['name'])) { | |
$_SESSION['name'] = $_POST['name']; | |
$_SESSION['start'] = time(); | |
echo "Welcome ".$_POST['name'].". A new session has been activated for you. Click <a href=".$_SERVER['PHP_SELF'].">here</a> to refresh the page."; | |
} | |
else { | |
echo "ERROR! Please enter your name!"; | |
session_destroy(); | |
} | |
} | |
elseif (isset($_SESSION['name'])) { | |
echo "Welcome back, ".$_SESSION['name'].". This session was activated ".round((time()-$_SESSION['start'])/60)." minutes ago. Click <a href=".$_SERVER['PHP_SELF'].">here</a> to refresh the page."; | |
echo "<br>Your session ID is = ".session_id(); | |
} | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment