Ads Posted August 11, 2010 Share Posted August 11, 2010 Hey Guys, I am trying to set a cookie so that when I registered user returns it Auto Logins them in. I am able to accomplish this on my Local server but as Soon as I upload to a web server to test it, it doesn't work. Is anyone able to shed some light on this for me. $usass = $mem['userid']; if (isset($_POST['rem'])) { $year = 3600*24*365; setcookie ("id" , "$usass", time()+$year); } if the code for setting the cookie, and the code for checking the cookie is Below if(isset($_COOKIE['id'])) { $_SESSION['userid'] = $_COOKIE['id']; print "<script>"; print " self.location='loggedin.php';"; // Comment this line if you don't want to redirect print "</script>"; } Quote Link to comment https://forums.phpfreaks.com/topic/210442-php-setcookie/ Share on other sites More sharing options...
jackw899 Posted August 11, 2010 Share Posted August 11, 2010 Hi, Try changing $usass = $mem['userid']; if (isset($_POST['rem'])) { $year = 3600*24*365; setcookie ("id" , "$usass", time()+$year); } to $usass = $mem['userid']; if (isset($_POST['rem'])) { $year = 3600*24*365; setcookie ("id" , $usass, time()+$year); } Quote Link to comment https://forums.phpfreaks.com/topic/210442-php-setcookie/#findComment-1098086 Share on other sites More sharing options...
PFMaBiSmAd Posted August 11, 2010 Share Posted August 11, 2010 Define: it doesn't work. What does it do? Is the cookie being set in your browser when you check? Is the cookie being set but the value is empty? Is your form setting $_POST['rem']? Have you checked by echoing it in your php code? Is $mem['userid'] set to an expected value? Have you checked by echoing it in your php code? Is that all the code on your page that sets the cookie (in case you are outputting something to the browser that would prevent the setcookie() from working on your live server)? Quote Link to comment https://forums.phpfreaks.com/topic/210442-php-setcookie/#findComment-1098089 Share on other sites More sharing options...
Ads Posted August 11, 2010 Author Share Posted August 11, 2010 When I try and set the cookie on the Server I get this Error: Warning: Cannot Modify header information - headers already sent by (output started at /home2/*******/Public_html/config.php:3) in /home2/*******/public_html/authenticate.php on line 58 Line 3 of COnfig.php is just an Open php tag. and Line 58 of Authenticate is the setcookie command. I echoed out the $_POST['rem'] and $mem['userid'] both come out as the values that they should. When I run it Locally it sets the cookie with the right value, when run on the server it doesn't set anything. Quote Link to comment https://forums.phpfreaks.com/topic/210442-php-setcookie/#findComment-1098097 Share on other sites More sharing options...
PFMaBiSmAd Posted August 11, 2010 Share Posted August 11, 2010 (output started at /home2/*******/Public_html/config.php:3) If line 3 is an opening <?php tag, what ARE lines 1 and 2, because they are content that is being sent to the browser and is preventing the cookie from being set. Quote Link to comment https://forums.phpfreaks.com/topic/210442-php-setcookie/#findComment-1098102 Share on other sites More sharing options...
freeloader Posted August 11, 2010 Share Posted August 11, 2010 Basically you're building a remember me feature, right? You could take a look in this topic: http://www.phpfreaks.com/forums/index.php/topic,307065.0.html I posted an example there on how I did this. You'd have to add some sql injection security, since I'm only using this in a close environment I didn't really take care of that fully. Make sure you aren't printing anything to the browser before the cookie stuff is handled. Quote Link to comment https://forums.phpfreaks.com/topic/210442-php-setcookie/#findComment-1098198 Share on other sites More sharing options...
Ads Posted August 12, 2010 Author Share Posted August 12, 2010 Thanks for your Help, once again you have saved me for a nightmare of over looking the simple facts. I was adding A style Sheet and an Apple icon in the Config file, Which the Cookie setting didn't like, It seems to all work now, Thanks Quote Link to comment https://forums.phpfreaks.com/topic/210442-php-setcookie/#findComment-1098317 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.