PaLZuM

2 October 2007

Dynamic login system - PHP & MySQL

Filed under: Umum

Hello,

In the about page on my blog is say’s I am a PHP coder… I am going to write a tutorial on how to make a PHP & MySQL login system which can come with anything from two to five hundred users. Here goes…

1) To start off we will have to make ourselves a MySQL database… in this example we shall use the name ‘loginsystem’. We will then run this code which creates a database with the following fields: user id(uid), firstname and lastname, email, password and username. Please note: The ‘uid’ field should be the primary key and should be made a auto_increment as in the example below. The MySQL code below will create this database if run in a MySQL client e.g. phpmyadmin:

CREATE TABLE `users` (

`uid` TINYINT( 4 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`username` VARCHAR( 225 ) NOT NULL ,
`password` VARCHAR( 225 ) NOT NULL ,
`email` VARCHAR( 225 ) NOT NULL ,
`firstname` VARCHAR( 225 ) NOT NULL ,
`lastname` VARCHAR( 225 ) NOT NULL

) ENGINE = innodb;

2) Now we will go onto adding some test data. If you were able to run the query above, you should be able to run the code below to insert some test data, else just manually create some… It doesn’t matter what it is. Please note: You may have to use a tool like http://www.iwebtool.com/md5 becuase the passwords need to be entered using MD5 for this example.

INSERT INTO `users` ( `uid` , `username` , `password` , `email` , `firstname` , `lastname` )
VALUES (

NULL , ‘johnd’, ‘098f6bcd4621d373cade4e832627b4f6098f6bcd4621d373cade4e832627b4f6′, ‘johnd@example.com’, ‘John’, ‘Doe’

), (

NULL , ‘foobar’, ‘098f6bcd4621d373cade4e832627b4f6′, ‘foo@example.com’, ‘foo’, ‘bar’

);

3) This now brings us onto the next thing… connecting to the mysql database. If you were creating a large application you would often use a framework which can process queries etc. in your own way, however we will just create a config.php file. Anything after ‘//’ will be a comment, you should usually put them above what you are about to do however I will just be doing it to save space:

  1. $db[‘host’] = ‘localhost’; // 99% of time leave this as is
  2. $db[‘user’] = ‘root’; ‘Your MySQL user
  3. $db[’pass‘] = ‘‘; // Your MySQL DB pass
  4. $db[’name‘] = ‘loginsystem‘; // Name of your db
  5. // Now we shall connect:
  6. mysql_connect($db[’host‘], $db[’user‘], $db[’pass‘])or die(’Could not connect because‘.mysql_error());
  7. mysql_select_db($db[’name‘]) or die(mysql_error());
  8. ?>

4) Save the above as config.php. As you can probably see by my commenting, It just connects to the database. Now we have our database, test data and connection… We can proceed onto the login. This will consist of a small form with two fields and a submit button and a piece of PHP which checks for empty fields and will check again the DB the input information. The code is below and is commented:

  1. // require the config.php:
  2. require_once(‘config.php’);
  3. // Check form submit:
  4. if($_POST[’submit’]) {
  5. // form submit, check fields
  6. if(empty($_POST[‘username’]) || empty($_POST[‘password’])) {
  7. // not empty, create into vars and strip their tags
  8. $username = htmlentities($_POST[‘username’]);
  9. $password = md5($_POST[‘password’]);
  10. // Do a query to check them
  11. $query = mysql_query("SELECT * FROM `users` WHERE `username` = ‘$username’ AND `password` = ‘$password’");
  12. // check there is a row
  13. if(mysql_num_rows($query) == 1) {
  14. // user valid, login
  15. echo ‘Logged in’; // You would usually create a session or cookie, however we won’t.
  16. } else {
  17. // Username of pass incorrect
  18. echo ‘Username or Password incorrect.’;
  19. }
  20. } else {
  21. // empty, echo error
  22. echo ‘Please fill in all fields.’;
  23. }
  24. }
  25. ?>
  26. $_SERVER[‘PHP_SELF’] –>
  27. "login" action="" method="post">
  28. Username: "text" name="username" maxlength="225" />
  29. Password: "password" name="password" maxlength="225" />
  30. "submit" name="submit" value="Submit" />
  31.  

5) Basically, the above code takes the user input, cleans it up after it’s check the fields aren’t empty and check it against the database. The users will now be able to login using their username and passwords and a greeting message will be issued to them!!

Thanks for reading and if you have any problems, Feel free to leave a comment or email me using the contact page and I will get back to you as soon as possible.






















Get free blog up and running in minutes with Blogsome
Theme designed by Hadley Wickham