title: PHP code issue - login and registration
body: <p>I'm a newbie to PHP and MySQL, and I have used this code to create a registration and login page.</p>
  
  <p>the database is <strong>dbusers</strong> with:</p>
  
  <ul>
  <li>id (PK, Auto Increment, Int(10) )</li>
  <li>username (Unique, Varchar(16))</li>
  <li>password (Varchar(16))</li>
  <li>email (Varchar(45))</li>
  </ul>
  
  <p>Here is the code for <strong>register.php</strong> (uses a form which is not relevant):
      
  
  <pre><code>// dbConfig.php is a file that contains your
  // database connection information. This
  // tutorial assumes a connection is made from
  // this existing file.
  include ("dbconfig.php");
  
  
  //Input validation and the dbase code
  if ( $_GET["op"] == "reg" )
  {
  $bInputFlag = false;
  foreach ( $_POST as $field )
  {
  if ($field == "")
  {
  $bInputFlag = false;
  }
  else
  {
  $bInputFlag = true;
  }
  }
  // If we had problems with the input, exit with error
  if ($bInputFlag == false)
  {
  die( "Problem with your registration info. "
  ."Please go back and try again.");
  }
  
  // Fields are clear, add user to database
  // Setup query
  $q = "INSERT INTO `dbusers` (`username`,`password`,`email`) "
  ."VALUES ('".$_POST["username"]."', "
  ."PASSWORD('".$_POST["password"]."'), "
  ."'".$_POST["email"]."')";
  
  // Run query
  $r = mysql_query($q);
  
  // Make sure query inserted user successfully
  if ( !mysql_insert_id() )
  {
  die("Error: User not added to database.");
  }
  else
  {
  // Redirect to thank you page.
  Header("Location: register.php?op=thanks");
  }
  } // end if
  
  
  //The thank you page
  elseif ( $_GET["op"] == "thanks" )
  {
  echo "&lt;h2&gt; Thanks for registering!&lt;/h2&gt; ";
  }
  
  // EOF
  ?&gt;
  </code></pre>
  
  <p>The code for <strong>login.php</strong> (to deal with the form @ another file which is not relevant):</p>
  
  <pre><code>&lt;body&gt;
  
      &lt;?php
      session_start();
      // dBase file
      include ("dbconfig.php");
  
      if ($_GET["op"] == "login")
      {
      if (!$_POST["username"] || !$_POST["password"])
      {
      die("You need to provide a username and password.");
      }
  
      // Create query
      $q = "SELECT * FROM `dbusers` "
      ."WHERE `username`='".$_POST["username"]."' "
      ."AND `password`=PASSWORD('".$_POST["password"]."') "
      ."LIMIT 1";
  
      // Run query
      $r = mysql_query($q);
  
      if ( $obj = @mysql_fetch_object($r) )
      {
      // Login good, create session variables
      $_SESSION["valid_id"] = $obj-&gt;id;
      $_SESSION["valid_user"] = $_POST["username"];
      $_SESSION["valid_time"] = time();
  
      // Redirect to member page
      Header("Location: home.php");
      }
  
      else
      {
      // Login not successful
      die("Sorry, could not log you in. Wrong login information.");
      }
      }
      ?&gt;
  
  &lt;/body&gt;
  </code></pre>
  
  <p>And the beginning code on my first page <strong>home.php</strong>:</p>
  
  <pre><code>&lt;body&gt;
  
  &lt;?php
      session_start();
  
      if (!$_SESSION["valid_user"])
      {
      // User not logged in, redirect to login page
      Header("Location: login.html");
      }
  
      // Display Member information
      echo "&lt;p&gt;User ID: " . $_SESSION["valid_id"];
      echo "&lt;p&gt;Username: " . $_SESSION["valid_user"];
      echo "&lt;p&gt;Logged in: " . date("m/d/Y", $_SESSION["valid_time"]);
  ?&gt;
  
  &lt;div id="MENUBAR"&gt;
  &lt;ul id="LINKS" class="MenuBarHorizontal"&gt;
      &lt;li&gt;&lt;a class="MenuBarItemSubmenu"&gt;Clientes&lt;/a&gt;
        &lt;ul&gt;
  </code></pre>
  
  <p>Now the questions:</p>
  
  <p>1) what does the and $_SESSION["valid_id"] = $obj->id do what is "->" symbol and why is it pointing to id (@<strong>login.php</strong>)?</p>
  
  <pre><code> if ( $obj = @mysql_fetch_object($r) )
          {
          // Login good, create session variables
          $_SESSION["valid_id"] = $obj-&gt;id;
          $_SESSION["valid_user"] = $_POST["username"];
          $_SESSION["valid_time"] = time();
  </code></pre>
  
  <p>2) Why do i get keep getting an invalid login? there are multiple users created (and written) successfully on the above mentioned database, but i still get the same error...</p>
  
  <p>Thanks in advance.</p>
  
  <p>Alex.</p>