Page 1 of 1
php login help
Posted: Sat Jun 05, 2010 7:08 pm
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.
Re: php login help
Posted: Sat Jun 05, 2010 7:42 pm
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.
Re: php login help
Posted: Sat Jun 05, 2010 8:19 pm
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>";
}
?>
Re: php login help
Posted: Sat Jun 05, 2010 8:42 pm
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?
Re: php login help
Posted: Sat Jun 05, 2010 8:44 pm
by Benjamin

Moved to PHP - Code
Re: php login help
Posted: Sat Jun 05, 2010 8:55 pm
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>";
}
?>
Re: php login help
Posted: Sat Jun 05, 2010 9:49 pm
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
}
?>
Re: php login help
Posted: Sat Jun 05, 2010 10:17 pm
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
Re: php login help
Posted: Sat Jun 05, 2010 10:22 pm
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
Re: php login help
Posted: Sun Jun 06, 2010 10:34 am
by Jonah Bron
Replace Header("Location: index.php"); with echo "You are logged in now";
Re: php login help
Posted: Sun Jun 06, 2010 11:51 am
by acting15
that didn't work
Re: php login help
Posted: Sun Jun 06, 2010 12:24 pm
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.
Re: php login help
Posted: Sun Jun 06, 2010 4:24 pm
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.
Re: php login help
Posted: Sun Jun 06, 2010 5:08 pm
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.
Re: php login help
Posted: Sun Jun 06, 2010 5:18 pm
by acting15
type something after this?
// Run query
$r = mysql_query($q);