neex1233 Posted December 19, 2010 Share Posted December 19, 2010 This has been driving me crazy for hours! I am trying to set a cookie: setcookie("username", $_POST['user']); And for a while it wasn't working, then it started randomly working. Now, when I try to use if(isset($_COOKIE['username'])) PHP is saying the cookie does not exist. PLEASE help! Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/222106-cookies/ Share on other sites More sharing options...
quelle Posted December 19, 2010 Share Posted December 19, 2010 use the setcookie function before any <html> tags cuz cookies have to be sent before any header information otherwise wont work. If the problem still is there try adding these 2 line before the function ob_start(); ob_clean(); Quote Link to comment https://forums.phpfreaks.com/topic/222106-cookies/#findComment-1149119 Share on other sites More sharing options...
neex1233 Posted December 19, 2010 Author Share Posted December 19, 2010 It's not before any tags, and those tags aren't helping. I tried putting var_dump($_COOKIE); on the page I'm having the problem, and it doesn't do anything. However, if I do it on a different page, it works. Quote Link to comment https://forums.phpfreaks.com/topic/222106-cookies/#findComment-1149121 Share on other sites More sharing options...
quelle Posted December 19, 2010 Share Posted December 19, 2010 Dont understand u at all. This "It's not before any tags, and those tags aren't helping" - whose tags ? "on the page I'm having the problem, and it doesn't do anything. However, if I do it on a different page, it works." - what do you mean with diferent ? Quote Link to comment https://forums.phpfreaks.com/topic/222106-cookies/#findComment-1149122 Share on other sites More sharing options...
Pikachu2000 Posted December 19, 2010 Share Posted December 19, 2010 Post the code you're having problems with. Quote Link to comment https://forums.phpfreaks.com/topic/222106-cookies/#findComment-1149124 Share on other sites More sharing options...
neex1233 Posted December 19, 2010 Author Share Posted December 19, 2010 <?php /* $con make a connection with database */ $con=mysql_connect("localhost","root",""); //select database mysql_select_db("users"); /* Below two commands will store the data in variables came from form input */ $username=$_POST['user']; $password=$_POST['pass']; /* below two commands are sql injection which stops extra characters as input */ $user=mysql_real_escape_string($username); $pass=mysql_real_escape_string($password); $query=mysql_query("SELECT * FROM users where user='$user' AND pass='$pass' "); $count=mysql_num_rows($query); if($count==1) /* $count checks if username and password are in same row */ { echo "Login Successful"; $hour = 86400*365; /* $hour sets cookie storage time for 1 hour */ /* setcookie() function sets cookie after login */ ob_start(); ob_clean(); setcookie("username", $_POST['user']); setcookie("password", $_POST['pass']); header("location:/"); /* header() funtion redirect user to members page */ } else { echo "Username or password is incorrect"; } ?> And <?php ob_start(); ob_clean(); $user = $_COOKIE['username']; if(isset($user)) { } else { header("location: /users/login.php"); } ?> I know the cookies exist, because I looked in Add 'n Edit Cookies. Quote Link to comment https://forums.phpfreaks.com/topic/222106-cookies/#findComment-1149127 Share on other sites More sharing options...
Pikachu2000 Posted December 19, 2010 Share Posted December 19, 2010 You have an echo immediately before you attempt to set the cookie. That won't work that way, there can be no output to the browser prior to setting the cookie. Quote Link to comment https://forums.phpfreaks.com/topic/222106-cookies/#findComment-1149129 Share on other sites More sharing options...
neex1233 Posted December 19, 2010 Author Share Posted December 19, 2010 It's not the actual making of the cookie that's the problem, it's the detection of the cookie. The cookie is made fine, I've checked in Add 'n Edit Cookies, but this code: <?php ob_start(); ob_clean(); $user = $_COOKIE['username']; if(isset($user)) { } else { header("location: /users/login.php"); } ?> isn't working. The code works on other pages, though. Quote Link to comment https://forums.phpfreaks.com/topic/222106-cookies/#findComment-1149130 Share on other sites More sharing options...
PFMaBiSmAd Posted December 19, 2010 Share Posted December 19, 2010 You are not using the 4th parameter in the setcookie(), so the cookie is only available at the same path where it was set. Quote Link to comment https://forums.phpfreaks.com/topic/222106-cookies/#findComment-1149131 Share on other sites More sharing options...
Pikachu2000 Posted December 19, 2010 Share Posted December 19, 2010 You should be developing with error reporting on also. Quote Link to comment https://forums.phpfreaks.com/topic/222106-cookies/#findComment-1149132 Share on other sites More sharing options...
www333 Posted December 19, 2010 Share Posted December 19, 2010 Cookies will not effect in its page, should access cookies in others page. Quote Link to comment https://forums.phpfreaks.com/topic/222106-cookies/#findComment-1149154 Share on other sites More sharing options...
neex1233 Posted December 19, 2010 Author Share Posted December 19, 2010 Thank you guys so much! (I'm a n00b) Quote Link to comment https://forums.phpfreaks.com/topic/222106-cookies/#findComment-1149272 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.