Jump to content

Problem with cookies


red-x

Recommended Posts

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 :)

 

Link to comment
Share on other sites

#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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.