KieranT
11-27-2008, 04:31 PM
PHP/SQL Login Script Help.
Hi,
I'm creating a simple login script using:
main_login.php
checklogin.php
login_success.php
main_login.php :
Code:
<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="account" type="text" id="account"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="password" type="text" id="password"></td>
</tr>
<tr>
<td></td>
<td></td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
checklogin.php
Code:
<?php
ob_start();
$host="localhost"; // Host name
$username="****"; // Mysql username
$password="****"; // Mysql password
$db_name="****"; // Database name
$tbl_name="membersb"; // Table name
// 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 $account and $password
$account=$_POST['Account'];
$password=$_POST['Password'];
// To protect MySQL injection (more detail about MySQL injection)
$account = stripslashes($account);
$password = stripslashes($password);
$account = mysql_real_escape_string($account);
$password = mysql_real_escape_string($password);
$sql="SELECT * FROM $tbl_name WHERE username='$account' and password='$password'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $account and $password, table row must be 1 row
if($count==1){
// Register $account, $password and redirect to file "login_success.php"
session_register("account");
session_register("password");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
ob_end_flush();
?>
login_success.php
Code:
<?
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>
<html>
<body>
Login Successful
</body>
</html>
Anyway my problem is that when I enter an account//password that's stored in the table I get this error:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/****/public_html/php/checklogin.php on line 27
Anybody have any ideas?
Kier
Hi,
I'm creating a simple login script using:
main_login.php
checklogin.php
login_success.php
main_login.php :
Code:
<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="account" type="text" id="account"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="password" type="text" id="password"></td>
</tr>
<tr>
<td></td>
<td></td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
checklogin.php
Code:
<?php
ob_start();
$host="localhost"; // Host name
$username="****"; // Mysql username
$password="****"; // Mysql password
$db_name="****"; // Database name
$tbl_name="membersb"; // Table name
// 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 $account and $password
$account=$_POST['Account'];
$password=$_POST['Password'];
// To protect MySQL injection (more detail about MySQL injection)
$account = stripslashes($account);
$password = stripslashes($password);
$account = mysql_real_escape_string($account);
$password = mysql_real_escape_string($password);
$sql="SELECT * FROM $tbl_name WHERE username='$account' and password='$password'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $account and $password, table row must be 1 row
if($count==1){
// Register $account, $password and redirect to file "login_success.php"
session_register("account");
session_register("password");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
ob_end_flush();
?>
login_success.php
Code:
<?
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>
<html>
<body>
Login Successful
</body>
</html>
Anyway my problem is that when I enter an account//password that's stored in the table I get this error:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/****/public_html/php/checklogin.php on line 27
Anybody have any ideas?
Kier
