wowcrofty Posted April 5, 2010 Share Posted April 5, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/197601-phpmysql-login-system-problem/ Share on other sites More sharing options...
trq Posted April 5, 2010 Share Posted April 5, 2010 $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 . "'"; Quote Link to comment https://forums.phpfreaks.com/topic/197601-phpmysql-login-system-problem/#findComment-1037031 Share on other sites More sharing options...
wowcrofty Posted April 5, 2010 Author Share Posted April 5, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/197601-phpmysql-login-system-problem/#findComment-1037032 Share on other sites More sharing options...
trq Posted April 5, 2010 Share Posted April 5, 2010 Once you have verified your user against your database you need to store a flag within the $_SESSION array. Something like.... session_start(); $_SESSION['logged'] = true; You can then check this flag on any page too see that a user is logged in. Quote Link to comment https://forums.phpfreaks.com/topic/197601-phpmysql-login-system-problem/#findComment-1037034 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.