Cookies and sessions

Cookies

Cookies are used in sessions so it is important to understand how they work. However, you hardly ever need to work with cookies directly: sessions libraries (demonstrated below) take care of managing cookies.



<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- 
Cookies in PHP
Author: Elena Machkasova 
Last modified: 4/17/08
--> 
<?php
// we must deal with cookies BEFORE the starting html tag

// get the cookie from the request, if there is a cookie
$count = $_COOKIE['count'];
// check if there was a cookie
if (!isset($count)) {
  $count = 0;
 } else {
// increment the counter
  $count++;
 }
// send back the cookie with the new count
// the cookie will expire in 5 minutes
setcookie('count', $count, time() + 300);
?>
<html  xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>
Counting visits to a page
</title>

</head>
<body>
<p>
<?php
if ($count == 0) print "Welcome, new visitor!\n";
else print "Welcome back! You visited this page $count times. \n";
?>
</p>
</body>
</html>

http://csci1101sp10.morris.umn.edu/~elenam/1101_spring10/sessions/cookies.php

Creating and destroying sessions

Log in (creates a session if there isn't one already)


<?php
// -------------- define functions -----------------
function isvalid($user, $password) {
	// check if the user's password is valid
	// at this point all non-empty passwords are valid
	if ($password != "") return true;
	return false;	
}

function print_login_form() {
	$form_string = "
	<form method=\"post\" 
	action=\"login.php\">
	<table border=\"0\">
	<tr>

	<td>Enter your user name:</td>
	<td>
	<input type = \"text\" name = \"user\" />
	</td>
	</tr>

	<tr>
	<td>Enter your password:</td>
	<td>
	<input type = \"password\" name = \"password\" />
	</td>

	</tr>
	<tr>
	<td>
	<input type=\"submit\" name = \"submit\" value=\"submit\" />
	</td>
	</tr>

	</table>
	</form>
	";
	print $form_string;
}
// ---------------- end of functions -----------------

// since sessions are handled with cookies, we must start
// a session before any HTML tags
session_start();
$display_form = true;
if (!isset($_SESSION['user'])) {
	// check if the user is responding to login form
	$user = $_POST['user'];
	$password = $_POST['password'];	
	if (isset($user)) {
		if (isvalid($user, $password)) {
			// the user logged in - no need to display form
			$_SESSION['user'] = $user;
			$display_form = false; 
		}
	}
} else {
	// returning user - no need to display the form
	$display_form = false;	
}
?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- 
Handling login via sessions in PHP
Author: Elena Machkasova 
Last modified: 4/17/08
--> 
<html  xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>
Handling user login
</title>
</head>
<body>
<?php
if ($display_form) {
	print_login_form();	
} else {
  	print "<p>";
	print "Welcome, ".$_SESSION['user']."!<br/>\n";	
	print "<a href=\"logout.php\">Logout</a><br/>\n";
	print "</p>";
}
?>

</body>
</html>

http://csci1101sp10.morris.umn.edu/~elenam/1101_spring10/sessions/login.php

Log out (destroys the session)


<?php 
session_start();
$user = $_SESSION['user'];
session_destroy(); 
?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<!-- 
Handling login via sessions in PHP
Author: Elena Machkasova 
Last modified: 4/17/08 
--> 

<html  xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>
Handling user logout.
</title>
</head>
<body>

<p>

<?php
print "Bye, $user<br/>\n";
print "To log in again, click <a href=\"login.php\">here</a><br/>\n";
?>
</p>
</body>
</html>


UMM CSci 1101