简体   繁体   中英

PHP setcookie only intermittently works (version compatibility?)

I am currently running 2 versions of PHP. My local (test) server has 5.3.5, while my remote (live) server has 5.2.4. This has caused a few compatibility issues in the past, but I've always been able to fix them pretty easily. Soon my remote server will be upgraded, so it won't cause too many more issues, but in the mean time I still have to provide solutions for these stupid compatibility issues. Will someone please take a look at my code below and let me know if they spot any issues for a 5.2.4 PHP build?

function _cookie($name, $value) {
    $expire = strtotime('+1 month');
    $old_expire = strtotime('last day of next month');

    setcookie(
        $name,
        $value,
        $expire
    );
    var_dump($name, $value, $expire, $old_expire);
}

$date = date(
    'Y-m-d H:i:s',
    filemtime($file)
);
_cookie('user', 'bob');//works
_cookie('up2date', $date);//does not

I originally had 'last day of next month' as my cookie expiration date, which is why its still in the var_dump list. However, that string was returning false on the remote server causing cookies to expire at the end of the session. Even though it returned false, it would still set the "user" cookie and I was able to view it in the browser. It would not, however, set the "up2date" cookie. I thought this was odd, but figured it had something to do with the strtotime function returning false. So I tried fixing that first. After consulting the PHP manual I determined that the string I was using was only available on 5.3. There, problem confirmed, easy fix. I replaced it with the '+1 month' string, which I know will work on 5.2. Success, my "user" cookie now expires as it should. Except I still dont have an "up2date" cookie. var_dump proves that the function is being called to set it and has all the correct parameters but it isn't setting the second cookie. I wouldn't be as frustrated if it just didn't work at all. That would at least tell me that I was doing something wrong and I could weedle it out. But for it to tease me like this... What am I missing? Does anyone have any ideas?

var_dump results:

//Local server
string(4) "user"
string(3) "bob"
int(1337797496)
int(1338488696)

string(7) "up2date"
string(19) "2012-04-20 10:52:09"
int(1337797496)
int(1338488696)

//Remote server
string(4) "user"
string(3) "bob"
int(1337795061)
bool(false)

string(7) "up2date"
string(19) "2012-04-23 09:14:19"
int(1337795061)
bool(false)

Thanks to DaveRandom. He deserves the credit here and if he adds his answer I'll switch my accepted to his. I just added this answer so I don't have any unanswered questions.

The Solution: I had output before I called the second cookie so it was failing to set it. I just moved where I set the cookie.

Thank you all for your suggestions and time!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM