...

View Full Version : Cookie / server problem



mark-ids
03-16-2010, 10:52 AM
Hi All

I hope you can help.
I'm moving my site over to a new server and during testing I've found an error with setcookie. The code I have is:


$arrayfirstname = ucfirst($table[firstname]);
$arraysurname = ucfirst($table[surname]);
$arrayusername = $table[username];
$forever = 99999999999999;

setcookie("firstname",$arrayfirstname,$forever,"/",".instructor.ignition-driving-school.co.uk",1);
setcookie("surname",$arraysurname,$forever,"/",".instructor.ignition-driving-school.co.uk",1);
setcookie("username",$arrayusername,$forever,"/",".instructor.ignition-driving-school.co.uk",1);

For some reason it's not working now. The only thing that has changed is the url (which I've updated above( and the server. Could this be causing problems?

Any help would be much appreciated.

Thanks
Mark

Goldfish
03-16-2010, 01:09 PM
What error are you getting?
Depending on the version of PHP running on the server and the character set in your script it might be a PHP bug.

IFF the error is cookie canīt be set because output startet before, and IFF your script is using UTF-8 then you have to save your sript as "UFT-8 without BOM", else you have to tell us what the error is.

mark-ids
03-16-2010, 02:16 PM
Hi Goldfish

The error is that it's not setting the cookie. From my vista files, the old site read:


__utma
14167279.430960242.1263657159.1268648692.1268726701.38
instructor.ignitiondrivingschool.com/
1600
1367383552
30212738
1619748448
30065887
*
__utmz
14167279.1267015699.26.9.utmcsr=intensives.ignitiondrivingschool.com|utmccn=(referral)|utmcmd=referr al|utmcct=/
instructor.ignitiondrivingschool.com/
1600
1615137664
30098616
2751655712
30061903
*
firstname
Mark
instructor.ignitiondrivingschool.com/
1536
1422649728
1727735832
1705538448
30065887
*
surname
Williams
instructor.ignitiondrivingschool.com/
1536
1422649728
1727735832
1705688448
30065887
*
username
mark
instructor.ignitiondrivingschool.com/
1536
1422649728
1727735832
1705868448
30065887
*

Where as the new one just reads:


__utma
110690653.1314019071.1268670387.1268682400.1268725511.3
instructor.ignition-driving-school.co.uk/
1600
1812547072
30212743
2060241968
30065892
*
__utmz
110690653.1268670387.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
instructor.ignition-driving-school.co.uk/
1600
4271113472
30102468
1117034224
30065756
*

The PHP version is 5.2.9. The PHP info is http://www.instructor.ignition-driving-school.co.uk/test.php

Thanks again for the reply
Mark

Goldfish
03-16-2010, 02:53 PM
setcookie("firstname",$arrayfirstname,$forever,"/",".instructor.ignition-driving-school.co.uk",1);


The last parameter indicates "secure" meaning the cookie shall only be set via HTTPS connection.
Since your URL reads http://... not https:// that may be the problem,
but I donīt really understand what your old and new site code means.

I would suggest you first check the return value of setcookie() like this:



$returned = setcookie("firstname",$arrayfirstname,$forever,"/",".instructor.ignition-driving-school.co.uk",1);
echo $returned;


If you get a "false" there it means the setting failed at server end, a "true" indicates, the error is at the client end.

Either way you may want to set



error_reporting(E_ALL);


to see, what the actual problem is.

mark-ids
03-16-2010, 03:30 PM
$returned = setcookie("firstname",$arrayfirstname,$forever,"/",".instructor.ignition-driving-school.co.uk",1);
echo $returned;

For some reason it just returns 1

I've tried taking out the 1 at the end of the setcookie, but that doesn't seem to change anything.

Goldfish
03-16-2010, 03:42 PM
Returning 1 equals "true", which means the error is on the client end.
Either the output startet before setcookie() was called, as I said above, or the client doesnīt accept the cookie for some other reason, which could be because it is no https connection or the site has no permission in the users browser security settings.
Taking out the 1 doesnīt help, you have to replace it by a 0.

Edit:
Another possible reason is that 99999999999999 doesnīt fit into 32 bit.
Not sure how PHP handles values > 4,294,967,296 but since cookies are never valid for more than 1 year, you might want to calculate $forever properly



$time = time();
$forever = $time + 31536000;

mark-ids
03-16-2010, 04:33 PM
That's strange changing the 1 to 0, still returns a 1 error.
The date change didn't seem to do it either.

Thank you very much for your help on this one, but it looks like it's back to the drawing board. Looks like I'll be using sessions instead.

Thanks again
Mark

MattF
03-16-2010, 04:36 PM
That's strange changing the 1 to 0, still returns a 1 error.
The date change didn't seem to do it either.

Post your updated code, as it is at this moment in time.

mark-ids
03-16-2010, 04:42 PM
Hi Matt

It's



$arrayfirstname = ucfirst($table[firstname]);
$arraysurname = ucfirst($table[surname]);
$arrayusername = $table[username];
$time = time();
$forever = $time + 31536000;

$returned = setcookie("firstname",$arrayfirstname,$forever,"/","http://www.instructor.ignition-driving-school.co.uk",0);

echo $returned;
exit;

...and this is returning 1

MattF
03-16-2010, 05:02 PM
Try:



$arrayfirstname = ucfirst($table[firstname]);
$expire = (time()+3600);

setcookie('firstname', $arrayfirstname, $expire, '/', 'www.instructor.ignition-driving-school.co.uk', 0, 1);


I've just altered the expiry for testing, nothing more, btw.

mark-ids
03-16-2010, 05:09 PM
That's got it!!

Not sure what the change was, but it worked!!

Many thanks
Mark

Goldfish
03-16-2010, 05:20 PM
That simply means, your client doesnīt accept cookies that expire after more than a given time.
+3600 is one hour and may be not sufficient for you, but +31536000 (1 year) seems too long.
I donīt know where to look up the limit, I guess a bit of trial and error can find out the max.

MattF
03-16-2010, 05:29 PM
It was most likely the http:// on the domain part which was causing your problem.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum