catelee2u Posted November 8, 2009 Share Posted November 8, 2009 Hi I have a page and want to set 2 cookies. The first cookie seems to work whereas the second does not. <?php // Connect to Database $con = mysql_connect("localhost","username","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } //Checking username and password are correct mysql_select_db("DB")or die("Cannot select DB"); $loginname = mysql_real_escape_string($_POST['loginname']); $pwd = md5($_POST['loginpwd']); $verify = mysql_num_rows(mysql_query("SELECT * FROM members WHERE loginname = '$loginname' AND loginpwd = '$pwd' ")); if ($verify != 1 ) {die('Your username or password is incorrect.');} //redirect header("Location: http://192.168.0.8/members.php"); //set cookie 2 hours (60*60*2=7200) $period = 7200 + time(); setcookie("ST[username]", $_POST['loginname'] ,$period); //Get id cookie $getid = mysql_query("SELECT id FROM members WHERE loginname = '$loginname'"); $id = mysql_fetch_array($getid, MYSQL_BOTH); setcookie("ST[iD]", $id ,$period); ?> I cannot figure out why the second cookie is not set. Thanks in advance Link to comment https://forums.phpfreaks.com/topic/180724-solved-cookie-not-set/ Share on other sites More sharing options...
PFMaBiSmAd Posted November 8, 2009 Share Posted November 8, 2009 The second parameter of setcookie() is a string value. mysql_fetch_array() returns an array. You would need to just use the 'id' element of the array that is being returned. Link to comment https://forums.phpfreaks.com/topic/180724-solved-cookie-not-set/#findComment-953477 Share on other sites More sharing options...
catelee2u Posted November 8, 2009 Author Share Posted November 8, 2009 ok I'm stumped I thought that was what I was doing. So how do I get the id out of the database in a useable format? I know I need the actual id number to go into the cookie and not what is pulled from the query or array but am at a loss as to how. Link to comment https://forums.phpfreaks.com/topic/180724-solved-cookie-not-set/#findComment-953486 Share on other sites More sharing options...
Alex Posted November 8, 2009 Share Posted November 8, 2009 $id['id']; Assuming your column name is 'id'. Link to comment https://forums.phpfreaks.com/topic/180724-solved-cookie-not-set/#findComment-953487 Share on other sites More sharing options...
catelee2u Posted November 8, 2009 Author Share Posted November 8, 2009 Hi Thankyou both. It took me a while to figure out what you meant but got there in the end. Thankyou very much it works now. Now I have: //Get id cookie $getid = mysql_query("SELECT id FROM members WHERE loginname = '$loginname'"); $id = mysql_fetch_array($getid); setcookie("STID", $id['id']); Link to comment https://forums.phpfreaks.com/topic/180724-solved-cookie-not-set/#findComment-953503 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.