Forum Moderators: coopster

Message Too Old, No Replies

Problem with small login script

         

dwighty

1:31 pm on Dec 11, 2005 (gmt 0)

10+ Year Member



Hey guys,

Below is a script that i have written but can't seem to get it to work... When submit is clicked it always displays "Username is incorrect" I know it isnt. Maybe i have missed something in writing this so would appreciate it if someone could give it a quick look over and see if i have made any mistakes...

/********SCRIPT FOR LOGIN.PHP*********/

//Start Session
session_start();

//Include The Config File
include("config.php");

if (isset($_POST['submit']))
{
$username = $_POST['username'];
$password = md5($_POST[password]);
$result = mysql_query("Select * FROM [db_table_name] WHERE user_name='$username' LIMIT 1",$con);
if(mysql_num_rows($result)>0)
{
$row = mysql_fetch_array($result, MYSQL_BOTH);
if($password == $row["user_pass"])
{
$_SESSION['loginok'] = "ok";
$_SESSION['username'] = "username";
$_SESSION['password'] = "password";
$_SESSION['level'] = $row["user_level"];
header("Location: main.php");
}
else
{
$msgpass = "Password incorrect";
}
}
else
{
$msgname = "Username incorrect";
}
}

/********The FORM*********/

<form name="" action="" method="post">
Username:<br />
<input name="username" type="text" id="username" width="200"><?php echo "&nbsp;&nbsp;<font color='red'>$msgname</font>"?><br /><br />
Password:<br />
<input name="password" type="password" id="password" width="200"><?php echo "&nbsp;&nbsp;<font color='red'>$msgpass</font>"?><br /><br />
<input type="submit" name="submit" value="Log in">
</form>

Thanks in advance

mm1220

1:53 pm on Dec 11, 2005 (gmt 0)

10+ Year Member



Instead of this:
$result = mysql_query("Select * FROM [db_table_name] WHERE user_name='$username' LIMIT 1",$con);

Try this:
$query = "Select * FROM [db_table_name] WHERE user_name='$username' LIMIT 1";
$result = mysql_query($query,$con);

The way you had it arranged was not submitting the value of the variable $username but the string "$username".

dwighty

2:31 pm on Dec 11, 2005 (gmt 0)

10+ Year Member



Thanks for your help.

There was also another issue I had made a sleepy mistake in the config file so there for it was never connecting in the first place, so everytime showing the %msgname error!

Ohwell..

thanks for yoru help.

dwighty

2:35 pm on Dec 11, 2005 (gmt 0)

10+ Year Member



Something else you maybe able to help me with...

I want to log which user has logged in and when and place this into a log file (in a dbtable)

As well as have a stat which shows the user when they were last logged in.

How do i go about doing this using the login script above?

Thanks

coopster

4:48 pm on Dec 14, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



First you create the table in your database. Next, immediately after you have confirmed a successful login you would insert a row into or perhaps update that table.