php login help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
acting15
Forum Newbie
Posts: 13
Joined: Sat Jun 05, 2010 6:53 pm

php login help

Post by acting15 »

Hi,

I need help in creating a login page for my website. I already have a register page set up and working perfectly. I have a table in my database called login that has 3 fields: id,user, and pass1. I also have a table called users that has all the info for the register form. I'm not sure which table to use. The names of the username and password fields in the users table are called "username" and "pass1" I also have 2 fields, 1 for the password and the other for password confirmation, pass1 and pass2 respectfully.

After I find out which table to use, I need to find out how to get the login form to work.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: php login help

Post by Jonah Bron »

You don't need two password columns. You only need one. The second box is compared against the first box when the user signs up. It's just a way of making sure the user knows he typed in what he thinks he typed.

Also, you don't even need two tables. Just put the columns in the "login" table into the "users" table. You might want to read a few tutorials on login systems.

http://www.knowledgesutra.com/forums/to ... -tutorial/
http://www.google.com/search?q=php+login+tutorial

BTW, PHP code questions belong in the PHP Code board.
acting15
Forum Newbie
Posts: 13
Joined: Sat Jun 05, 2010 6:53 pm

Re: php login help

Post by acting15 »

I apologize for posting in the wrong section, I thought the whole site was for php.
It's not working no matter what I do or do not type. I don't get errors or anything, it just tells me to enter username and password.

Code: Select all

<?php
        session_start();
        // dBase file
        include "connect_to_mysql.php";

        if ($_GET["op"] == "login")
  {
  if (!$_POST["username"] || !$_POST["pass1"])
        {
        die("You need to provide a username and password.");
        }
  
  // Create query
  $q = "SELECT * FROM `users` "
        ."WHERE `username`='".$_POST["username"]."' "
        ."AND `password`=PASSWORD('".$_POST["pass1"]."') "
        ."LIMIT 1";
  // Run query
  $r = mysql_query($q);

  if ( $obj = @mysql_fetch_object($r) )
        {
        // Login good, create session variables
        $_SESSION["valid_id"] = $obj->id;
        $_SESSION["valid_user"] = $_POST["username"];
        $_SESSION["valid_time"] = time();

        // Redirect to member page
        Header("Location: index.php");
        }
  else
        {
        // Login not successful
        die("Sorry, could not log you in. Wrong login information.");
        }
  }
        else
  {
//If all went right the Web form appears and users can log in
  echo "<form action=\"?op=login\" method=\"POST\">";
  echo "Username: <input name=\"username\" size=\"15\"><br />";
  echo "Password: <input type=\"password\" name=\"password\" size=\"8\"><br />";
  echo "<input type=\"submit\" value=\"Login\">";
  echo "</form>";
  }
        ?>
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: php login help

Post by Jonah Bron »

Well, first off

Code: Select all

if (!$_POST["username"] || !$_POST["pass1"])
needs to be replaced by

Code: Select all

if (!isset($_POST["username"]) || !isset($_POST["pass1"]))
Do you get either of the messages that you put into die() statements?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: php login help

Post by Benjamin »

:arrow: Moved to PHP - Code
acting15
Forum Newbie
Posts: 13
Joined: Sat Jun 05, 2010 6:53 pm

Re: php login help

Post by acting15 »

I get the first die message of You need to provide a username and password. I changed the code to what you provided.

Code: Select all

<?php
        session_start();
        // dBase file
        include "connect_to_mysql.php";

        if ($_GET["op"] == "login")
  {
 if  (!isset($_POST["username"]) || !isset($_POST["pass1"]))
        {
        die("You need to provide a username and password.");
        }
  
  // Create query
  $q = "SELECT * FROM `users` "
        ."WHERE `username`='".$_POST["username"]."' "
        ."AND `password`=PASSWORD('".$_POST["pass1"]."') "
        ."LIMIT 1";
  // Run query
  $r = mysql_query($q);

  if ( $obj = @mysql_fetch_object($r) )
        {
        // Login good, create session variables
        $_SESSION["valid_id"] = $obj->id;
        $_SESSION["valid_user"] = $_POST["username"];
        $_SESSION["valid_time"] = time();

        // Redirect to member page
        Header("Location: index.php");
        }
  else
        {
        // Login not successful
        die("Sorry, could not log you in. Wrong login information.");
        }
  }
        else
  {
//If all went right the Web form appears and users can log in
  echo "<form action=\"?op=login\" method=\"POST\">";
  echo "Username: <input name=\"username\" size=\"15\"><br />";
  echo "Password: <input type=\"password\" name=\"password\" size=\"8\"><br />";
  echo "<input type=\"submit\" value=\"Login\">";
  echo "</form>";
  }
        ?>
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: php login help

Post by Jonah Bron »

The die() function stops PHP from processing any more of the page. You don't see the form because you're not letting it show. Here's your code. I've made some necessary revisions.

Code: Select all

<?php
        session_start();
        // dBase file
        include "connect_to_mysql.php";

        if (isset($_GET["op"]) && isset($_POST["username"]) && isset($_POST["pass1"]))
  {
 
  // Create query
  $q = "SELECT * FROM `users` "
        ."WHERE `username`='".$_POST["username"]."' "
        ."AND `password`=PASSWORD('".$_POST["pass1"]."') "
        ."LIMIT 1";
  // Run query
  $r = mysql_query($q);

  if ( $obj = @mysql_fetch_object($r) )
        {
        // Login good, create session variables
        $_SESSION["valid_id"] = $obj->id;
        $_SESSION["valid_user"] = $_POST["username"];
        $_SESSION["valid_time"] = time();

        // Redirect to member page
        Header("Location: index.php");
        }
  else
        {
        // Login not successful
        die("Sorry, could not log you in. Wrong login information.");
        }
  }
        else
  {
//If all went right the Web form appears and users can log in
?>
<form action="?op=login" method="POST">
Username: <input name="username" size="15"><br />
Password: <input type="password" name="password" size="8"><br />
<input type="submit" value="Login">
</form>
<?php
}
?>
acting15
Forum Newbie
Posts: 13
Joined: Sat Jun 05, 2010 6:53 pm

Re: php login help

Post by acting15 »

I could see the form with the code I had. It just gives me that message no matter what I put into the form.

godaddy wont let me upload the new code you gave me to see if that works
acting15
Forum Newbie
Posts: 13
Joined: Sat Jun 05, 2010 6:53 pm

Re: php login help

Post by acting15 »

ok now it doesn't show any messages. When I enter something in and click login, it clears the form and shows the login page
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: php login help

Post by Jonah Bron »

Replace Header("Location: index.php"); with echo "You are logged in now";
acting15
Forum Newbie
Posts: 13
Joined: Sat Jun 05, 2010 6:53 pm

Re: php login help

Post by acting15 »

that didn't work
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: php login help

Post by Jonah Bron »

Did it say "you are logged in now"? If not, echo something meaningless right after the query, but before the if statement. What we are doing here, is finding out where exactly the code stops. That's the first thing to do in debugging.
acting15
Forum Newbie
Posts: 13
Joined: Sat Jun 05, 2010 6:53 pm

Re: php login help

Post by acting15 »

It doesn't say anything. You can take a look at what it does. http://www.applestarr.com register and see what's going on, I'll delete your account later if you want

btw, I'm not trying to promote my site on here in any way, I'm just asking for help getting my login form to work.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: php login help

Post by Jonah Bron »

Did you remember to output something after the query? Echo mysql_error().

By the way, you should put a captcha into your signup page.
acting15
Forum Newbie
Posts: 13
Joined: Sat Jun 05, 2010 6:53 pm

Re: php login help

Post by acting15 »

type something after this?

// Run query
$r = mysql_query($q);
Post Reply