carlg Posted October 12, 2007 Share Posted October 12, 2007 This is a unique problem in which I'm struggling on. Earlier I thought the problem was global variables, but it is not the problem. It is the cookies which is causing me the problem. So here's the story. I had an entire site working perfectly on my test server in my basement (php 5.2) I signed up for VPS access. Installed this entire site on my new VPS server (php 4.3.9) I have the exact same code running on both servers, but it works on my test server in the basement, but not on the VPS server. For some reason one of my cookie variables is becoming unset. I use the setcookie function to set the cookie and $_COOKIE['name'] to get the value out. I ripped out all of the cookie processing code on the VPS server and put it in separate source files with no other code from the site and it worked perfectly fine. The cookie value was retained as expected. Where should I look to fins an error like this? Where would you look first? This is driving me crazy and there is really no way I can post the code of my entire site here. I thought about ripping out the cookie related code and posting it here, but when I did, it worked. Please help Thanks Carl Quote Link to comment https://forums.phpfreaks.com/topic/73005-what-could-cause-a-cookie-to-become-unset/ Share on other sites More sharing options...
prime Posted October 12, 2007 Share Posted October 12, 2007 Can you post your code please? Quote Link to comment https://forums.phpfreaks.com/topic/73005-what-could-cause-a-cookie-to-become-unset/#findComment-368138 Share on other sites More sharing options...
carlg Posted October 12, 2007 Author Share Posted October 12, 2007 No, because which part would I post? I couldn't post the entire site, it's 1000's of lines. The part that I would post works independently. Quote Link to comment https://forums.phpfreaks.com/topic/73005-what-could-cause-a-cookie-to-become-unset/#findComment-368144 Share on other sites More sharing options...
prime Posted October 12, 2007 Share Posted October 12, 2007 the cookie setting and retreving Quote Link to comment https://forums.phpfreaks.com/topic/73005-what-could-cause-a-cookie-to-become-unset/#findComment-368148 Share on other sites More sharing options...
carlg Posted October 12, 2007 Author Share Posted October 12, 2007 Ok, but this code works fine when it is isolated. Here is the cookie setting if ($staylogged) { //user chooses to stay logged from this machine setcookie(profile_id, $profile_id, time()+360000000, "/"); } else //user does not want to stay logged from this machine { setcookie(profile_id, $profile_id, 0, "/"); } Here is where I get the value of the cookie if (!empty($_COOKIE['profile_id'])) { //do stuff } else { //do other stuff } Here is where the cookie gets unset, but this code does not get executed until the user logs out. It does not get executed in my scenario. if ($_GET['logout']) { $profile_id = ""; setcookie("profile_id", '',time() - 3600,"/"); $o_id = ''; $o_uname=''; } Quote Link to comment https://forums.phpfreaks.com/topic/73005-what-could-cause-a-cookie-to-become-unset/#findComment-368180 Share on other sites More sharing options...
prime Posted October 12, 2007 Share Posted October 12, 2007 thig is cookies are one of the few things in php that can work different on different browsers and such. So you should be filling out all of the cookie fields to minimize room for error for example you should be doing something like the following $sitedomain = ereg_replace('www.', '', $_SERVER['SERVER_NAME']); setcookie ("profile_id, $profile_id, time()+360000000, "/", "$sitedomain", "0") Quote Link to comment https://forums.phpfreaks.com/topic/73005-what-could-cause-a-cookie-to-become-unset/#findComment-368184 Share on other sites More sharing options...
prime Posted October 12, 2007 Share Posted October 12, 2007 you could just replace th4e site domain variable with the ereg thing that I used, for just your domain. I just like making my scripts portable Quote Link to comment https://forums.phpfreaks.com/topic/73005-what-could-cause-a-cookie-to-become-unset/#findComment-368189 Share on other sites More sharing options...
carlg Posted October 12, 2007 Author Share Posted October 12, 2007 If the $_COOKIE stuff never gets translated into html, how does it ever make it to the browser? Quote Link to comment https://forums.phpfreaks.com/topic/73005-what-could-cause-a-cookie-to-become-unset/#findComment-368190 Share on other sites More sharing options...
prime Posted October 12, 2007 Share Posted October 12, 2007 cookie's get sent through a header style funtion, which is why you need to send cookies before you send any other data to the browser on that page. Quote Link to comment https://forums.phpfreaks.com/topic/73005-what-could-cause-a-cookie-to-become-unset/#findComment-368191 Share on other sites More sharing options...
carlg Posted October 12, 2007 Author Share Posted October 12, 2007 But, I'm using the same browser for all of my testing. Quote Link to comment https://forums.phpfreaks.com/topic/73005-what-could-cause-a-cookie-to-become-unset/#findComment-368193 Share on other sites More sharing options...
prime Posted October 12, 2007 Share Posted October 12, 2007 same deal with different servers, its just a good practice to fill al fields Quote Link to comment https://forums.phpfreaks.com/topic/73005-what-could-cause-a-cookie-to-become-unset/#findComment-368197 Share on other sites More sharing options...
carlg Posted October 12, 2007 Author Share Posted October 12, 2007 OK, I'm trying this now, I'll respond soon Quote Link to comment https://forums.phpfreaks.com/topic/73005-what-could-cause-a-cookie-to-become-unset/#findComment-368205 Share on other sites More sharing options...
carlg Posted October 12, 2007 Author Share Posted October 12, 2007 No, didn't do it But thanks for getting me in line with the cookies. Quote Link to comment https://forums.phpfreaks.com/topic/73005-what-could-cause-a-cookie-to-become-unset/#findComment-368213 Share on other sites More sharing options...
carlg Posted October 13, 2007 Author Share Posted October 13, 2007 GREAT NEWS!!!!! I finally figured out the problem!!!!!! I was outputing the <html> tag before setting the cookie. This seems to be a problem with the older version of php, but not with the newer version of php. When I did my little sample test above, I was not outputing anything before setting the cookie, that's why it was working. Again, thanks for all of your input. Carl Quote Link to comment https://forums.phpfreaks.com/topic/73005-what-could-cause-a-cookie-to-become-unset/#findComment-368277 Share on other sites More sharing options...
prime Posted October 13, 2007 Share Posted October 13, 2007 actualy that can cause a problem with any version. your supposed to output cookies and headers before any code or blank spaces whatsoever, even before the doc type declaration, no type of browser output whatsoever Quote Link to comment https://forums.phpfreaks.com/topic/73005-what-could-cause-a-cookie-to-become-unset/#findComment-368312 Share on other sites More sharing options...
d22552000 Posted October 13, 2007 Share Posted October 13, 2007 I dont know if anyone has noticed yet but in PHP 4 you CANNOT use "$_COOKIE" you have to use "$_HTTP_COOKIE_VARS" Quote Link to comment https://forums.phpfreaks.com/topic/73005-what-could-cause-a-cookie-to-become-unset/#findComment-368371 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.