jparker Posted May 31, 2006 Share Posted May 31, 2006 HelloI'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 appreciatedJames Quote Link to comment https://forums.phpfreaks.com/topic/10853-php-cookie-problem/ Share on other sites More sharing options...
wildteen88 Posted May 31, 2006 Share Posted May 31, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/10853-php-cookie-problem/#findComment-40565 Share on other sites More sharing options...
jparker Posted May 31, 2006 Author Share Posted May 31, 2006 sorry I'll elaborateI 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 pageprint "<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 blankYour help would be appreciatedJames Quote Link to comment https://forums.phpfreaks.com/topic/10853-php-cookie-problem/#findComment-40574 Share on other sites More sharing options...
jparker Posted May 31, 2006 Author Share Posted May 31, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/10853-php-cookie-problem/#findComment-40610 Share on other sites More sharing options...
lead2gold Posted May 31, 2006 Share Posted May 31, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/10853-php-cookie-problem/#findComment-40619 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.