ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming»
  • Programming Languages

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

    • profile image

      manish 4 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

    • profile image

      varma 4 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

      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

      Akshatha 4 years ago

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

    • profile image

      RAM 5 years ago

      HELLO

    • profile image

      Mr AKheel 5 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

    • itech profile image
      Author

      Krishna 5 years ago from Dausa, 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

      maryannestoya@gmail.com 5 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..

    • profile image

      john 5 years ago

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

      thanks

    • profile image

      kamukannan 6 years ago

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

    • profile image

      Mark 6 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

    • itech profile image
      Author

      Krishna 6 years ago from Dausa, 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 6 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

    • profile image

      sun!L 6 years ago

      dear help me!

      where i am upload login php at msql or other side

    • profile image

      Praveen 6 years ago

      thanks alot

    • itech profile image
      Author

      Krishna 6 years ago from Dausa, 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

      RICH 6 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

    • profile image

      Tejasvi 6 years ago

      Thanks. Comments in the php code made understanding easier.

    • profile image

      vishal 6 years ago

      nice done

    • profile image

      hi 6 years ago

      hhiihihih

    • itech profile image
      Author

      Krishna 6 years ago from Dausa, 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

      Vasi 6 years ago

      Hi,

      I am waiting for the reply.

      Thank you.

    • profile image

      Vasi 6 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.

    • itech profile image
      Author

      Krishna 6 years ago from Dausa, 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

      Trideep 6 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 6 years ago from Dausa, 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

      hanamant 6 years ago

      mysql_num_rows(): supplied argument is not a valid MySQL

      Can you please help how to solve this..

    • profile image

      hanamant 6 years ago

      its showing error in 17th line

    • profile image

      Anas 6 years ago

      Yes i figured that bit out. thanks tho :)

    • itech profile image
      Author

      Krishna 6 years ago from Dausa, 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 6 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.

    • profile image

      Callum 6 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.

    • itech profile image
      Author

      Krishna 6 years ago from Dausa, 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

      Malzahar 6 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.

    • profile image

      joydevgiri 6 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

      Danish Iqbal 6 years ago

      Well I Understand

      Thank you so much

    • profile image

      Danish Iqbal 6 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 6 years ago

      oh thank you so much

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

      once again thanks

    • itech profile image
      Author

      Krishna 6 years ago from Dausa, 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 6 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 6 years ago from Dausa, 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

      Andrew 6 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 6 years ago from Dausa, India

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

    • profile image

      raju 6 years ago

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

      or the connection code

    • itech profile image
      Author

      Krishna 6 years ago from Dausa, India

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

    • profile image

      Pradeesh 6 years ago

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

    • itech profile image
      Author

      Krishna 6 years ago from Dausa, 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

      queen 6 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 6 years ago from Dausa, 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

      samba 6 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 ?

    • profile image

      dinesh 7 years ago

      my name is dinesh kumar verma i am ready this work

    • profile image

      sarath 7 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

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

    • itech profile image
      Author

      Krishna 7 years ago from Dausa, 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.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: "https://hubpages.com/privacy-policy#gdpr"

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)