TWadmin Posted December 31, 2006 Share Posted December 31, 2006 I'm having one final problem with one of my sites after migrating from a PHP4 server to one with v5.1.6 and I'm guessing it's related to the use of global vars. The proper $_ references below are from my updates after figuring out that the newr PHP doesn't default to globals.I'm setting cookies with no problem, but I can't delete them.I have simple test on each page to decide whether to show the login or logout box:[code]if($_COOKIE["cookie_id"]){ include ("/var/www/html/arc/members/logoutbox.php");}else{ include ("/var/www/html/arc/members/loginbox.php");}[/code]The login (cookie setting) process...loginbox.php (non-essentials stripped)[code]echo "<form action=\"/members/login.php\" method=\"post\" enctype=\"multipart/form-data\">";echo "<INPUT TYPE=\"TEXT\" NAME=\"login\">";echo "<INPUT TYPE=\"PASSWORD\" NAME=\"password\">";echo "<INPUT TYPE=\"image\" src=\"/images/login_bot.gif\">";[/code]login.php (non-essentials stripped)[code]$login = $_POST['login'];$password = $_POST['password'];$expire= time()+60*60*24*30;setcookie("cookie_login","$arr[3]","$expire","/");setcookie("cookie_id","$arr[0]","$expire","/");setcookie("cookie_username","$arr[1]","$expire","/");[/code]As I said, the login and cookie setting work fine. This is provided for reference.Now, the broken logout process...logoutbox.php (non-essentials stripped)[code]echo "<form action=\"/members/logout.php\" method=\"post\" enctype=\"multipart/form-data\">";echo $_COOKIE["cookie_username"]; //this shows the logged-in user's name in the boxecho "<INPUT TYPE=\"image\" src=\"/images/logout_bot.gif\">";[/code]logout.php (non-essentials stripped)[code]$expire = time()-60*60*24*30*12*10;// Next 3, original PHP4 removal method --- broken on PHP5//setcookie("cookie_login","$arr[3]","$expire","/");//setcookie("cookie_id","$arr[0]","$expire","/");//setcookie("cookie_username","$arr[1]","$expire","/");// Next 3 don't work either//setcookie("cookie_login","","$expire","/");//setcookie("cookie_id","","$expire","/");//setcookie("cookie_username","","$expire","/");// Next 3 don't work eithersetcookie($_COOKIE['cookie_login'],"","$expire","/");setcookie($_COOKIE['cookie_id'],"","$expire","/");setcookie($_COOKIE['cookie_username'],"","$expire","/"); [/code]The logout script above redirects to loggedout.php which, for debugging purposes now, tests for the existence of the cookie and reports whether it is still present and displays contents. In my tests, it shows that it is and shows the correct set values. I also confirm this by looking at the cookies listed in the browser settings.What am I missing here? I think I've followed all the recommendation I can find and I've checked the PHP manual for setcookie(), but I'm still failing to delete. Any ideas? Quote Link to comment Share on other sites More sharing options...
marcus Posted December 31, 2006 Share Posted December 31, 2006 Just do unset($_COOKIE[name]); Quote Link to comment Share on other sites More sharing options...
TWadmin Posted January 1, 2007 Author Share Posted January 1, 2007 [quote author=mgallforever link=topic=120442.msg493999#msg493999 date=1167528414]Just do unset($_COOKIE[name]);[/quote]I tried that as follows, but it doesn't work.[code] unset($_COOKIE['cookie_login']); unset($_COOKIE['cookie_id']); unset($_COOKIE['cookie_username']);[/code]I'm probably screwing up some easy here. What is it? Quote Link to comment Share on other sites More sharing options...
lszanto Posted January 1, 2007 Share Posted January 1, 2007 Shouldn't you just be able to - the time as in:[code]setcookie("cookiename", "cookievalue", time()-3600)[/code] Quote Link to comment Share on other sites More sharing options...
TWadmin Posted January 1, 2007 Author Share Posted January 1, 2007 [quote author=lszanto link=topic=120442.msg494484#msg494484 date=1167621505]Shouldn't you just be able to - the time as in:[code]setcookie("cookiename", "cookievalue", time()-3600)[/code][/quote]Maybe I'm mis-using the setcookie() statement. I've tried this and some variations, but it's still not working. I checked the error log for the website and I'm getting PHP warnings of "cannot modify header information" for all three lines.Does that suggest the source of my error? Quote Link to comment Share on other sites More sharing options...
lszanto Posted January 1, 2007 Share Posted January 1, 2007 That means you are setting the cookies below the <html> because a cookie is a header you must put it before html code otherwise it can't be sent to the server. Also the example I showed you is setting a cookie for -1 hour which just un-does the cookie. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted January 1, 2007 Share Posted January 1, 2007 The complete error message in the log should indicate a line number where "output" was started that is preventing the header/cookie from being sent. Quote Link to comment Share on other sites More sharing options...
TWadmin Posted January 1, 2007 Author Share Posted January 1, 2007 [quote author=lszanto link=topic=120442.msg494536#msg494536 date=1167628112]That means you are setting the cookies below the <html> because a cookie is a header you must put it before html code otherwise it can't be sent to the server. Also the example I showed you is setting a cookie for -1 hour which just un-does the cookie.[/quote]Bah! Yes, that's exactly what it was! I had read elsewhere that header info can't be modified after the HTML tag, but I hadn't realized cookies were header info. So many things to learn.This required a slight change to the way I was handling the logout process, but it's worth it to have it working.Now, this was working before the server upgrade, as was a header redirect within the php code, so is this something else that is different between current php and pre-4.2 php? It may be considered inconsequential at this point, but I'd like to figure out why it worked on the old server and not on the new.As a result of digging for the solution to this problem, I've found several more areas of the original code for this site that are spitting out errors after the upgrade. Looks like I'll have ample opportunity to get my hands a bit more dirty. Quote Link to comment Share on other sites More sharing options...
TWadmin Posted January 1, 2007 Author Share Posted January 1, 2007 [quote author=PFMaBiSmAd link=topic=120442.msg494538#msg494538 date=1167628336]The complete error message in the log should indicate a line number where "output" was started that is preventing the header/cookie from being sent.[/quote]Yes, it showed all three lines where I was trying to modify the cookies.Your sig looks like something I'll be able to use while reworking some of this site's code:[quote]The most suggested line of PHP code that helps pinpoint the reason why code is not working - error_reporting(E_ALL);[/quote]I'm sure I'll be adding that into several pages. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted January 1, 2007 Share Posted January 1, 2007 Your previous PHP installation probably had [b]output_buffering[/b] turned on/set to some integer value, while your new installation probably has this setting turned off. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.