How to create Login and Signup System in PHP
Received many request from PHP web developer for very basic article to create login and signup page in php. This article is totally for PHP developers who is very new in programming. In this tutorial I have created 2 forms for login and signup with code download and a demo.
Database Details:
database name => phpgang
table name => users
db.sql
Database file run in your MySQL to create database and add data in table.
CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(240) NOT NULL, `email` varchar(240) NOT NULL, `password` varchar(240) NOT NULL, `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
db.php
Edit this file as per your database credentials.
<?php $connection = mysqli_connect('db_host','db_user','db_password','db_name') or die(mysqli_error($connection)); ?>
This time I have used mysqli_connect as mysql_connect is deprecated in PHP 5.5 so web developers start shifting your apps to mysqli.
Now come to the main index file in this index file i have done all work like forms and php code to signup and login users.
Note: MD5 is no more secure passwords so please use this class for passwords: How to Hashing Password in PHP 5.5 with Password Hashing API
<?php include('db.php'); if(isset($_POST['action'])) { if($_POST['action']=="login") { $email = mysqli_real_escape_string($connection,$_POST['email']); $password = mysqli_real_escape_string($connection,$_POST['password']); $strSQL = mysqli_query($connection,"select name from users where email='".$email."' and password='".md5($password)."'"); $Results = mysqli_fetch_array($strSQL); if(count($Results)>=1) { $message = $Results['name']." Login Sucessfully!!"; } else { $message = "Invalid email or password!!"; } } elseif($_POST['action']=="signup") { $name = mysqli_real_escape_string($connection,$_POST['name']); $email = mysqli_real_escape_string($connection,$_POST['email']); $password = mysqli_real_escape_string($connection,$_POST['password']); $query = "SELECT email FROM users where email='".$email."'"; $result = mysqli_query($connection,$query); $numResults = mysqli_num_rows($result); if (!filter_var($email, FILTER_VALIDATE_EMAIL)) // Validate email address { $message = "Invalid email address please type a valid email!!"; } elseif($numResults>=1) { $message = $email." Email already exist!!"; } else { mysql_query("insert into users(name,email,password) values('".$name."','".$email."','".md5($password)."')"); $message = "Signup Sucessfully!!"; } } } ?> <!-- Login and Signup forms --> <div id="tabs"> <ul> <li><a href="#tabs-1">Login</a></li> <li><a href="#tabs-2" class="active">Signup</a></li> </ul> <div id="tabs-1"> <form action="" method="post"> <p><input id="email" name="email" type="text" placeholder="Email"></p> <p><input id="password" name="password" type="password" placeholder="Password"> <input name="action" type="hidden" value="login" /></p> <p><input type="submit" value="Login" /></p> </form> </div> <div id="tabs-2"> <form action="" method="post"> <p><input id="name" name="name" type="text" placeholder="Name"></p> <p><input id="email" name="email" type="text" placeholder="Email"></p> <p><input id="password" name="password" type="password" placeholder="Password"> <input name="action" type="hidden" value="signup" /></p> <p><input type="submit" value="Signup" /></p> </form> </div> </div>
In signup process we are checking email validation (!filter_var($email, FILTER_VALIDATE_EMAIL)) and duplicate email no more than one account with single email and encrypt your password with md5().
In demo design i have used jQuery UI tabs and create some css effect on input boxes.
CSS to show input boxes effects:
<style type="text/css"> input[type=text] { border: 1px solid #ccc; border-radius: 3px; box-shadow: inset 0 1px 2px rgba(0,0,0,0.1); width:200px; min-height: 28px; padding: 4px 20px 4px 8px; font-size: 12px; -moz-transition: all .2s linear; -webkit-transition: all .2s linear; transition: all .2s linear; } input[type=text]:focus { width: 400px; border-color: #51a7e8; box-shadow: inset 0 1px 2px rgba(0,0,0,0.1),0 0 5px rgba(81,167,232,0.5); outline: none; } </script>
If you have any problem regarding this tutorial configuration please feel free to comment we love to answer your queries.
Tutorial Categories:
nice but little bit lengthy..
can you show password recovery(forgot password) function ? via email i think.
thanks in advance !
forgot password function will ba very helpful ! Regards !
“Remember me” checkbox will be great too !
Agree with Anthony -(remember me) function will be awesome!
Why we useWhy we USe this mysqli_real_escape_string
What is Purpose o fthese Function
To protect you from attacker that using sql injection to hack your website
this is basic form for developer ! … http://www.tutorialsscripts.com/free-php-scripts/string-script/convert-ascii-to-character.php
can u pls send me the code?
i couldnt download the code although already subscribed
save the webpage dear.
not able to insert record in database using sign up form.
Some mistakes I’ve found and respective corrections…
1. Missing semicolon after CURRENT_TIMESTAMP.
2. Change line 38 to mysqli and don’t forget to add the $connection parameter.
3. Add echo $message on line 41 to see the errors.
nice article, i have done this. 🙂 ty for sharing
I’m working with bootstrap framework …
$message not working….
Where can I find the member’s area after login tutorial?
i am getting error .
i am using mysql database , should i use mysql instead of mysqli in code of php.
Line 38 change to.
mysqli_query($connection,”insert into users(name,email,password) values(‘”.$name.”‘,'”.$email.”‘,'”.md5($password).”‘)”);
how should i name the database db.sql . and what should be the name of css file
mysqli_real_escape_string() expects parameter 1 to be mysqli
help u.u
having problem with the layout.css is not working for me
do i have make 1 file or 3 files for this coding. thanks
hi i am creating online we store and i want make signup and login form. i dont know php please can some one write full coding for me please i need to make login and signup form on my web store my database name is “phpgang” and table name is “users”………please some one help me thanks a lot in advance
were you able to create the page
Deprecated: mysql_query(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:wampwwwphpgangindex.php on line 38
while login it shows only the message . i need a secure login process for a page where only a members can access the page?
still: Deprecated: mysql_query(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:wampwwwphpgangindex.php on line 38
How can I make a login form that redirects each user to its OWN page so that only that user sees that page?
Thanks in advice!
Great website great article many thanks 🙂 Sir
New to this php, learning … I tried using this code, but it does not insert any record to the table.
Can you help me.
You must got some error try to debug and send error.
do i have to link this index file with any html file to see those forms in google chrome. because when i opened index.php in chrome it showed me source code not the forms to login and signup.rep asap
The files in download are correct but the code written above has errors.
Lot of unclear points…….
1.What is the name of main php page ?
2.What is the name of .css file ?
3.Where to put the css code ?
Error correction:
There should be a comma after
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
like
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
There is no “forget password” option in the downloaded code too.
@huzoorbux :disqus do i have to link this index file with any html file to see those forms in google chrome. because when i opened index.php in chrome it showed me source code not the forms to login and signup.rep asap
did you run it on server? wamp or any server?
Hello @disqus_e9dTrguKPm:disqus You have to run a web server (e.g. Apache) and browse to your localhost. some helpful links below.
https://www.phpgang.com/how-to-install-php-environment-for-windows-linux-and-mac-os_809.html
http://stackoverflow.com/questions/8580273/how-to-run-php-files-on-my-computer
please i have downloaded the code, am running apache server but still get this error, Need some help
Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:xampphtdocslogin-signup-in-phpdb.php on line 2
Warning: mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:xampphtdocslogin-signup-in-phpdb.php on line 2
Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in C:xampphtdocslogin-signup-in-phpdb.php on line 2
thank you for helping.having a project i need to fix
for issue no 1 and 2 update db.php according to your apache setup
I had the same problem and went around it by pasting the table rules into phpmyadmin createtable screen (after I had created the database: db.mysql)
#1064 – You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near
‘(`id`)
) ENGINE=MyISAM’ at line 7 how to fix
On which page you are getting this error and which version of mysql you are using?
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(`id`)
He missed comma.
CURRENT_TIMESTAMP,
Thanks for the article, I am having an issue with line 98 ‘.$message.’. This is my error:
Notice: Undefined variable: message in C:wampwwwPUNISHMENTindex.php on line 98
check it with isset($message);
Thank you for this post, very helpful.
using pdo registration + sign up source code????
what is the purpose of using real escape string function ??
Escapes special characters in a string for use in an SQL statement, taking into account the current charset of the connection.
Escapes special characters in a string for use in an SQL statement, taking into account the current charset of the connection.
Escapes special characters in a string for use in an SQL statement, taking into account the current charset of the connection.
where should i include this css script??? plz post the answer immediatly
css Does not work…
change at the end to
For Passwords please use belo tutorial: https://www.phpgang.com/how-to-hashing-password-in-php-5-5-with-password-hashing-api_458.html
sir it always shows invalid password.
Please try to debugg script its working fine for me.
what is the use of $message ?
I didnt understand why u used it?