PHP Login Error

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!

Moderator: General Moderators

Post Reply
yuoi18
Forum Newbie
Posts: 1
Joined: Fri Jan 21, 2011 3:05 am

PHP Login Error

Post 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]
Peter Kelly
Forum Contributor
Posts: 143
Joined: Fri Jan 14, 2011 5:33 pm
Location: England
Contact:

Re: PHP Login Error

Post 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?
brothaofdes
Forum Newbie
Posts: 21
Joined: Thu Jan 20, 2011 10:05 am

Re: PHP Login Error

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: PHP Login Error

Post 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.
Post Reply