Jump to content

Sessions and Cookies


forumnz

Recommended Posts

I'm using sessions and cookies for a login. I use the following code snippet as part of a login code and the code after to echo the cookie name, but it doesn't work. Why would this be? Thanks heaps, Sam.

 

<?php
if(mysql_num_rows($result)>0) {
		//Login Successful
		session_regenerate_id();
		$member=mysql_fetch_assoc($result);
		$_SESSION['SESS_MEMBER_ID'] = $member['id'];
		$_SESSION['SESS_LEVEL_ID'] = $member['lvl'];
		session_write_close();
		setcookie("user", "Alex Porter", time()+3600);
}
?>

 

<?php
echo $_COOKIE["user"];
?>

Link to comment
https://forums.phpfreaks.com/topic/91200-sessions-and-cookies/
Share on other sites

Also, are you calling the cookie on the same page in which you're setting it?  If you're not you need to specify the save path in the cookie as well.

 

setcookie("user", "Alex Porter", time()+3600,"/");

 

BTW are you declaring the session to be started anywhere?  session_start() needs to be declared at the top of every page that uses a session variable.

Link to comment
https://forums.phpfreaks.com/topic/91200-sessions-and-cookies/#findComment-467458
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.