0

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>

Edited by Alex12345: n/a

2
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by diafol
0
<?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.

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.