?
Solved

Destroy Cookie??

Posted on 2012-08-10
27
Medium Priority
?
468 Views
Last Modified: 2012-08-10
Can someone please tell me why this will not destroy  cookies in internet explorer 7.  It seems to work fine in all other browsers.

<?php 

/*---------------------------------------------------------------------------------*/
if (isset($_COOKIE['countyANSI'])) {
$date_of_expiry = time()- 60; //destroys cookies by setting expiration date 60 seconds in the past
	   
      setcookie( "countyANSI", "none", $date_of_expiry, "/" ) ;
	  setcookie( "stateANSI", "none", $date_of_expiry, "/" ) ;
}
/*---------------------------------------------------------------------------------*/


$go_to = "index.php";     
header("Location: " . $go_to );
?>

Open in new window

0
Comment
Question by:Luey
  • 13
  • 12
  • 2
27 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 38281393
Set your domain instead of /
0
 

Author Comment

by:Luey
ID: 38281464
Hey Gary
Tried that just now and it still does not work in ie7.  Works every where else.
I have another program written just like this one for another cookie and it works fine.
I can't even change the value of these two cookies I have tried.  That is also a problem for me.  Any suggestions.

<?php 

/*---------------------------------------------------------------------------------*/
if (isset($_COOKIE['countyANSI'])) {
$date_of_expiry = time()- 60; //destroys cookies by setting expiration date 60 seconds in the past
	   
      setcookie( "countyANSI", "none", $date_of_expiry, "http://www.webcahoot.com" ) ;
	  setcookie( "stateANSI", "none", $date_of_expiry, "http://www.webcahoot.com" ) ;
}
/*---------------------------------------------------------------------------------*/


$go_to = "index.php";     
header("Location: " . $go_to );
?>

Open in new window

0
 
LVL 58

Expert Comment

by:Gary
ID: 38281498
What format are you using to set them? Make sure it is exactly the same.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:Luey
ID: 38281535
This is the way they were originally set.  All this has worked several days ago or at least I was pretty sure it did.  Now I cannot even change the cookie with this code.

setcookie( "countyANSI", "", time()-3600, "http://www.webcahoot.com") ;
setcookie( "stateANSI", "", time()-3600, "http://www.webcahoot.com") ;


/*---------------------------------------------------------------------------------*/
//sets cookie for county and state on change location
if (isset($_GET['county'])) {
	   $reload_page = $_GET['return_page'];
       //if they click on a location set the cookie for the location and reload the parent page
	   $county = $_GET['county'];
	   $state = $_GET['state'];
	   $number_of_days = 999 ; //variable for days to set cookie to expire
       $date_of_expiry = time() + 60 * 60 * 24 * $number_of_days ; //does math to put the number of days into seconds
	   
       setcookie( "countyANSI", "$county", $date_of_expiry, "http://www.webcahoot.com") ;
	   setcookie( "stateANSI", "$state", $date_of_expiry, "http://www.webcahoot.com") ;
       
	   header("Location: " . $reload_page );
}

Open in new window

0
 
LVL 84

Accepted Solution

by:
Dave Baldwin earned 2000 total points
ID: 38281579
I don't believe you were setting them correctly to begin with.  The 'setcookie' page http://us2.php.net/manual/en/function.setcookie.php shows several examples.  First, "time()-3600" should make it expire immediately since that is in the past.  Second, you left out the 'path' and third, you used a complete URL instead of the domain

setcookie( "countyANSI", "", time()+3600, "/", "webcahoot.com") ;
setcookie( "stateANSI", "", time()+3600, "/", "webcahoot.com") ;

Open in new window

To delete the cookie, you use exactly the same code except the expiration is set to some time in the past.
setcookie( "countyANSI", "", time()-3600, "/", "webcahoot.com") ;
setcookie( "stateANSI", "", time()-3600, "/", "webcahoot.com") ;

Open in new window

0
 

Author Closing Comment

by:Luey
ID: 38281611
got it to work then thanks.
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 38281620
You're welcome.
0
 

Author Comment

by:Luey
ID: 38281678
Help Again!

This works in ie7  but does not work in firefox
setcookie( "countyANSI", "", time()-3600, "/", "webcahoot.com") ;

Open in new window

0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 38281792
How are you testing it?
0
 

Author Comment

by:Luey
ID: 38282398
The site is actually live.  The problem is I know it was working before but now it does not.  I don't know any other way to explain it.  The destroy I was trying to do was just trying to see if I removed the cookie could I make it work again.  The site is WebCahoot.com and at the top of the page you can set a location.  Like I said it all works except IE7.  Any help would be greatly appreciated.
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 38282425
But you're not telling me how you are checking it so I can test it too!!
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 38282440
In Firefox 14, I'm getting the PHPSESSIONID cookie and the Google cookies but nothing else.
0
 

Author Comment

by:Luey
ID: 38282499
Ok I put all the code back to the way it was.  I know it is wrong. When you visit the site at the top of the page you will see "Click Here To Change Location".  That make an ajax call.  Then put in a zip code or city.  It will search for your location.  When you click the link for your location it calls a program that sets the cookie and reloads the page.  This should change the field to say your location.  I am also echoing one of the cookies in the footer.  The way it is now works everywhere except ie7.  I have tried the code the way you suggested but then cant get it to work in firefox but does work in ie.  What next.  Thanks so  much for you time.
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 38282543
The code that I showed you above works perfectly on my own site in Firefox14 and IE7.  The only change I made in the format was to add a '.' in front of the domain name because the Google cookies are doing that.  Try putting this at the top of your 'index.php' page and I think you'll see that it works.  It may be the AJAX process that is screwing it up somehow.  I don't see anything in the footer of your page.

setcookie( "countyANSI", "County", time()+3600, "/", ".webcahoot.com") ;
setcookie( "stateANSI", "State", time()+3600, "/", ".webcahoot.com") ;

Open in new window

0
 

Author Comment

by:Luey
ID: 38282583
One question am I having a problem because I already have cookies set with the same name but wrong format.  Just guessing.  Because everytime I put anything to the right of "/" it stops working in firefox but will work in ie7.  If so do I need to correct the code and change all the instances of that cookie name in my other programs.  The site has not been up long so there would not be to many people with the wrong cookies.  If that is the problem because I have followed your instruction to the t and it still does not work.  I even found that the php manual say you may need a . for older browsers in front of the domain to match all subdomains.
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 38282596
You can change the cookie name because right now it isn't working.  Show me what you mean by anything to the right of "/".
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 38282614
Here are two simple pages that set and delete the cookies.  They link to each other so you can just go back and forth and see that it works.  'cookie11.php' sets the cookies and displays the cookies sent with the request.  'cookie22.php' deletes the cookies and displays the cookies sent with the request.  The differences between them are very small.

cookie11.php
<?php 
// cookies test
//setcookie( "countyANSI", "Sacramento", time()+3600, "/", ".webcahoot.com") ;
//setcookie( "stateANSI", "California", time()+3600, "/", ".webcahoot.com") ;
// use default domain
setcookie( "countyANSI", "Sacramento", time()+3600, "/") ;
setcookie( "stateANSI", "California", time()+3600, "/") ;
                                            
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>cOOKIE tEST Pages</title>
</head>
<body>
<div id="wrap">
<h1>cOOKIE tEST Pages</h1>
<div style="font-size: 10pt; font-family: Arial; font-weight: bold; text-align: center; color: #330066;">
<script type="text/javascript">
<!--
/*Current date script credit: 
JavaScript Kit (www.javascriptkit.com)
Over 200+ free scripts here!
*/

var mydate=new Date()
var year=mydate.getYear()
if (year < 1000)
year+=1900
var day=mydate.getDay()
var month=mydate.getMonth()
var daym=mydate.getDate()
if (daym<10)
daym="0"+daym
var dayarray=new Array("Surfin' Sunday","Moody Monday","Totally Tuesday","Wild Wednesday","Thrilling Thursday","Finally Friday","Shopping Saturday")
var montharray=new Array("Jan","Feb","Mar","April","May","June","July","Aug","Sept","Oct","Nov","Dec")
document.write(dayarray[day]+" "+montharray[month]+" "+daym+", "+year)
// -->
</script>
</div>
<!-- ****** Comments and Current Events ****** -->
<div align="center">
<?php var_dump($_COOKIE); ?>
<br />
<a href="cookie22.php">cookie22.php</a>
</div>
<hr class="hr2">

</body>
</html>

Open in new window


cookie22.php
<?php 
// cookies test
//setcookie( "countyANSI", "Sacramento", time()-3600, "/", ".webcahoot.com") ;
//setcookie( "stateANSI", "California", time()-3600, "/", ".webcahoot.com") ;
// use default domain
setcookie( "countyANSI", "Sacramento", time()-3600, "/") ;
setcookie( "stateANSI", "California", time()-3600, "/") ;
                                            
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>cOOKIE tEST Pages</title>
</head>
<body>
<div id="wrap">
<h1>cOOKIE tEST Pages</h1>
<div style="font-size: 10pt; font-family: Arial; font-weight: bold; text-align: center; color: #330066;">
<script type="text/javascript">
<!--
/*Current date script credit: 
JavaScript Kit (www.javascriptkit.com)
Over 200+ free scripts here!
*/

var mydate=new Date()
var year=mydate.getYear()
if (year < 1000)
year+=1900
var day=mydate.getDay()
var month=mydate.getMonth()
var daym=mydate.getDate()
if (daym<10)
daym="0"+daym
var dayarray=new Array("Surfin' Sunday","Moody Monday","Totally Tuesday","Wild Wednesday","Thrilling Thursday","Finally Friday","Shopping Saturday")
var montharray=new Array("Jan","Feb","Mar","April","May","June","July","Aug","Sept","Oct","Nov","Dec")
document.write(dayarray[day]+" "+montharray[month]+" "+daym+", "+year)
// -->
</script>
</div>
<!-- ****** Comments and Current Events ****** -->
<div align="center">
<?php var_dump($_COOKIE); ?>
<br />
<a href="cookie11.php">cookie11.php</a>
</div>
<hr class="hr2">

</body>
</html>

Open in new window

0
 

Author Comment

by:Luey
ID: 38282618
"/", ".webcahoot.com"
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 38282624
That alone will not cause Firefox to fail.  Something else is going on.  The Google cookies for your site that use ".webcahoot.com" are working fine in Firefox.
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 38282633
Firefox says you have this error:

Timestamp: 8/10/2012 4:00:30 PM
Error: TypeError: $("#article_images").validate is not a function
Source File: http://www.webcahoot.com/javascript/webcahoot_javascript.js
Line: 126

Open in new window

0
 

Author Comment

by:Luey
ID: 38282636
Ok I test your programs and see that they work fine.  I also have some other cookies such as login etc that work fine.  I will look at that error now.
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 38282657
Take the two programs and uncomment the first two cookie lines that use ".webcahoot.com" and comment out the next two lines that use the default domain and see what happens.  Make the changes on both programs.
0
 

Author Comment

by:Luey
ID: 38282681
I can make yours work both ways in ie but it does not work in ff like this.

setcookie( "stateANSI", "California", time()-3600, "/", ".webcahoot.com") ;

Like i said earlier if i use .webcahoot.com my code even works in ie but not ff.
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 38282714
I know what you said but I also know that it works perfectly that way on my website using my domain name.  And it works the same in Firefox14, IE8, Chrome, Safari, and Opera on three different computers, IE7 on a third computer, Firefox 3.6 and Chrome on my Linux computer, and Firefox 2 on my old Mac.  So I believe that if it is not working in your copy of Firefox14, then maybe there is something wrong with your copy.  When you look at cookies for "webcahoot.com" in your copy of Firefox, do you see the Google cookies that use ".webcahoot.com"?
0
 

Author Comment

by:Luey
ID: 38282726
Hold the presses.  I think I have found the problem.  All of your code works fine.  However mine does not.  I have another test to run but I think it is because I an not treating my variable properly.  I changed your sacremento  and california to some variables that are in the data.   They are numbers like 045 and it failed to work.   I know that is not an integer so how am supposed to be handling my variables.

From the ajax  call I simply pass them in a url variable and get and asign them like below. What I am doing wrong with them.  I hate I have wasted your whole afternoon.

$county = $_GET['county'];
$state = $_GET['state'];
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 38282758
Everything that goes into a cookie must be text except for the expire date.  If you're trying to put an integer (binary value) into a cookie, that won't work because all cookie content is text.  On the other hand, there is no obvious reason that your assignments below won't work.

$county = $_GET['county'];
$state = $_GET['state']; 

Open in new window

0
 

Author Comment

by:Luey
ID: 38282871
I know this sounds crazy but the only way I could make it work was to destroy the cookies in both formats before trying to change one.  


setcookie( "countyANSI", "", time()-3600, "/", ".webcahoot.com") ;
setcookie( "stateANSI", "", time()-3600, "/", ".webcahoot.com") ;
setcookie( "countyANSI", "", time()-3600, "/") ;
setcookie( "stateANSI", "", time()-3600, "/") ;

Open in new window


Then I set them like this.  It all seems to work now.  I will have to test more.

setcookie( "countyANSI", "$county", $date_of_expiry, "/") ;
setcookie( "stateANSI", "$state", $date_of_expiry, "/") ;

Open in new window

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

807 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