Jump to content

PHP setcookie


Ads

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
Share on other sites

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);
}

Link to comment
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
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
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
Share on other sites

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

Link to comment
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.