Simple Php Signup via HTML form, registration script plus code.
By itech
This tutorial will help you in building your own working registration section on your site. First of all, Create a table with name "login" in database (That you've created for your site on the your host), then add three fields in it with names "id, username, password" and "id" should be set to integer and auto-increment mode (Primary Key) while creating. Now, We need to create two pages on our site,
- First for the HTML Form,
- And Second one for php script (PHP code) which will process the details submitted by user via HTML form to store it in database.
Let we start through registration form (You can add as many input fields according to need but do not forget to make necessary changes in php script on another page).
See the form code below:
<form method="post" action="link of the page having sign-up script"> <table border="0"> <tr><td>username: </td><td><input type="text" name="username" /></td></tr> <tr><td>Password: </td><td><input type="password" name="password" /></td></tr> <tr><td>Confirm Password: </td><td><input type="password" name="confirm_password" /></td></tr> <tr><td></td><td><input type="submit" value="submit" /></td></tr> </table> </form>
Links to Learn about Raw MySQL Queries
- SQL Tutorial: Raw Sql Queries to interact with MySql...
Here, You'll learn about Raw SQL Queries, Which can typed directly from command line/prompt in case of lacking MySQL GUI (Graphical User interface) such as PHPmyadmin. - Sql Tutorial (Structured Query Language): Four types...
Here, you'll learn about sql queries (Insert, Retrieve Update, Delete etc.) which are most important part of every websites that uses databases (ex. MySQL) on their site such as dynamic websites.
Now, We are going to see the script that will do all the processing and insertion work of the details submitted via registration form into the database. Note that we are first storing the values of form in php variables to check some requirements.
Notice:
- You should Create a separate page for this php script below and then change the action="" attribute in form to action="php script page link".
- You can also try your own ideas to add more juices in this code.
- I recommend You to use the encryption method MD5 to encrypt the password, so that password remain safe in database without getting known to anyone (means encrypted password will get store in database instead of that used during registration).
- If you are encrypting password here then you also need to change your 'log in' script so that user entered password via login form is first converted into encrypted password then checks the database for user-name and password match, see the 'log in' script written here by me.
See the code below:
<?php
/* This code will make a connection with database */
$con=mysql_connect("Hostname","username","password");
/* Now, we select the database */
mysql_select_db("Database Name");
/* Now we will store the values submitted by form in variable */
$username=$_POST['username'];
$pass=$_POST['password'];
/* we are now encrypting password while using md5() function */
$password=md5($pass);
$confirm_password=$_POST['confirm_password'];
/* Now we will check if username is already in use or not */
$queryuser=mysql_query("SELECT * FROM login WHERE username='$username' ");
$checkuser=mysql_num_rows($queryuser);
if($checkuser != 0)
{ echo "Sorry, ".$username." is already been taken."; }
else {
/* now we will check if password and confirm password matched */
if($pass != $confirm_password)
{ echo "Password and confirm password fields were not matched"; }
else {
/* Now we will write a query to insert user details into database */
$insert_user=mysql_query("INSERT INTO login (username, password) VALUES ('$username', '$password')");
if($insert_user)
{ echo "Registration Succesfull"; }
else
{ echo "error in registration".mysql_error(); }
/* closing the if else statements */
}}
mysql_close($con);
?>
Hope, you will find this code simple and easy to understand as well as executable on your site. If you find any difficulties you can ask me here at comments section or can contact via contact form on hubpages after joining here.
Please Vote your opinion about article?
See results without votingComments
also the my sql sysntax like using phpmy admin so that the beginners know about it
@ronjerson, Everything has been commented out in code for ease to understand.... if you find difficulty in particular section of code, you are free to ask here.
greetings and thanks for the tut :)
Im having trouple login in after signing up, the problem is in the mysql i believe. I find the directions there confusing :( can you post the raw mysql code ?
kind regards,
formanzone.
u r giving the basic code.. in this i want to only the string on the user name. how can i add this futures??
my name is dinesh kumar verma i am ready this work
Hi, thanks you for your codes, in the script, i'm not seeing the Table selection from the database, can you explain me ?
Hello Samba, mysql_query() function in PHP code above is doing the table selection work from database.... and If you Mean anything else...Pls clarify.
hi can you do me a favor ? how can i get the code for signup and sign in ..im so confuse about the codes !!can you plz help me ? tnx
@queen, I will definitely do favor... But I'm not clarified with your problem, Pls Explain.
Are you facing problems in understanding the code...
Its working ...How can i make the page even better ...
@pradeesh, You can use CSS (Cascading Style Sheets) for that task OR "style" attribute of any HTML tags.
where should we write the php code................
or the connection code
you need to create seperate page with '.php' extension for 'PHP Code' as described in article above
Hi
I made the Html form on the website
I made a .php page with the code and uploaded it to my server and called it “register_script.php”
I changed action” “to “register_script.php”
The form works but I get the following error
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log. Additionally, a 404 Not Found error was encountered while trying to use an Error Document to handle the request.
I think maybe I made the tables in the database wrong as I have never done one before.
I made a table called “login” with 3 columns
I set column “id” to INT and AUTO_INCREMEN and Primary
I set column “username” to INT
I set column “password” to INT
Any help would be appreciated
Hello @Andrew, You should read "error log" file of your server to know about the cause of this error... because this script doesn't have any such thing which is dealing in anyway with configuration files of server.
I think you did some erroneous changes in php.ini (PHP Configuration) file, which may be the reason behind this error.
Thanks for visiting
I just want to known that if the user password is secured with md5 and store in mysql in md5 format so on login time how its login with normal text
hello @Danish Iqbal, You first need to covert normal text into md5 hash using "MD5($normaltext);" function of PHP, then only, you need to match it with secured password stored in MySQL database.
oh thank you so much
can you give me your email id i want to add you
once again thanks
one more question hope you dont mind
do you known how to secure a php html file so that no one can see its code
Thanks
Well I Understand
Thank you so much
Really i read carefully your php simple program. I am very glad because it is easy process. But one problem that the use of azzex program and there use of into this php program. Please discuss clearly. Thanks!
Hello itech!
Everything in this code works fine, but even after I get the error that the "Username is already in use", it still puts it into the database, of course with a different user_id.
Any help of how do I stop the script if it detects that the username is already in use?
Thanks and thanks for this nice tutorial, it helped me alot.
I think your did some modification in "if-else" statements of this php script, Check if all the curly brackets of this statements are closed properly and at appropriate places. This script already has inbuilt functionality of stop executing after detecting your mentioned case/situation (via using if-else conditional statements).
Hope, It may help.
I know it is only simple script, but please do not use this in real world websites without securing every $_POST variable that enters.
Add these functions:
function secureString($string){
return htmlentities(mysqlFixString($string));
}
function mysqlFixString($string){
if (get_magic_quotes_gpc()) {
$string = stripslashes($string);
}
return mysql_real_escape_string($string);
}
Then to use on every variable do this:
$username = secureString($username);
$password = secureString($password);
$confirm_password = secureString($confirm_password);
Not Flaming just advising.
Nice tut though.
Hi. i tried the php script. i am getting a problem here. when i click on submit in the html file, the whole php code is displayed instead of processing the data.
Hell @Anas, You need to surround PHP code with this "<?php ?>" tags i.e "<?php" at starting and and "?>" at the end of script.
Yes i figured that bit out. thanks tho :)
its showing error in 17th line
mysql_num_rows(): supplied argument is not a valid MySQL
Can you please help how to solve this..
Did u created the required table with name "login" in MySQL database.
If you'll try to run this script directly without fulfilling the background requirements than such errors are obvious (See 1st paragraph of this article for reference purposes).
Sir, I have completed the steps given above to make my registration form... But in the time of registrrtion,it gets error because I am creating it in my html site. So please tell how can I added a registration form in my html site? plz reply soon...
EMAIL ID- trideep.rocks@yahoo.com
Hello @Trideep, This script requires "PHP + MySQL" need to be installed on your server and if they're already installed, then, you need to change extension of webpages from ".html" to ".php".
Hi,
I am a .Net developer and really i dont have any php background but your article is very simple and easy to understand.
I have a doubt it may be silly also please bear me,i gone through in an article that to start coding in php we should have installed servers like versions of Apache , Microsoft IIS and other server software packages. What is that i have to do with these servers?
Can you plese explain me how to start with php? I am really eager to learn php.
Thank you.
Hi,
I am waiting for the reply.
Thank you.
You can install xampp (A server software) on your local computer to use it for testing purposes before uploading files on online webhost.
Xampp comes prepackaged with PHP, Apache, MySQL, phpmyadmind etc. and you wont need to download them seperately.
hhiihihih
nice done
Thanks. Comments in the php code made understanding easier.
OK LOOKS EASY BUT SEE ON EVERY PART YOU WAS DOING THERE
IS THERE AN INDEX.PHP OR IS THERE REGISTER.PHP FILES TO BE ADDED . IF SO WERE DO THEY GO YOUVE ONLY SHOWED WHAT IT DOES , IM NEW TO THIS I ONLY NO HTML WHICH IS BETTER NEVER ANY PROBLEMS
There will be two pages... One for html form and another one for PHP code. You need to set action of form pointing to page hving PHP script.
Before all this stuff, we need to create a MySQL database and a table inside that which will be used to store our credentials.
Database structure has already been specified in article itself.
thanks alot
dear help me!
where i am upload login php at msql or other side
Thanks so much for ur help, i really enjoyed this conversations, but my problem is that i cant see anythin on my php script after clicking on my submit button. I dont know why, it just show a blank page!! I want to know if there is a specific directory or folder where i can upload my php scripts defferent from the html form folder (i.e wwwroot folder)?? Pls am ancious to see the answer to this question.
Thanks
It is recommended to keep both pages in same folder. You need to change action attribute of form pointing to "name of page containing php script". You also need to check if you did created a table as specified in article along with changes that need to be done after that.
Thanks so much!!!!
it works now, i think my table wasnt corresponding with the script, but i have rectified the problem!!! Its now working perfectly. Thanks once agian for your response
you done a good job..vital smashing code for the php beginner as well as all....nice!
ronjersan 18 months ago
can you post the complete registration membership details please like sign up and the user exist you cannot continue. or if the user not exist please i need a complete details because i am very confuse about codes i hope you can help me and post or send the complete details. ronjersan@gmail.com