PHP Login Error
Posted: Fri Jan 21, 2011 3:13 am
hello, i am a young web site designer of only 16.
i am stuck with this code for my site and i need to be able to get users to log in to my website this is becasue i want them to be able to access features that unregistered users wouldn't usually be to get to...
i have problems with the login code though, any one help?
i am stuck with this code for my site and i need to be able to get users to log in to my website this is becasue i want them to be able to access features that unregistered users wouldn't usually be to get to...
i have problems with the login code though, any one help?
Code: Select all
[syntax=php]
<?php $username = $_POST['username'];
$password = $_POST['password'];
$self = $_SERVER['PHP_SELF'];
$referer = $_SERVER['HTTP_REFERER'];
#if either form field is empty return to the log-in page
if((!$username) or (!$password))
{ header("Location:$referer"); exit(); }
#connect to MySQL
$conn = @mysql_connect("localhost", "web113-daniel", "danieltest")
or die("Could not connect");
#select the specicfied database
$rs = @mysq;_select_db("web113-daniel", $conn)
or die("Could not select Database");
#create the sql query
$sql="select * from users where user_name=\"$username\"
and password = password(\"$password\")";
#execute the query
$rs = mysql_query($sql)
or die("Could not execute query");
#get number of rows that match username and password
$num = mysql_num_rows($rs);
#if there is a match the log-in is authenticated
if($num!=0)
{$msg = "Welcome $username, Your Log In has Succeeded!";}
else #or return to log-in page
{ header("Location:$referer"); exit();}
?>
[/syntax]