Creating user login system is one of the most interesting thing for developers. If you want to create a web application with user sign up and sign in system or even if you want to create a secure admin area this method can help you a lot.
Today we are going to create a simple application with php that can allow a user to sing up, sign in and viewing secure page.
Live Demo Download SourceStep 1 - Database
In this tutorial we are going to use MySQL database based user login system.
Users table
At first we need to create users database table. This table has two fields (username & password) with an auto increment filed (id).
CREATE TABLE `users` ( `id` int(6) NOT NULL AUTO_INCREMENT, `username` varchar(16) NOT NULL, `password` varchar(40) NOT NULL, PRIMARY KEY (`id`) );
Database connection
In this tutorial we are going to use PDO (PHP Data Object) to do database queries. If you are not familiar with PDO I recommend you to read this article at first.
function dbconnect() { global $pdo; try { $pdo = new PDO('mysql:host=localhost;dbname=mydb', 'root', 'root'); } catch (PDOException $e) { die('MySQL connection fail! ' . $e->getMessage()); } }
The dbconnect function will establish database connect. In here you will need to change host name, dbname, username and password.
Step 2 - User Signup
We will allow new users to signup in this tutorial. In this section will will create a user signup system.
Signup HTML
To create a user signup system at first we will need to create a signup form. The following html code is placed in signup.php file.
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Sign Up</title> </head> <body> <h1>Sign Up</h1> <p>Already have an account? <a href="signin.php">Login</a></p> <!-- error or success message will be shown here --> <?php if (!empty($_GET['error'])): ?> <p><?php echo $_GET['error']?></p> <?php endif ?> <?php if (!empty($_GET['success'])): ?> <p><?php echo $_GET['success']?></p> <?php endif ?> <!-- signup form --> <form action="signup_post.php" method="post"> <input type="text" name="username" placeholder="username"> <input type="password" name="password" placeholder="password"> <input type="submit" value="Sign Up"> </form> </body> </html>
Insert new user information
The insert_new_user function inserts new user's data to the database.
function insert_new_user($username, $password) { # checking username is already taken if (username_exists($username)) return false; # insert new user info global $pdo; $stmt = $pdo->prepare(' INSERT INTO users (username, password) values (:username, :password)'); $stmt->execute( array(':username' => $username, ':password' => md5($password)) ); if ($pdo->lastInsertId()) return true; else return false; }
Step 3 - Sign in
Now we are going to create user sign in system. This system is based on PHP session function.
Sign in HTML
The sign in form is almost same as sing up form and it is placed in singin.php file.
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Sign In</title> </head> <body> <h1>Sign In</h1> <!-- error or success message will appear here --> <?php if (!empty($_GET['error'])): ?> <p><?php echo $_GET['error']?></p> <?php endif ?> <!-- sign in form --> <form action="sign_post.php" method="post"> <input type="text" name="username" placeholder="username"> <input type="password" name="password" placeholder="password"> <input type="submit" value="Sign In"> </form> </body> </html>
Sign in attempt function
The attempt function attempts to sign in users. If a user requests to sign in by his username & password it looks into users table on database and if it founds everything ok it sets username to the session variable and returns true.
function attempt($username, $password) { global $pdo; $stmt = $pdo->prepare(' SELECT id, username FROM users WHERE username = :username AND password = :password LIMIT 1'); $stmt->execute(array(':username' => $username, 'password' => md5($password))); if ($data = $stmt->fetch( PDO::FETCH_OBJ )) { # set session $_SESSION['username'] = $data->username; return true; } else { return false; } }
thanks this is a great easy to understand tutorial
ReplyDelete1- Don't use global
ReplyDelete2 - MD5 is a weak hashing algorithm. You should be using crypt()
3 - use html5 attribute 'require' to force/validate user to enter only email, pass, url types
4 - use PDO with proper emulation, and error mode exception..
I really do agree on this never use global.
ReplyDeleteMD5 is a weak hash algorithm but accepted by large, but crypt really should be used instead.
yes you could use the html5 attribute require but we still struggle with the the end client to have a up to date browser.
Dear Resalat Haque
ReplyDeletei need some help i n this tutorial can u please help me
Thank you very much for great support.
ReplyDeletedịch vụ làm báo cáo tài chính tại quận cầu giấy
ReplyDeletedịch vụ làm báo cáo tài chính tại quận hai bà trưng
dịch vụ làm báo cáo tài chính tại quận ba đình
dịch vụ làm báo cáo tài chính tại thanh trì
dịch vụ làm báo cáo tài chính tại quận hoàng mai
dịch vụ làm báo cáo tài chính tại quận tây hồ
dịch vụ làm báo cáo tài chính tại quận đống đa
công ty dịch vụ kế toán tại vĩnh phúc
công ty dịch vụ kế toán tại hưng yên
công ty dịch vụ kế toán tại phú thọ
công ty dịch vụ kế toán tại hải dương
công ty dịch vụ kế toán tại hải phòng
công ty dịch vụ kế toán tại bắc ninh
dịch vụ kế toán thuế tại vĩnh phúc
dịch vụ kế toán thuế tại hưng yên
dịch vụ kế toán thuế tại hải dương
dịch vụ kế toán thuế tại ninh bình
dịch vụ kế toán thuế tại vĩnh phúc
dịch vụ kế toán thuế tại hưng yên
dịch vụ kế toán thuế tại phú thọ
dịch vụ kế toán thuế tại thái bình
dịch vụ kế toán thuế tại hải dương
dịch vụ kế toán tại hải phòng
máy chạy bộ điện bàn bóng bàn
ReplyDeleteAce Maxs Asli Di Apotik K24 - Selain di apotik k24 juga ace maxs asli bisa anda dapatkan disini
ReplyDeleteI have read your blog its very attractive and impressive. I like it your blog.
ReplyDeletePHP Training in chennai | PHP Training Course
PHP Training in chennai | Online PHP Course
Tiếng anh cho người đi làm
ReplyDeleteTiếng anh cấp tốc
Luyện thi toeic tại hà nội
Tiếng anh cho người lớn tuổi
Tiếng anh cho người mới bắt đầu
Học tiếng anh tại hà nội
dạy tiếng anh cho doanh nghiệp
GSSSB Recruitment 2018
ReplyDeleteCentral Railway Recrutiment 2018
OSSSC Recruitment 2018
NCR Recruitment 2018
RRC NWR Recruitment 2018
JKPSC Recruitment 2018
MP Vyapam Recruitment 2018
Kolkata Police Recruitment 2018
UPPSC Recruitment 2018
Rajasthan Police Recruitment
Thanks for the post visit
ReplyDeleteAOC recruitment 2018
Very good informative article. Thanks for sharing such nice article, keep on up dating such good articles.
ReplyDeleteNO.1 AQM Services | Application Quality Managment Services | Austere Technologies
wow...nice blog, very help full information. Thanks for sharing.
ReplyDeleteNO.1 API DEVELOPMENT SERVICES | MASSIL TECHNOLOGIES
ReplyDeleteمكافحة حشرات بالخبر افضل شركة مكافحة حشرات بالخبر
مكافحة حشرات بمكة افضل شركة رش حشرات بمكة
مكافحة حشرات بالمدينة المنورة افضل شركة رش حشرات بالمدينة المنورة
مكافحة حشرات بالدمام افضل شركة مكافحة حشرات بالدمام