Skip to content

Instantly share code, notes, and snippets.

@recck
Created October 7, 2012 16:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save recck/3848801 to your computer and use it in GitHub Desktop.
Save recck/3848801 to your computer and use it in GitHub Desktop.
Programming in PHP - Week 6 - Day 11 - Sessions and Cookies
<?php
if(isset($_POST['submit'])){
if(!empty($_POST['name'])){
setcookie('yourName', htmlentities($_POST['name']));
header("Location: cookies.php");
}else {
echo 'Please enter a name!<br />';
}
}
if(!empty($_COOKIE['yourName'])){
echo 'Welcome, ' . $_COOKIE['yourName'];
}else {
echo 'Enter your name:';
}
?><br />
<form method="post">
Name: <input type="text" name="name" />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
session_start();
if(isset($_POST['submit'])){
if(isset($_POST['end'])){
unset($_SESSION['yourName']);
header("Location: sessions.php");
}
if(!empty($_POST['name'])){
$_SESSION['yourName'] = htmlentities($_POST['name']);
}else {
echo 'Please enter a name!<br />';
}
}
if(!empty($_SESSION['yourName'])){
echo 'Welcome, ' . $_SESSION['yourName'];
}else {
echo 'Enter your name:';
}
?><br />
<form method="post">
Name: <input type="text" name="name" /><br />
End session? <input type="checkbox" name="end" value="1" />
<input type="submit" name="submit" value="Submit" />
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment