View Full Version : Register/Login Script Refusing to Work


onigiri
08-18-2006, 04:26 AM
I created a login system in PHP/MySQL. But for some reason, when I tried the login script, it refused to log me in even though I filled in the fields with what was in the database. I suspect it has something to do with the fact that the registration script inserted the name of the database I was using into the name field, but I put the name of the database into the name field for the login form, and it still refused to work. Confusing? Let me try to make myself clearer:

The register script is supposed to enter the name and password of the person registering into the name and password fields in the database. However, the register script inserts the name of the database (I'll call it 'Bob') into the name field. So when I log in, I put the name of the 'Bob' into the name field of the login form, and it still refuses to log me in!

Register code:
<?php

//check to make sure the variables were passed
if(empty($_POST['name'])) {
$name = FALSE;
echo "No name inputed!";
exit;
} else {
$name = $_POST['name'];
}

if(empty($_POST['password'])) {
$pass = FALSE;
echo "No password inputed!";
exit;
} else {
$pass = $_POST['password'];
}

if(!ereg("^.+@.+\..+$", $_POST['email']) || empty($_POST['email'])) {
$email = FALSE;
echo "A valid email was not supplied!";
exit;
} else {
$email = $_POST['email'];
}

//connect to the database
require("config.php");

//make sure the username does not already exist
$check_original = "SELECT member_name FROM members WHERE member_name = '$name'";
$check_original_res = mysql_query($check_original) or die(mysql_error());

if(mysql_num_rows($check_original_res) == 1) {
//username is in use
echo "Username is in use! Please try another one.";
exit;
} else {

//insert the user into the database
$date = date('l, F j, Y \a\t g:i A');
$insert_user = "INSERT INTO members VALUES('', '$name', '$pass', '$email', '$date', '', 'U', 'Member', '', '', '')";
$insert_user_res = mysql_query($insert_user) or die(mysql_error());

if($insert_user_res) {
//send the email
$from = "looking_for_latex@hotmail.com";
$subject = "Registration Details";

$message = "Hi $name, <br><br> Thank you for registrating an account with us. Here's the information you registered:- <br><br>";
$message .= "Username: $name<br> Password: $pass <br> ";

if(mail($email, $subject, $message)) {
echo "Thank you for joining. An email has been sent to the address you entered with your registration details.";
} else {
echo "Sorry, the email couldn't be sent for some reason. But you're still registered.";
}
}else {
echo "Sorry, the insert process went awry.";
}
}

?>

Login code:
<?php

//check for the required information
if(empty($_POST['name']) || empty($_POST['password'])) {
$variables = FALSE;
$display = "All fields are required!";
} else {
$username = $_POST['name'];
$password = $_POST['password'];
}

//connect
require("config.php");

//run a query to check if the user is a valid one
$verify = "SELECT member_name, member_password, member_rank FROM members WHERE member_name = '$username' AND member_password = '$password'";
$verify_res = mysql_query($verify) or die(mysql_error());

if(mysql_num_rows($verify_res) !== '1') {
//no match
$display = "No member found with that data. Please try again.";
} else {
$member_info = mysql_fetch_array($verify_res);
$member_name = $member_info['member_name'];
$member_password = $member_info['member_password'];
$member_rank = $member_info['member_rank'];

//set a cookie
setcookie ("member",$member_name,time()+1957240,"/");
setcookie ("rank",$member_rank,time()+1957240,"/");

//start a session if cookies are blocked
session_start();

$_SESSION['name'] = $member_name;
$_SESSION['rank'] = $member_rank;

//update the login
$date = date("l, F j, Y \a\t g:i A");
$update_login = "UPDATE members SET login = '$date' WHERE member_name = '$member_name' AND member_password = '$member_password'";
$update_login_res = mysql_query($update_login) or die(mysql_error());

//if all worked out, redirect the user
if($update_login_res) {
header("Location: index.php");
}
}

?>

iTom
08-18-2006, 10:40 AM
I think I may have fixed your problem:

<?php

//check for the required information
if(empty($_POST['name']) || empty($_POST['password'])) {
$variables = FALSE;
$display = "All fields are required!";
} else {
$username = $_POST['name'];
$password = $_POST['password'];
}

//connect
require("config.php");

//run a query to check if the user is a valid one
$verify = "SELECT member_name, member_password, member_rank FROM members WHERE member_name = '$username' AND member_password = '$password'";
$verify_res = mysql_query($verify) or die(mysql_error());

if(mysql_num_rows($verify_res) == 0) {
//no match
$display = "No member found with that data. Please try again.";
} else {
$member_info = mysql_fetch_array($verify_res);
$member_name = $member_info['member_name'];
$member_password = $member_info['member_password'];
$member_rank = $member_info['member_rank'];

//set a cookie
setcookie ("member",$member_name,time()+1957240,"/");
setcookie ("rank",$member_rank,time()+1957240,"/");

//start a session if cookies are blocked
session_start();

$_SESSION['name'] = $member_name;
$_SESSION['rank'] = $member_rank;

//update the login
$date = date("l, F j, Y \a\t g:i A");
$update_login = "UPDATE members SET login = '$date' WHERE member_name = '$member_name' AND member_password = '$member_password'";
$update_login_res = mysql_query($update_login) or die(mysql_error());

//if all worked out, redirect the user
if($update_login_res) {
header("Location: index.php");
}
}

?>

onigiri
08-18-2006, 11:47 PM
Hmm, it still doesn't work. :\ Thanks anyways. I guess I'll just have to try something else.

Douglas
08-19-2006, 12:15 AM
What is the name of the field that holds the username on the login page (HTML) check and make sure it is equal to name

onigiri
08-19-2006, 12:29 AM
Yep, it is.

EhraniNavy
08-19-2006, 05:03 AM
I tried to run a login script on my home machine (WinXP Pro - MySQL 5.0) and I wasn't able to connect to the server on my computer (running a login script as well - registered users weren't added to the table). However, when I uploaded the site to my host (godaddy) I was able to get it to work fine.

Are you trying this on your machine?

onigiri
08-19-2006, 05:20 AM
No, I'm running it on a remote host.

Phillip
08-19-2006, 04:51 PM
Hey the script looks to me in php5, are you sure you have php5 installed?
Also check there is no "active" collum the the database and it is not null on the user you've tried to login with!

Douglas
08-19-2006, 07:23 PM
Tell me one reason why you think that that is PHP 5 ?

onigiri
08-22-2006, 12:51 AM
I'm still not sure about the login script, but I found out that the query only inserts the name of the database instead of what I filled into the "name" field of the form when I try to insert the variable with the value of the field of the form. When I insert the value directly, though, it works.

For example, if $name is the variable that's holding the value of the name, and I run this query: INSERT INTO members (name, password) VALUES ('$name', '$password'), it will insert the name of the database ("Bob") into the name column instead of $name. When I run this query, however: INSERT INTO members (name, password) VALUES ('name', '$password'), then it will insert "name" without any problems. Any idea why?

Phillip
08-22-2006, 09:30 AM
Try this, it might not work but :)
<?php

//check for the required information
if(empty($_POST['name']) || empty($_POST['password'])) {
$variables = FALSE;
$display = "All fields are required!";
} else {
$username = $_POST['name'];
$password = $_POST['password'];
}

//connect
require("config.php");

//run a query to check if the user is a valid one
$verify = "SELECT member_name, member_password, member_rank FROM members WHERE member_name = '$name' AND member_password = '$password'";
$verify_res = mysql_query($verify) or die(mysql_error());

if(mysql_num_rows($verify_res) == 0) {
//no match
$display = "No member found with that data. Please try again.";
} else {
$member_info = mysql_fetch_array($verify_res);
$member_name = $member_info['member_name'];
$member_password = $member_info['member_password'];
$member_rank = $member_info['member_rank'];

//set a cookie
setcookie ("member",$member_name,time()+1957240,"/");
setcookie ("rank",$member_rank,time()+1957240,"/");

//start a session if cookies are blocked
session_start();

$_SESSION['name'] = $member_name;
$_SESSION['rank'] = $member_rank;

//update the login
$date = date("l, F j, Y \a\t g:i A");
$update_login = "UPDATE members SET login = '$date' WHERE member_name = '$member_name' AND member_password = '$member_password'";
$update_login_res = mysql_query($update_login) or die(mysql_error());

//if all worked out, redirect the user
if($update_login_res) {
header("Location: index.php");
}
}

?>

Douglas
08-22-2006, 01:57 PM
You are actually trying to insert $name into the database instead of the value of $name? Well just do this:

(name, blah) VALUES('\$name', '$blah)

onigiri
08-22-2006, 10:57 PM
You are actually trying to insert $name into the database instead of the value of $name? Well just do this:

(name, blah) VALUES('\$name', '$blah)
No, I meant I want to insert the value of $name, but the script keeps inserting the name of the database I'm using instead. Sorry for any inconvenience my inability to articulate myself may have caused. :blush:

And thanks Phil, I'll try your solution.