How can I create a login

November 19, 2007 3:21am CST
Hi there, Is there anyone that can tell me or help me creating a login script with php and mysql? thank in advance
1 person likes this
3 responses
• India
20 Nov 07
I hope you got a basic idea on how to store, fetch data from a mysql table using PHP. Store the login details like username and password to the table. On login page check the value the user have entered and check if those values are there in the table (Both username and password.) The mysql query could be "SELECT count(*) FROM users WHERE username= $_POST[Username] AND password= $_POST[password]". This will return the number of records which satisfies the where condition. If the count is equal to 1 then the login details are correct and thus set a session variable, let us say $_SESSION['login']== true else set the session variable to false. Check this session variable (whether it is true or false. If true allow the user to view the page. If false or the varaible is not set then redirect to the login page) on all the other pages which require the user to be logged in to view the page. The above setup is a basic login page setup. In a real project you need to consider many security related things like sql injection, password encryption etc. Just to learn the logging process the above said details would be fine. For more details about session, mysql related function refer http://www.php.net. Hope that helps you.
@tails5 (115)
• Australia
20 Nov 07
I think you mean "SELECT count(*) FROM users WHERE username='$_POST['$Username'] AND password=$_POST['password']"
• India
20 Nov 07
Actually its been years since I really used any mysql related php functions while developing my web application. I usually employ php frameworks or PEAR. So to be honest I lost touch in using SQL statements directly in my application. The reason I used the above statement is that we are enclosing the sql statement in a quotes. In such a case if we add a single quotes like $_POST['username'] it will lead to a syntax error. I am not sure if I am correct or not. So try the above. If it doesnt work try the one you suggested. If noting works then let me know I will help you to solve the problem =)
@tails5 (115)
• Australia
20 Nov 07
Sorry for the double post, but I made a mistake in that SQL Query, it should have been "SELECT count(*) FROM users WHERE username='$_POST['Username']' AND password='$_POST['password']'"
@tails5 (115)
• Australia
19 Nov 07
I found this quite helpful: http://www.sitepoint.com/article/php-mysql-tutorial - How to build your own database driven website, helpful for learning about mysql and php. http://www.sitepoint.com/article/users-php-sessions-mysql - Managing Users with PHP Sessions and MySQL, has the actual code for it aswell Hope it helps
19 Nov 07
I will certainly have a look at it. Thanks Does anyone else have any ideas where I can get some more info?
• Italy
8 Apr 08
u can download a simple script from here http://php.html.it/script/vedi/1804/phpmylogin-v-15/ very easy to install. It create table in MySql where register all passwor.