PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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?
[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]
There is nothing obviously wrong with the code (from a quick scan). A quick question though, is this something you downloaded and want to get to work of is it your design? If you downloaded it, did you setup the database table correctly? Do you have a database (mysql)?
$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");
It looks like you are trying to connect to the "web113-daniel" localhost mysql server with username "web113-daniel" and password "danieltest". It is not common for the user-name to be the same as the database name. Check to see if this is correct.