Page 1 of 1

PHP Login Error

Posted: Fri Jan 21, 2011 3:13 am
by yuoi18
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?

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]

Re: PHP Login Error

Posted: Fri Jan 21, 2011 4:01 am
by Peter Kelly
Starters you need to put the code in PHP code tags, and also you need to tell us what error your having?

Re: PHP Login Error

Posted: Fri Jan 21, 2011 9:43 am
by brothaofdes
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)?

Code: Select all

$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.

Please post more information so that we can help.

Re: PHP Login Error

Posted: Fri Jan 21, 2011 10:09 am
by John Cartwright
Please make sure to not post username/passwords in your code. This is obviously test data, but it is our policy have our users not include it at all.