red-x Posted February 28, 2009 Share Posted February 28, 2009 Hello, I'm having a weird problem with cookies. I have a normal login form that checks for the user name and if it finds anything it creates the cookie and the user will be loged in. I have it working perfectly in my localhost and in a host that I was testing the form in. But I just got a new host and the script is not creating the cookies, I'm not sure why. I don't think is my code because it works in other hosts. But I installed phpbb and it works, so I guess its something of my code or the database. I'm going to post the code so you guys can see it. $username = $_REQUEST["username"]; $password = $_REQUEST["password"]; $remember = $_REQUEST["remember"]; $SQL = "SELECT * FROM users WHERE username = '{$username}' "; $result = mysql_query($SQL) or die(mysql_error()); $row = mysql_fetch_array($result); $pin = $row["pin"]; $user = $row["username"]; $level = $row["level"]; $active = $row["active"]; //Lets make the password. $pass = md5($password); //Lets check if the username exits in the database. $find = "SELECT * FROM users WHERE username = '{$username}' AND password = '{$pass}'"; $qry = mysql_query($find) or die ("Could not match data because ".mysql_error()); $num_rows = mysql_num_rows($qry); //Lets start checking everything. if (empty($username)) { bad_error("You need to enter your user name"); } elseif (empty($password)) { bad_error("You need to enter your password."); } elseif ($num_rows <= 0) { bad_error("Sorry, there is no username \"$username\" with the specified password"); } else { if ($active == "1") { //If the user wants to be remembered lets create a cookie for him. if (isset($remember)) { setcookie("username", $username, time()+60*60*24*100, "/"); //One year cookie. setcookie("pin", $pin, time()+60*60*24*100, "/"); setcookie("level", $level, time()+60*60*24*100, "/"); } else { //Then just create cookies for 1 hour. setcookie("username", $username, time()+3600, "/"); //1 hour cookie. setcookie("pin", $pin, time()+3600, "/"); setcookie("level", $level, time()+3600, "/"); } //And let me know I had successfully loged in. good_error("Thank you, You are now login $user."); //And redirect me. redirect('.',2000); } else { bad_error("Your account has not been active yet."); } } Thank you for any help Quote Link to comment https://forums.phpfreaks.com/topic/147267-problem-with-cookies/ Share on other sites More sharing options...
RussellReal Posted February 28, 2009 Share Posted February 28, 2009 #1 $SQL = "SELECT * FROM users WHERE username = '{$username}' "; $result = mysql_query($SQL) or die(mysql_error()); $row = mysql_fetch_array($result); $pin = $row["pin"]; $user = $row["username"]; $level = $row["level"]; $active = $row["active"]; is not needed.. get all that data from the second query #2 do the checks for if user is invalid or w\e BEFORE you do queries.. #3 on your localhost you probably have the database set up.. on the server your database probably has errors.. like wrong types.. no table at all, misspelled table name.. etc etc Quote Link to comment https://forums.phpfreaks.com/topic/147267-problem-with-cookies/#findComment-773145 Share on other sites More sharing options...
red-x Posted February 28, 2009 Author Share Posted February 28, 2009 Thanks RussellReal, for #1 and #2 I'll be fixing them but for number 3 I do have the database correct with working tables, cause the scripts displays the success message (Thank you, You are now login $user.) but no cookies are created. Quote Link to comment https://forums.phpfreaks.com/topic/147267-problem-with-cookies/#findComment-773212 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.