Help needed with login script..
Date: 02/03/06
(PHP Community) Keywords: php, mysql, html, database, sql
I have the following login script when it logs in it just goes straight to back to the login page. Now I've worked out it's something to do with the else or if ( $num != 0) statement as when commented out it goes thru ish but it doesn't print the $msg variable which I have an echo statement calling on it in the html body..The session start/auth isn't being worked on atm hence why it's been commented out.
#set variables
$usr = $_POST['username'];
$pass = $_POST['password'];
$self = $_SERVER['PHP_SELF'];
/*#Start session and auth it
if (isset( $usr ) and isset( $pass ) )
{
session_start();
$_SESSION['authorized'] = 'ok';
$url = (isset($_GET['url'])) ? $_GET['url'] : 'index.php';
header("Location: $url");
}*/
#check form not empty
if ( (!$usr) or (!$pass) )
{
header ("Location:{$_SERVER['HTTP_REFERER']}");
exit();
}
#connect to sql db
$conn = @mysql_connect( "localhost", "xxx", "xxx" ) or die( "Could not connect to SQL" );
$rs = @mysql_select_db ( "xxx", $conn ) or die ( "Could not select database" );
#select db
$sql = 'select * from users where \'user_name\'=\'$username\' and \'password\'= password(\'$password\')';
#execute the query
$rs = mysql_query($sql, $conn) or die ( 'Could not execute query' );
#get no. of matching rows
$num = mysql_num_rows ( $rs );
#check for match
if ($num != 0 )
{
$msg = 'Welcome $usr - you are now logged in';
}
else
{
header ("Location:{$_SERVER['HTTP_REFERER']}");
exit ();
}
Source: http://community.livejournal.com/php/406868.html