Solved

Trouble deleting cookie

Posted on 2015-01-01
3
66 Views
Last Modified: 2015-01-05
I'm having trouble deleting a cookie using PHP.  The URL is https://buildmomentum.org/register/staff/index.php. When I run the login page, the cookie is still showing up.  Use the login information below to set the cookie.  Then click your back button and login again.  If you see what I see, you'll still the cookie in the variable dump.

Login
email address: steve@somedomain.com  
password: pass

This is the first page used to login the user and delete the cookie if it exists.  (Code at the beginning is used just for troubleshooting.)
.
  <?php
date_default_timezone_set('America/New_York');
//remove staff cookie to start over with a registration
echo "<pre>";
		var_dump($_COOKIE);
echo "</pre>";
	
if (isset($_COOKIE["mo_id_invite"])){
	//remove cookie
	echo "this is running";
	setcookie ("mo_id_invite", "", time() - 3600);
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Momentum Staff Registration</title>
</head>

<html>
<body>

<table width="600" align="center">
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>Enter your email address and temporary password emailed with your staff invitation. Once you register, you will receive a new password and instructions to log into your account.</td>
  </tr>
  <tr>
    <td><form action="login_staff_code.php" method="post" name="form1" id="form1">
      <table width="377" align="center">
        <tr>
          <td width="253">Email</td>
          <td width="335"><label for="email_address"></label>
            <input type="text" name="email_address" id="email_address" /></td>
        </tr>
        <tr>
          <td>Password</td>
          <td><input type="password" name="password" id="password" /></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td><input type="submit" name="submit" id="submit" value="Log In" /></td>
        </tr>
      </table>
    </form></td>
  </tr>
</table>
</body>
</html>

Open in new window


I'm using this code (login_staff_code.php) to check the username and password and set the cookie.  (Code at the beginning is used just for troubleshooting.)

<?php
date_default_timezone_set('America/New_York');
echo "<pre>";
		var_dump($_COOKIE);
echo "</pre>";

if (isset($_COOKIE["mo_registrant"])){
	echo "mo_registrant "  . $_COOKIE["mo_registrant"];
}

if (isset($_COOKIE["mo_id_invite"])){
	echo "mo_id_invite "  . $_COOKIE["mo_id_invite"];
}

?>
  
  <?php //used to register staff 

if (isset($_POST['submit'])) {
	$var_email_address=$_POST['email_address'];
	$var_password=$_POST['password'];
	//Check staff_invites table for login information

	try {
		$qry_staff = new PDO("connection info);
    	foreach($qry_staff->query("SELECT field FROM table WHERE email_address='$var_email_address' AND password='$var_password'") as $row_staff) {
			$var_id_invite=$row_staff['field'];
			//Set cookie with staff_invite ID
			setcookie("mo_id_invite", $var_id_invite, time() + (86400 * 30), "/"); // 86400 = 1 day
			exit;
			//redirect_to("../nextpage.php?id_invite=$var_id_invite");
		}
    	$qry_staff = null;
	} catch (PDOException $e) {
		//echo $e;
	    
    	die();
	}
	echo "Username/Password combination does not match our records.";
	exit;
}

Open in new window

0
Comment
Question by:stkoontz
3 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 40526924
I had a similar problem last month.  I solved it by making the code almost exactly the same for to both set and delete the cookie.  Try this to delete the cookie.
setcookie ("mo_id_invite", "", time() - 3600, "/");

Open in new window

I added the domain to the cookie also for both setting and deleting.  If the path or domain are not the same, then there will be two cookies, not one.  What I ended up doing is like this.
// set cookie
setcookie("mo_id_invite", $var_id_invite, time() + (86400 * 30), "/", ".buildmomentum.org");
// delete cookie
setcookie("mo_id_invite", "", time() - 3600), "/", ".buildmomentum.org");

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40526928
There is a possibility that your browser is cacheing the responses.  The back button is not the right tool to use when you're looking for a new request from the browser, and a new response from the server.  You might want to try it like this:

1. Do the login
2. Refresh the page after the login.  The new request should send the cookie and you'll still be logged in,
3. Do the logout (or whatever process you use to remove the cookie)
4. Refresh the page.  At this point the browser should not send the cookie again.

A general design pattern for PHP client authentication is shown in this article.  It uses the session for short-term authentication and a cookie for the "remember me" functionality.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_2391-PHP-login-logout-and-easy-access-control.html

Some more information on how PHP handles sessions is given in this article.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11909-PHP-Sessions-Simpler-Than-You-May-Think.html

HTH and Happy New Year, ~Ray
0
 
LVL 2

Author Comment

by:stkoontz
ID: 40527903
Happy New Year.  Thanks to both for responding on a holiday!

Dave: Being consistent and adding the domain name on the code to set/remove the cookies has helped.

Ray: The user might use the back button to start over on their registration.  Is location.reload(forceGet) the best command to use to reload the page?

Thanks again,

Steve
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question