Jump to content

PHP/MySQL Login System Problem


wowcrofty

Recommended Posts

Hey guys, I have a question about a few blocks of code that have been stumping me for a while.  I am attempting to create a PHP/MySQL based login system for a website and, although I can successfully create the user and it appears in the database table, I cannot seem to find my error in the PHP code for my login.php file.  I am obtaining the apparently ever popular T_VARIABLE error.  I am very new to both PHP and MySQL and I only learned how to do this much by reading an online tutorial.  My code for all my pages is included below for reference.

 

REGISTER.PHP - THIS WORKS

<?

$username="xxxx";

$password="xxxx";

$database="xxxx";

$host="xxxx";

 

$user=$_POST['user'];

$pass=$_POST['pass'];

$firstname=$_POST ['firstname'];

$lastname=$_POST ['lastname'];

$email=$_POST ['email'];

 

@mysql_connect($host,$username,$password);

@mysql_select_db($database) or die("Unable to select database");

 

$query = "INSERT into users VALUES ('','$user','$pass','$firstname','$lastname','$email')";

mysql_query($query);

 

mysql_close();

?>

 

Here is the corresponding HTML file (user_registration.html) - AGAIN NO PROBLEM WITH THIS

<html xmlns="http://www.w3.org/1999/xhtml">

 

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled 1</title>

</head>

 

<body>

<form action="register.php" method="post">

Desired User Name    <input type="text" name="user">

<br />

Desired Password     <input type="text" name="pass">

<br />

<br />

First Name                <input type="text" name="firstname" size="30">

<br />

Last Name               

<input type="text" name="lastname"

<br style="width: 187px" />

<br />

Email                       

<input type="text" name="email"

 

<br /> <br />

<br />

<input type="submit" value="Register" name="submit">

 

</form>

</body>

</html>

 

LOGIN.PHP - THIS IS WHERE THE PROBLEM LIES

<?

$username="xxxx";

$password="xxxx";

$database="xxxx";

$host="xxxx";

 

$user=$_POST['user'];

$pass=$_POST['pass'];

 

@mysql_connect($host,$username,$password);

@mysql_select_db($database) or die("Unable to select database");

 

$sql="SELECT username, password FROM users WHERE username='"$user"' and password='"$pass"';

$r=mysql_query($sql);

if(!$r){

$err=mysql_error();

print $err;

close();

}

 

ITS ASSOCIATED HTML FILE (user_login.html)

<head>

<meta http-equiv="Content-Language" content="en-us" />

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled 1</title>

</head>

 

<body>

 

<form method="post" action="login.php">

User Name    

<input name="user" type="text" style="width: 150px" /><br />

Password     <input name="pass" type="password" /><br />

<br />

<input name="submit" type="submit" value="Submit" /></form>

 

</body>

 

</html>

 

 

What am I doing wrong?  Thank you in advance for all your feedback!

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/197601-phpmysql-login-system-problem/
Share on other sites

$sql="SELECT username, password FROM users WHERE username='"$user"' and password='"$pass"';

 

Sould be....

 

$sql="SELECT username, `password` FROM users WHERE username='$user' and `password`='$pass'";

 

or....

 

$sql="SELECT username, `password` FROM users WHERE username='" . $user . "' and `password`='" . $pass . "'";

Thank you very much!  That worked perfectly.

 

Would you also be able to give me a quick run down on how I can use the logged in user to access custom pages?  Again, I am very new when it comes to this, so anything you can offer would be greatly appreciated.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.