Jump to content


Photo

php cookie problem


  • Please log in to reply
4 replies to this topic

#1 jparker

jparker
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 31 May 2006 - 12:44 PM

Hello

I'm having a problem with cookies now that I have moved my code to the production server.

I have set the cookies with

setcookie ("username", $row["username"], time()+604800);
setcookie ("userid", $row["urn"], time()+604800);

and retrieve with <?php echo $_COOKIE["username"] ?> which works fine with Apache and PHP for windows. If I move it to my production server which is Apache it doesn't work. I can't update the version of either Apache (API Version: 20020903) or PHP (Version: 4.3.10) so is it possible to get round this problem or do I have to think of using sessions or even worse javascript cookies.

Your help would be much appreciated

James

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 31 May 2006 - 02:11 PM

Are you setting and using the cookies in the same piece if code? If you are this is not possible with cookies unless you refresh the page then PHP will be able to access the cookies you have just set.

Also it does help if you describe whats wrong in a little more detail becuase "!it doesn't work" isn't very informative.

Also incase there might be an error kicking up somewhere which is stoping setcookie from setting a cookie make sure you look at your servers error log file. You should be able get acess to your error log file from your sites control panel.

#3 jparker

jparker
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 31 May 2006 - 02:46 PM

sorry I'll elaborate

I am setting a cookie on a login script (index.php) with the following code : -

while($row=mysql_fetch_array($result)){
setcookie ("username", $row["username"], time()+604800);
setcookie ("userid", $row["urn"], time()+604800);
}

As this is successful login I am then redirecting using the following code to redirect to the home page

print "<script language=\"JavaScript\">";
print "window.location = 'home.php' ";
print "</script>";

then once in the home.php page I am simply trying to display the name to welcome the user : -

Hello and welcome <?php echo $_COOKIE["username"] ?>

As I said this works fine under Apache and Php on windows yet on Apache for linux all that is displayed is "Hello and welcome" as the cookie holds no value.

In edition if I run the following code on the home.php page

<?php
echo $_COOKIE["username"]."<BR>";
echo $HTTP_COOKIE_VARS["username"]."<BR>";

// Another way to debug/test is to view all cookies
print_r($_COOKIE);
?>

I get the following output : -



Array ( [PHPSESSID] => 686b88e663c215f4d150fbaf620df7ee )

whereby the first two statements are blank

Your help would be appreciated

James

#4 jparker

jparker
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 31 May 2006 - 04:23 PM

I have simplified the scripts further

I have a script called testcookie.php which I have the following code in

<?php
// Create a session brower cookie (0: denotes deletion on browser closure)
setcookie ("username", "jparker", 0);

if(isset($_COOKIE["username"])) {
echo $_COOKIE["username"] ;
}
else {
echo "Cookie not set";
}
?>

The first time I open the page I get "Cookie not set" and then when I refresh I get "jparker" so this is working fine.

If I then navigate to testcookie1.php in the same browser which has the following code : -

<?php
if(isset($_COOKIE["username"])) {
echo $_COOKIE["username"] ;
}
else {
echo "Cookie not set";
}
?>

I get "Cookie not set" rather than the "jparker" which was set on the previous page.

the problem is not so much that it doesn't hold a cookie value but it doesn't take the value to another page.

Is this a common problem or am I doing something really stupid?

JP



#5 lead2gold

lead2gold
  • Members
  • PipPipPip
  • Advanced Member
  • 164 posts
  • LocationOttawa, On

Posted 31 May 2006 - 05:01 PM

Do you have any firewall software (like zonealarm) installed?

ZoneAlarm for example marks 127.0.0.1 (localhost) as a trusted site and will block anything outside of that.
It could be the reason why your cookies arn't working on the remote site, but they are when you test it on your own local computer.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users