roper22 Posted September 24, 2007 Share Posted September 24, 2007 Hi everyone. I'm kinda new to PHP, but I feel as though I am learning quite quickly. I was attempting to make a login, but I hit a roadblock in this... -------------------------------------------------------------- <?php setcookie("cookie", "Hello.", time()+3200); ?> -------------------------------------------------------------- That code works. --------------------------------------------------------- <?php include 'connect.php'; $password = $_POST['password']; $username = $_POST['username']; $sql = "SELECT * FROM users WHERE username = '".$username."' AND password = '".md5($password)."'"; $result = mysql_query($sql); $num = mysql_num_rows($result); if ($num > 0) { //USER AND PASS ARE CORRECT $id = mysql_fetch_array($result); echo "s"; setcookie("cookie", "Hello.", time()+3200); } else { echo "f"; } ?> --------------------------------------------------------- But after taking that EXACT line, and putting it in my login script, it doesn't work. Help appreciated! Thanks tons guys. The first code can be seen active on www.trportfolio.net/cookie_test.php The second can be seen if you register at www.trportfolio.net/register.php and then login at www.trportfolio.net/login.php Quote Link to comment https://forums.phpfreaks.com/topic/70540-cookies/ Share on other sites More sharing options...
roopurt18 Posted September 24, 2007 Share Posted September 24, 2007 Cookie information is sent with headers and you can't echo anything to the browser without sending all of the headers. You don't have error reporting turned on, but if you did you would get a message about headers already sent, unable to blah blah blah. Either queue up all of your output into a variable and echo it a single time at the end, turn on output buffering, or set your cookie before you echo anything. Quote Link to comment https://forums.phpfreaks.com/topic/70540-cookies/#findComment-354420 Share on other sites More sharing options...
roper22 Posted September 24, 2007 Author Share Posted September 24, 2007 Cookie information is sent with headers and you can't echo anything to the browser without sending all of the headers. Like I said, i'm new to PHP, and to be honest, I have no idea what that means. I took out any "echo" scripts that I had, yet the cookies still don't work. Maybe I misunderstood you when you said "Set your cookie before you echo anything." My code now looks like this... <?php include 'connect.php'; $time = time(); $password = $_POST['password']; $username = $_POST['username']; $sql = "SELECT * FROM users WHERE username = '".$username."' AND password = '".md5($password)."'"; $result = mysql_query($sql); $num = mysql_num_rows($result); if ($num > 0) { //USER AND PASS ARE CORRECT $id = mysql_fetch_array($result); setcookie("cookie", "Hello.", time()+3200); header("Location: login_successful.php"); } else { header("Location: login_fail.php"); } ?> Since we were on the topic of headers, I figured i'd tackle another problem and ask for a bit of help about why this isn't working. Please realize that I am not fully aware of all these 'exceptions' yet. I am actually learning a lot of my codes from tutorials, but I am tweaking it to how i'd like it, as I attempt to learn in the process. I apologize for the newbie questions, and your help is much appreciated (Sorry for being a nub roopurt, lol.) Quote Link to comment https://forums.phpfreaks.com/topic/70540-cookies/#findComment-354438 Share on other sites More sharing options...
roopurt18 Posted September 24, 2007 Share Posted September 24, 2007 You should call exit() after making a call to header where you set the location. Headers are basically "hidden" information that is sent between the browser and web server; it's hidden in the sense that you have to make extra effort to view it in most browsers. Header information gives the browser clues on what to expect, such as what type of file the server is about to send or if it should set a cookie or not. The way the HTTP protocol works headers have to be sent before any other form of output, such as the actual HTML. The moment your script spits out any output, even a white space, you are no longer allowed to set any header information. Quote Link to comment https://forums.phpfreaks.com/topic/70540-cookies/#findComment-354446 Share on other sites More sharing options...
roper22 Posted September 24, 2007 Author Share Posted September 24, 2007 Alright, I now have a better understanding, but the cookie is still not being sent, and unless variables are output, I don't think that anything is being sent to the browser until the cookie line...? By the way, much thanks for clearing that up. Quote Link to comment https://forums.phpfreaks.com/topic/70540-cookies/#findComment-354450 Share on other sites More sharing options...
roopurt18 Posted September 24, 2007 Share Posted September 24, 2007 Well, throw an echo statement into your script as the last thing. echo "hi"; ?> If you see a 'hi' on your browser it finished executing; if you see nothing the problem is something else. Quote Link to comment https://forums.phpfreaks.com/topic/70540-cookies/#findComment-354455 Share on other sites More sharing options...
roper22 Posted September 24, 2007 Author Share Posted September 24, 2007 Alright, so I saw the "Hi." I don't get how that is helping me with the cookies Quote Link to comment https://forums.phpfreaks.com/topic/70540-cookies/#findComment-354458 Share on other sites More sharing options...
roopurt18 Posted September 25, 2007 Share Posted September 25, 2007 Well now you know your script isn't terminating early without throwing an error, which can happen. Are you sure that your DB connection is successfully made and that your query is actually returning results? Quote Link to comment https://forums.phpfreaks.com/topic/70540-cookies/#findComment-354627 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.