jjacquay712 Posted January 12, 2009 Share Posted January 12, 2009 I have a set of functions that store and retrieve info from a database about the user based on a single cookie (Kind of like php sessions). I am having trouble with getting the username from the database based on their cookie. here is my code: <?php //Connect mysql_connect(Ya right); mysql_select_db(lol); //End Connect function set_cookie($username) { for ( $i = 0; $i < 24; $i++ ) { $uid .= chr(rand(65,90)); } mysql_query("UPDATE cookie SET uid = '{$uid}' WHERE username = '{$username}' LIMIT 1 ;"); setcookie("uid", $uid); } function get_cookie() { if ( $_COOKIE['uid'] ) { $cookie = mysql_fetch_array(mysql_query("SELECT username FROM cookie WHERE uid = '{$_COOKIE['uid']}' LIMIT 1 ;")); return $cookie['username']; } else { return false; } } function kill_cookie() { $username = get_cookie(); mysql_query("UPDATE cookie SET uid = '' WHERE username = '{$username}' LIMIT 1 ;"); } ?> When i call get_cookie() on another page, it returns nothing for the username. Here is that code: <?php require("cookie_functions.php"); set_cookie("jjacquay712"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Cookie Test</title> </head> <body> <?php echo get_cookie(); ?> </body> </html> Any ideas about what might be causing my problem? Thanks, John Quote Link to comment https://forums.phpfreaks.com/topic/140592-solved-cookie-problems/ Share on other sites More sharing options...
premiso Posted January 13, 2009 Share Posted January 13, 2009 setcookie You are not using that function right. Set the other parameters. The domain should be .yourdomain.com path to / and time to however long you want the session to last. Quote Link to comment https://forums.phpfreaks.com/topic/140592-solved-cookie-problems/#findComment-735766 Share on other sites More sharing options...
jjacquay712 Posted January 13, 2009 Author Share Posted January 13, 2009 the set cookie function works fine, the problem is with get_cookie function. Quote Link to comment https://forums.phpfreaks.com/topic/140592-solved-cookie-problems/#findComment-735864 Share on other sites More sharing options...
premiso Posted January 13, 2009 Share Posted January 13, 2009 the set cookie function works fine, the problem is with get_cookie function. Hey, what do I know? I have only been coding in PHP for the past 12 years. But in the very slight case I am wrong try this: function get_cookie() { if (isset($_COOKIE['uid'])) { $query = mysql_query("SELECT username FROM cookie WHERE uid = '{$_COOKIE['uid']}' LIMIT 1") or die('COOKIE UID:' . $_COOKIE['uid'] . ' SQL STATEMENT: ' . mysql_error()); $cookie = mysql_fetch_array($query); return $cookie['username']; } else { echo 'COOKIE UID Has not been set.'; return false; } } Give that a try and see what happens. Quote Link to comment https://forums.phpfreaks.com/topic/140592-solved-cookie-problems/#findComment-735869 Share on other sites More sharing options...
corbin Posted January 13, 2009 Share Posted January 13, 2009 jjacquay712, you need to provide a third parameter to setcookie, as premiso said. Do print_r($_COOKIE), and you'll see that no cookies have been set. http://php.net/setcookie Quote Link to comment https://forums.phpfreaks.com/topic/140592-solved-cookie-problems/#findComment-735874 Share on other sites More sharing options...
jjacquay712 Posted January 14, 2009 Author Share Posted January 14, 2009 the cookies have been set (im using a firefox extension to view them) but whatever... it doesn't matter. Quote Link to comment https://forums.phpfreaks.com/topic/140592-solved-cookie-problems/#findComment-736631 Share on other sites More sharing options...
premiso Posted January 14, 2009 Share Posted January 14, 2009 the cookies have been set (im using a firefox extension to view them) but whatever... it doesn't matter. What did the get_cookie function return? Quote Link to comment https://forums.phpfreaks.com/topic/140592-solved-cookie-problems/#findComment-736649 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.