Jump to content

Recommended Posts

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>";

}

Link to comment
https://forums.phpfreaks.com/topic/210442-php-setcookie/
Share on other sites

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)?

Link to comment
https://forums.phpfreaks.com/topic/210442-php-setcookie/#findComment-1098089
Share on other sites

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.

 

Link to comment
https://forums.phpfreaks.com/topic/210442-php-setcookie/#findComment-1098097
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/210442-php-setcookie/#findComment-1098198
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.