ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Simple Php Signup via HTML form, registration script plus code.

Updated on December 5, 2011

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,

  1. First for the HTML Form,
  2. 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>

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:

  1. 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".
  2. You can also try your own ideas to add more juices in this code.
  3. 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).
  4. 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

Comments

    0 of 8192 characters used
    Post Comment

    • itech profile image
      Author

      Krishna 6 years ago from New Delhi, India

      @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.

    • profile image

      formanzone 6 years ago

      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.

    • profile image

      sarath 6 years ago

      u r giving the basic code.. in this i want to only the string on the user name. how can i add this futures??

    • profile image

      dinesh 6 years ago

      my name is dinesh kumar verma i am ready this work

    • profile image

      samba 5 years ago

      Hi, thanks you for your codes, in the script, i'm not seeing the Table selection from the database, can you explain me ?

    • itech profile image
      Author

      Krishna 5 years ago from New Delhi, India

      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.

    • profile image

      queen 5 years ago

      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

    • itech profile image
      Author

      Krishna 5 years ago from New Delhi, India

      @queen, I will definitely do favor... But I'm not clarified with your problem, Pls Explain.

      Are you facing problems in understanding the code...

    • profile image

      Pradeesh 5 years ago

      Its working ...How can i make the page even better ...

    • itech profile image
      Author

      Krishna 5 years ago from New Delhi, India

      @pradeesh, You can use CSS (Cascading Style Sheets) for that task OR "style" attribute of any HTML tags.

    • profile image

      raju 5 years ago

      where should we write the php code................

      or the connection code

    • itech profile image
      Author

      Krishna 5 years ago from New Delhi, India

      you need to create separate page with '.php' extension for 'PHP Code' as described in article above

    • profile image

      Andrew 5 years ago

      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

    • itech profile image
      Author

      Krishna 5 years ago from New Delhi, India

      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

    • profile image

      Danish Iqbal 5 years ago

      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

    • itech profile image
      Author

      Krishna 5 years ago from New Delhi, India

      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.

    • profile image

      Danish Iqbal 5 years ago

      oh thank you so much

      can you give me your email id i want to add you

      once again thanks

    • profile image

      Danish Iqbal 5 years ago

      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

    • profile image

      Danish Iqbal 5 years ago

      Well I Understand

      Thank you so much

    • profile image

      joydevgiri 5 years ago

      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!

    • profile image

      Malzahar 5 years ago

      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.

    • itech profile image
      Author

      Krishna 5 years ago from New Delhi, India

      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.

    • profile image

      Callum 5 years ago

      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.

    • profile image

      Anas 5 years ago

      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.

    • itech profile image
      Author

      Krishna 5 years ago from New Delhi, India

      Hell @Anas, You need to surround PHP code with this "&lt;?php ?&gt;" tags i.e "&lt;?php" at starting and and "?&gt;" at the end of script.

    • profile image

      Anas 5 years ago

      Yes i figured that bit out. thanks tho :)

    • profile image

      hanamant 5 years ago

      its showing error in 17th line

    • profile image

      hanamant 5 years ago

      mysql_num_rows(): supplied argument is not a valid MySQL

      Can you please help how to solve this..

    • itech profile image
      Author

      Krishna 5 years ago from New Delhi, India

      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).

    • profile image

      Trideep 5 years ago

      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

    • itech profile image
      Author

      Krishna 5 years ago from New Delhi, India

      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".

    • profile image

      Vasi 5 years ago

      Hi,

      I am a .Net developer and really i don't 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.

    • profile image

      Vasi 5 years ago

      Hi,

      I am waiting for the reply.

      Thank you.

    • itech profile image
      Author

      Krishna 5 years ago from New Delhi, India

      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.

    • profile image

      hi 5 years ago

      hhiihihih

    • profile image

      vishal 5 years ago

      nice done

    • profile image

      Tejasvi 5 years ago

      Thanks. Comments in the php code made understanding easier.

    • profile image

      RICH 5 years ago

      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 YOU'VE ONLY SHOWED WHAT IT DOES , IM NEW TO THIS I ONLY NO HTML WHICH IS BETTER NEVER ANY PROBLEMS

    • itech profile image
      Author

      Krishna 5 years ago from New Delhi, India

      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.

    • profile image

      Praveen 5 years ago

      thanks alot

    • profile image

      sun!L 5 years ago

      dear help me!

      where i am upload login php at msql or other side

    • profile image

      Mark 5 years ago

      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 don't 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

    • itech profile image
      Author

      Krishna 5 years ago from New Delhi, India

      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.

    • profile image

      Mark 5 years ago

      Thanks so much!!!!

      it works now, i think my table wasn't corresponding with the script, but i have rectified the problem!!! Its now working perfectly. Thanks once again for your response

    • profile image

      kamukannan 5 years ago

      you done a good job..vital smashing code for the php beginner as well as all....nice!

    • profile image

      john 4 years ago

      Everything working ok but how to i auto send to thank you page upon successful registering.

      thanks

    • profile image

      maryannestoya@gmail.com 4 years ago

      sir..

      i would like to ask php ive create my registers finish to code but my problem is i don't know on how to code an output registration can you help me..tnx..

    • itech profile image
      Author

      Krishna 4 years ago from New Delhi, India

      @mary hi, I'd like to help but I didn't understand your problem clearly.

      I think, You already written registration code and you need help in executing it (Is it true).

    • profile image

      Mr AKheel 4 years ago

      Presently i'm Developing a Local Search Engine.. in this Project i need a help.. i have a Drop down select, to select CITY..

      Problem is:

      in that form a have a Default option "Select your city ",

      rest of the optioins filled with City names, Delhi, Mumbai, Hyd, Etc..

      When visitor select Mumbai from "Select" and Search for their query, on the Search Result page the "Select" option is changing to "Select your city"...

      Need:

      i want the selected city name on Search Result page as a "Selected option"... plz email if possible..

      aqeel_softs@yahoo.com

    • profile image

      RAM 4 years ago

      HELLO

    • profile image

      Akshatha 4 years ago

      I was struggling since 3days, It helped me a lot! Thank u so much!

    • profile image

      nitya 4 years ago

      Thanks for the code. I m trying to modify it, so that it doesn't take empty fields. can u help me with it?

    • profile image

      varma 3 years ago

      hi, i have used this work. i repalce host name with"local host" and database name with my database name and clicked register button, it is showning errors

    • profile image

      manish 3 years ago

      hi.....i have used this work. i repalce host name with"local host" and database name with my database name and clicked register button, it is showning errors 14 lines

    Click to Rate This Article