12 Replies - 936 Views - Last Post: 02 December 2011 - 10:12 AM Rate Topic: -----

#1 Boyan  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 254
  • Joined: 09-September 07

Login Script Not Working

Posted 02 December 2011 - 06:07 AM

Hello everyone!

I got my registration script working smooth with no problems, but I have some trouble logging in. The username and password are already in the database, but when I try to log-in, all I got is "Username and password are incorrect".

I couldn't locate the problem, I don't get any errors, so I thought you guys could help me out. Thank you.

Here's my code (it's a simple script with cookies, just to see if it works):

<?php

// connection with the database

$con=mysql_connect ("localhost", "root", "password");
mysql_select_db ("thedatabase");

// Storing the data in variables from the form input

$username = $_POST['username'];
$password = $_POST['password'];

// preventing SQL injection

$user = mysql_real_escape_string($username);
$pass = mysql_real_escape_string($password);

$query = mysql_query ("SELECT * FROM users WHERE username='$username' AND password='$password'  ");

$count = mysql_num_rows($query);

if ($count == 1)

// checks if the username/password are in the same row

	{ 
		echo "Login Succesful";
		$hour = time() + 3600;
		
		// $hour sets the cookie storage time for 1 hour
		
		setcookie("username", $username, $hour);
		setcookie("password", $password, $hour);
		
		header ("location: dashboard.html");
		
	}

		else 
		{	
			echo "Username and password are incorrect";
		}



?>



Is This A Good Question/Topic? 0
  • +

Replies To: Login Script Not Working

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3560
  • View blog
  • Posts: 10,356
  • Joined: 08-June 10

Re: Login Script Not Working

Posted 02 December 2011 - 06:25 AM

OK, essentially this means that the count is not 1. echo out $count to see what it really is. if it is 0, check if username and password match to the given ones in the DB (e.g. by running the query in a SQL client like phpMyAdmin)

second. the query could use improvement. since you’re only interested in the count, request it:
SELECT COUNT(*) FROM users WHERE username='$username' AND password='$password'

see also this login tutorial.

and one more. mysql_* functions are outdated. use the more modern PDO or MySQLi instead.
Was This Post Helpful? 2
  • +
  • -

#3 Boyan  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 254
  • Joined: 09-September 07

Re: Login Script Not Working

Posted 02 December 2011 - 06:33 AM

View PostDormilich, on 02 December 2011 - 06:25 AM, said:

second. the query could use improvement. since you’re only interested in the count, request it:
SELECT COUNT(*) FROM users WHERE username='$username' AND password='$password'


First of all, thank you Dormilich. I've changed the query and now I got this:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in path\login.php on line 20


The tutorial seems nice, but I want to learn the basics first, then switch to PDO. :)
Was This Post Helpful? 0
  • +
  • -

#4 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 948
  • View blog
  • Posts: 2,357
  • Joined: 15-February 11

Re: Login Script Not Working

Posted 02 December 2011 - 06:44 AM

Your query is failing. $user and $pass are the sanitized values yet you're using $username and $password in your query. You should also check to see if the values aren't empty. You have the option of using the functions isset or empty.
Was This Post Helpful? 1
  • +
  • -

#5 Boyan  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 254
  • Joined: 09-September 07

Re: Login Script Not Working

Posted 02 December 2011 - 06:53 AM

Thank you codeprada.

I changed them in the query, $user and $pass and still doesn't working.

I even removed this part here. It seems the query itself is not okay, and still trying to figure it out. :S

		// $hour sets the cookie storage time for 1 hour
		
		setcookie("username", $username, $hour);
		setcookie("password", $passname, $hour);
		
		header ("location: http://localhost/eGalerija/dashboard.html");


Was This Post Helpful? 0
  • +
  • -

#6 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 948
  • View blog
  • Posts: 2,357
  • Joined: 15-February 11

Re: Login Script Not Working

Posted 02 December 2011 - 06:57 AM

The best thing you could do is output the error from MySQL.
if($query == false)
    echo mysql_error();
else
{
    //proceed with normal execution
}


Was This Post Helpful? 1
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6081
  • View blog
  • Posts: 23,557
  • Joined: 23-August 08

Re: Login Script Not Working

Posted 02 December 2011 - 07:08 AM

Even better! Basic debugging!

$user = mysql_real_escape_string($username);
$pass = mysql_real_escape_string($password);
$sql = "SELECT * FROM users WHERE username='$user' AND password='$pass'";
$query = mysql_query ($sql);
if (!$query) {
    die("Query $sql failed with error " . mysql_error());
}



That way you can see what it is you're actually providing as a query.

And OF COURSE you're storing your password in the database in cleartext. Sigh...I want to kill every login tutorial writer that teaches this.
Was This Post Helpful? 3
  • +
  • -

#8 Boyan  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 254
  • Joined: 09-September 07

Re: Login Script Not Working

Posted 02 December 2011 - 07:20 AM

Thank you, guys! Will try now.

@JackOfAllTrades, the password isn't stored in cleartext, it's hashed in the registration script.

EDIT: There's no SQL error. :S I'm still getting "Username and password are incorrect".

This post has been edited by Boyan: 02 December 2011 - 07:25 AM

Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6081
  • View blog
  • Posts: 23,557
  • Joined: 23-August 08

Re: Login Script Not Working

Posted 02 December 2011 - 07:40 AM

Well, you need to understand how hashing works, because that's the reason it's not working.

You're comparing the raw password typed in by the user with the hashed password saved in the database! It's never going to succeed. You need to do the same thing to the raw password here that was done prior to saving in the database, and THEN use THAT in the query as the password!
Was This Post Helpful? 2
  • +
  • -

#10 Boyan  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 254
  • Joined: 09-September 07

Re: Login Script Not Working

Posted 02 December 2011 - 07:59 AM

Oh, now it's clearer. :) Thank you, thank you very much.
Was This Post Helpful? 0
  • +
  • -

#11 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 927
  • View blog
  • Posts: 3,209
  • Joined: 19-January 10

Re: Login Script Not Working

Posted 02 December 2011 - 09:40 AM

You should also hash your passwords.
Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6081
  • View blog
  • Posts: 23,557
  • Joined: 23-August 08

Re: Login Script Not Working

Posted 02 December 2011 - 10:08 AM

Well apparently he is, he's just not doing it here in the login process, hence the problem.
Was This Post Helpful? 0
  • +
  • -

#13 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 927
  • View blog
  • Posts: 3,209
  • Joined: 19-January 10

Re: Login Script Not Working

Posted 02 December 2011 - 10:12 AM

Ah, I didn't notice.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1