Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

PHP Register Page

registering

  • Please log in to reply
11 replies to this topic

#1 Epatron

Epatron

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 54 posts

Posted 13 August 2011 - 05:46 AM


Posted Image
PHP Register Page
Tutorial by: Epatron

  • Connect to MYSQL DB
  • HTML Form
    • Code
  • PHP Part
    • Create Table
    • Register Code
    • Simple
    • Little Detailed
    • Fully Detailed
Connect to MYSQL DB:
We need a file named connect.php which connects us to mysql database! We will include that to other file later at this tutorial!
Go HERE, to find out how the connection is made!
HTML Form:
Create file named index.html and do simple register form, please understand that I'm not going to style forms , you can do it, if you want!
<html>
<head><title>My Register Page</title></head>
<body><form action="register.php" method="post">
<h2>Register:</h2><br /><br />
<b>User Name:</b><br />
<input type="text" name="username" /><br />
<b>Password:</b><br />
<input type="password" name="password" /><br />
<b>Re-type Password:</b><br/>
<input type="password" name="repassword" /><br />
<input type="submit" value="Register!" /></form>
</body>
</html>

I presume that you understand html forms well, so I'm not going to explain it!
PHP Part:
Create Table:
We need to create table to our database, so we can save all registered users to database!
You can now choose how you wanna do the table to database! WITH PHP or SQL
With PHP:
Create a file with name: install.php and put this code into it:
<?php
include ("connect.php"); // Includes the connect.php ! You don't have? Check tutorials first part "Connect to mysql DB" again!
mysql_query("create table users(
username varchar(30) NOT NULL,
password varchar(32) NOT NULL,
PRIMARY KEY (username)
)") or die (mysql_error());
echo "Table created! DELETE THE INSTALL.PHP NOW!";
?>

If you get "Table created!" text, table is created to your database, and you need to delete the install.php from your folder!
I will explain the code after showing the SQL way to do table!
With SQL:
Go to phpmyadmin and select SQL above! Image instructions:
Posted Image
and insert this code into the box:
CREATE TABLE users(
username varchar(30) NOT NULL,
password varchar(32) NOT NULL,
PRIMARY KEY (username)
)
Explaining:
CREATE TABLE users(
Kinda explains it by itself ! So this code creates table named 'users' don't forget the ( to the beginning!
username varchar(30) NOT NULL,
password varchar(32) NOT NULL,
"username" and "password" are the field names, "varchar" its the data type of the field and it will contain the letters, "(30)" and "(32)" are the maxium length of the fields and "NOT NULL" means that the field can't be empty.
PRIMARY KEY (username)
)
Primary key is an "unique identifier", means that there can't be two with the same text (username).

Register code:
Now It's time to code the hardest thing at the register!
We're gonna use functions: mysql_query and mysql_num_rows!
Create file named: register.php and go through all the points:
Simple Register code:
I don't recommend to use this, but you should go trough this code, because I will some parts at this O N L Y!
And you will learn the register code better!
<?php
include ("connect.php");
mysql_query("INSERT into users VALUES('".$_POST['username']."','".$_POST['password']."')") or die(mysql_error());
?>

This is a really simple register code, you can try and check is it working! But as you might noticed we need to check this:
-Username already in use? and -Does the passwords matches?
But before adding them, I want to explain this code, because it will be on better codes too!
Explaining:
"mysql_query" is function which asks the MYSQL database to do something, in this case we're asking to "INSERT INTO users" table, and
after that line it tells what we will insert to users table! "VALUES" and we will insert the username and password from form!
Little detailed register code:
I don't recommend this either, only the Fully detailed is what I want to you use, but just for learning:
<?php
include ("connect.php");
//Let's do code for if username already is in use!
if(mysql_num_rows(mysql_query("SELECT * from users WHERE username='" . $_POST['username'] . "'")) == 1){
echo "Oops! Username is already in use";
}else{
mysql_query("INSERT into users VALUES ('".$_POST['username']."', '".$_POST['password']."')") or die(mysql_error());
}
?>

Now, we added if statement which checks that is username avaible or not, if its already in use you will get message: "Sorry..."
We'are using SELECT * from to check the username exist.
And now you might think: Is it possible to add more errors like that username already taken? Answer is YES!

Fully detailed register page:
I recommend to use this code, it checks these things:
-Unknown letters in username, -Username already taken, -Username too long, -Username too short
-Unknown letters in password, -Two entered passwords wont match, -Password too long, -Password too short.
And if the user is created it will lead you back to index.html !
<?php
include ("connect.php");
if(mysql_num_rows(mysql_query("SELECT * from users WHERE username='" . $_POST['username'] . "'")) == 1){
echo "Oops! Username is already in use!";
}
else if($_POST['password'] != $_POST['retype-password']){
echo "Oops! The two entered passwords don`t match!";
}
else if(strlen($_POST['username']) > 15){
echo "Oops! Username is too long!";
}
else if(strlen($_POST['username']) < 6){
echo "Oops! Username is too short!";
}
else if(strlen($_POST['password']) > 15){
echo "Oops! Password is too long!";
}
else if(strlen($_POST['password']) < 6){
echo "Oops! Password is too short!";
}
else if(preg_match('/[^0-9A-Za-z]/',$_POST['username'])){
echo "Oops! Unknown letters at username!";
}
else if(preg_match('/[^0-9A-Za-z]/',$_POST['password'])){
echo "Oops! Unknown letters at password!";
}
else{
mysql_query("INSERT into users VALUES ('".$_POST['username']."', '".$_POST['password']."')") or die(mysql_error());
header('Location: index.html');
}
?>

Gz! You know have working register page! If you want to keep up the log of the user registers, check this tutorial about the php file functions!
You will learn log-in at the next tutorial!
Errors / Questions? Send message below or PM me ! :)

Edited by Epatron, 14 August 2011 - 03:56 AM.

  • 1

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 13 August 2011 - 06:19 AM

General rule of thumb: never save the password in plaintext. Save a secure hash (such as SHA-256) of the password instead. Preferably salted :)
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 14 August 2011 - 03:35 AM

Also you should do some input sanitation on all the $_POST fields.

Another point to make, the validation regular expressions used to check if a username or password contains invalid characters will probably be used frequently throughout the site. For this reason, in a real implementation of this it would be benefical to have these in a function/class somewhere for easier reuse. :)

Other than that, good job!
  • 0

#4 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 16 August 2011 - 10:39 AM

As chili5 pointed out, your code is vulnerable to SQL injections. Please fix these.
  • 0

#5 charsta

charsta

    CC Regular

  • Member
  • PipPipPip
  • 28 posts

Posted 27 February 2012 - 05:14 PM

I know that topic is a little bit old but I have a question!
Can we somehow modify this part of code:
else if(preg_match('/[^0-9A-Za-z]/',$_POST['username'])){
echo "Oops! Unknown letters at username!";
}

and make it also check if the username starts with a letter and not with a number?

(I thought that it was a good idea not to open a new topic for this!)

Thank you
  • 0

#6 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 27 February 2012 - 05:18 PM

Yeah

if (preg_match("/[A-Za-z].+/",$_POST["username"]))

That should work but I didn't test it. To match a letter at the beginning use the pattern [A-Za-z]. Let me know if that works. I'm not familar with the exact semantics of preg_match but fairly familar with regex.

This regex [A-Za-z].+ matches any string that starts with a letter followed by 1 or more characters.
  • -1

#7 charsta

charsta

    CC Regular

  • Member
  • PipPipPip
  • 28 posts

Posted 27 February 2012 - 05:25 PM

Yes it works!
Thank you
  • 0

#8 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 27 February 2012 - 05:26 PM

You're welcome.
  • 0

#9 alvera

alvera

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts
  • Learning:Java, C#, PHP, JavaScript, Visual Basic .NET

Posted 17 November 2014 - 06:31 PM

how to notify user if they didn't completed the sign up form? for example when they just leave their username empty


  • 0

#10 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 17 November 2014 - 08:02 PM

how to notify user if they didn't completed the sign up form? for example when they just leave their username empty

 

hello alvera, you my simply check for the right POST data, and if it is empty or not valid, you can have the page lead back to the register page with a small notice. You can store their valid information in a session to prevent them from needing to re-enter all information, and the session array can contain an error and reason for them to view in a box somewhere.

 

Alexander.


  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#11 alvera

alvera

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts
  • Learning:Java, C#, PHP, JavaScript, Visual Basic .NET

Posted 17 November 2014 - 10:09 PM

hello alvera, you my simply check for the right POST data, and if it is empty or not valid, you can have the page lead back to the register page with a small notice. You can store their valid information in a session to prevent them from needing to re-enter all information, and the session array can contain an error and reason for them to view in a box somewhere.

 

Alexander.

hello, thanks for ur reply.. actually i am new with php and still in learning process, can u show me some example simple coding for noticing user when user leave box empty?


  • 0

#12 WesleyfuhPP

WesleyfuhPP

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 25 December 2016 - 10:08 AM

Hi everyone, I created a php page which contains forms textfields and drop-down boxes. I can see my page when I try on localhost. But after I upload the page via ftp and call my site, I only see "51" on the page. Can anyone tell me why? Thanks 11
  • 0





Also tagged with one or more of these keywords: registering