The first version was not working.. I made this simpler one, but it is not working either. It says the username or password are wrong. I also verified that it counts 0 rows with if($count==1). I am using WAMP. Thanks so much for any help.

Oh and I got the script from here and changed it slightly: http://phpeasystep.com/phptu/6.html (in case you need to look at their code)

checklogin.php:

<?php
ob_start();
$host="localhost:3306"; 
$username="root"; 
$password="1234"; 
$db_name="youtube"; 
$tbl_name="user"; 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// Define $username and $mypassword 
$username=$_POST['username']; 
$password=$_POST['password'];

// To protect MySQL injection (more detail about MySQL injection)
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);

$sql="SELECT * FROM $tbl_name WHERE username='$username' and password='$password'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("username");
session_register("password"); 
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}

ob_end_flush();
?>

index.php:

<html>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="username" type="text" id="username"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="password" type="password" id="password"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
<td><a href='register.php'>Register?</a></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</html>
Member Avatar for diafol
<?php
session_start();
//...other code...
if(isset($_POST['username']) && isset($_POST['password']) && !empty($_POST['username']) && !empty($_POST['password'])){

	$user=mysql_real_escape_string(stripslashes($_POST['username'])); 
	$pass=mysql_real_escape_string(stripslashes($_POST['password']));
	 	 
	$result=mysql_query("SELECT password FROM $tbl_name WHERE username='$user' LIMIT 1");
	
	if(mysql_num_rows($result)>0){
		$d = mysql_fetch_array('result');
		if($pass == $d['password']){
			$_SESSION['username'] = $user;
			header("location:login_success.php");
			exit;	
		}else{
			echo "Wrong Username or Password";		
		}
	}else{
		echo "Count is 0";	
	}
}

Nothing is shouting out at me, but try this bit above. ALso check that you don't have more than one entry for a username.

Thanks, I scrapped it and got it to work

Member Avatar for diafol

Care to share? Anyway, mark as solved (link below).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.