HTML SideQuote:
FULL CREDITS TO stealarcher
Ok, in this guide im going to do my best to teach you how to make your own register page using PHP and a LITTLE of html. The first thing any register page needs is obviously the form, so thats what we will start with.
To make a form, you will need to use HTML. So lets go ahead and make a new text file, and call it register.php (its .php because we will be using PHP inside of the file). The first thing to start a form, you need to make sure that the browser knows its a form in order for it to process. How you do this is by writing the following inside your register.php file.
To explain this a little bit, obviously we are making a form by calling it, the method is how we are going to pass the information. It can be either post or get. The post method is more safer, and you can pass more information across with it. The post method also passes hidden information across, whereas the get method will post the information across in your address bar. It is very important you select the post method for this tutorial because PHP handles each method differently.
The action part of the form is where you want the information being passed to. In this case, sense the action is blank, it will pass it to the page that we are currently on. You can set it to a different page if you wanted. Such as process_register.php or something as your site gets bigger, but for now, we will stick with the K.I.S.S. (keep it simple stupid) way of doing things.
Now to explain the </form>. The </form> is used to tell the browser that you are ending the use of the form.
Now that we have explained how to start the form in HTML, we can get into the different types of inputs there are in forms. You will be using 3 main input types in your register page. A text, password, and a submit type. All of them do exactly what they say. So this is how they are used.
The Username I have wrote is just the text that is being displayed before the field. The input type is telling the browser how to display the information. In this case, it will be a text box that shows text while information is entered. The name is the identifier that we will be able to access through the post function later on, so basically for now, all you need to know is that it is used to access the information passed from that field. The value part of the form is the pre-filled information in the text box. So you could put David in the value, and when you view the form, there will already be text in there saying David. Then the /> is used to properly end the line of HTML. Now to take a look at a hole register form.
In these lines of code, you can still see the input types, text, password, and submit. The text will make a text box that displays text for each key entered, the password will make a text box that displays circles for each key entered that way it hides the password as its being entered. The submit makes a button that will actually tell the browser to process the form. And as you can see, we still have the form tags around the actual form fields. Now, the only thing we haven't discussed in this tutorial is the <br/>. Because HTML doesnt actually use white space, and just hitting enter wont make a new line on your website, you have to use <br/> to make a new line. So basically its telling the browser to hit enter and stop being retarded.
Overview
So far we have discussed how to make the actual form inside html, and how to pass the information across, now we can actually start doing something with that information to make it actually do something.
To make a form, you will need to use HTML. So lets go ahead and make a new text file, and call it register.php (its .php because we will be using PHP inside of the file). The first thing to start a form, you need to make sure that the browser knows its a form in order for it to process. How you do this is by writing the following inside your register.php file.
HTML Code:
<form method="POST" action=""> </form>
The action part of the form is where you want the information being passed to. In this case, sense the action is blank, it will pass it to the page that we are currently on. You can set it to a different page if you wanted. Such as process_register.php or something as your site gets bigger, but for now, we will stick with the K.I.S.S. (keep it simple stupid) way of doing things.
Now to explain the </form>. The </form> is used to tell the browser that you are ending the use of the form.
Now that we have explained how to start the form in HTML, we can get into the different types of inputs there are in forms. You will be using 3 main input types in your register page. A text, password, and a submit type. All of them do exactly what they say. So this is how they are used.
HTML Code:
Username: <input type="text" name="username" value="" />
HTML Code:
<form method="post" action=""> Username: <input type="text" name="username" value="" /><br/> Password: <input type="password" name="pass" value="" /><br/> Repeat Password: <input type="password" name="pass2" value=""/><br/> <input type="submit" name="submit" value="Submit" /> </form>
Overview
So far we have discussed how to make the actual form inside html, and how to pass the information across, now we can actually start doing something with that information to make it actually do something.
PHP Side
The first thing you have to do when working with PHP at any time, is tell the browser that you are going to work with PHP. In order to do this you will use the "PHP Tags".
Html cannot be used directly inside of the "PHP Tags" unless you use something called an echo or a print. For now, sense we are using the K.I.S.S. method, keep the form information from above OUTSIDE of the "PHP Tags", and all of your PHP Code, INSIDE of the "PHP Tags".
The <?php tells the browser you are going to start coding in PHP, and the ?> tells the browser you are done coding in php.
The first thing we will need to do in order for the register to work what so ever is connect to your database. Almost every server uses mysql to actually store account information, unless you are using 5165 or something that is done by text files, which I will cover later on. To connect to a mysql database, you will need to do the following.
As you can see, we have our programming inside of our "PHP Tags" still. The dollar sign $ is stating that we are accessing a variable, in this case we are setting it equal to the mysql connection and another variable to the mysql database. You can name the $connection or the $database what ever you want, but I like to keep it simple and name it what its used for.
The mysql_connect is a built in function in PHP used to connect to a mysql database. It has a total of 3 arguments, each one is split out by a comma. The first argument (or "localhost" above) is the host your connection is running on. If you are running your server on the computer your running your website off of, it will be localhost for you as well. The second argument (or "db username") is the username that you use to login to mysql. In most cases the default is root. The third argument (or "db password") is the password that you use to login to mysql. Also if you notice, each line its with a ; to tell the browser that you are ending that line of php code and are starting a new line. Without the ; you may come across many errors.
In the $database variable you can see that we are setting it to mysql_select_db, which basically does exactly what it says, it selects the database inside mysql that you are going to be using. This will be your copro, coemu, or w/e you name your database inside of mysql. And that will end with the ; as well. So the most common way you will see this is the following.
Now that we have got PHP to connect to mysql, lets start accessing the form information we pass across from the HTML form we did earlier.
I know that looks like a lot and its im not expecting you to understand it all at once. I will break down each and ever part of it for you so u can understand it a little more. As we discussed earlier, they are still inside the "PHP Tags", and we still have our connection to MySQL up at the top. After the connection you will see the following.
This statement is basically saying, If the $_POST['submit'] variable is set (hints the isset function), then do the following inside the brackets. If $_POST['submit'] is set (submit is the variable passed by our html form we created earlier), then that means that the form has been submitted.
If you notice the name="submit" is why we are using $_POST['submit']. So if you use the following.
Then in order to to check if the form was submitted, we would use the following.
If it hasnt been submitted, then we dont need to do anything at all. So what you should get out of this statement is for 1, to check if something is true, you use the if statement, and the $_POST array contains all the form information using the method "post". Remember earlier when we were talking about post and get?
This line of HTML code is where we selected the method post.
Well this is where it comes in handy. If you selected the method to be get earlier, we would be using $_GET['submit'] for the information.
The next line inside of code checks whether or not the password field has been used or not.
As you can see, the code up top checks to see if the variable pass is set and to check if its not equal to nothing. So if the pass variable is set and its not equal to nothing, it basically means they have entered in a password, so if they entered in a password, it will continue with the code inside of the brackets.
The next line of code will check whether or not the password field and password 2 fields are equal to each other.
As you can see, instead of using = we use the == operator. The = operator is to set information to the variable, where the == operator is used to check if its equal or not equal to (in other words a boolean). So the above is checking if pass and pass2 are equal to each other.
The next line of code checks the length of the string for password to check if its inside the length you want them.
The strlen function is used to get the number of characters inside of a string. In this situation we are basically saying if the string inside $_POST['pass'] is greater than 3 AND (the && operator is basically saying AND) check to make sure its less than 32 characters. If its between both of those characters, it will continue the line of code inside the brackets.
Inside of the brackets you will see the following.
The first $sql is setting a string to the variable for later use. As you can see, it is saying INSERT INTO `tablename` which would be the table you want to insert the information into. Then the (Table1, Table2) are the columns or fields inside of the table that you want the information inserted into. Then the VALUES operator to tell mysql that we are inserting the following values. ('".$_POST['username']."', '".$_POST['pass']."') is the values we are inserting into Table1 and Table2. Notice that the first table (Table1) will be inserted with the Username information. Due to them being both in the same slot, and the Table2 will be inserted with the password information because they are both in the 2nd slot.
The next line is setting the variable $sql to the mysql_query of $sql. This is actually executing the query to insert the information into mysql.
The line after that is the echo, this will basically display the text Registered Successfully onto your website.
So the hole register page should look like this.
PHP Code:
<?php
?>
The <?php tells the browser you are going to start coding in PHP, and the ?> tells the browser you are done coding in php.
The first thing we will need to do in order for the register to work what so ever is connect to your database. Almost every server uses mysql to actually store account information, unless you are using 5165 or something that is done by text files, which I will cover later on. To connect to a mysql database, you will need to do the following.
PHP Code:
<?php
$connection = mysql_connect("localhost", "db username", "db password");
$database = mysql_select_db("db name");
?>
The mysql_connect is a built in function in PHP used to connect to a mysql database. It has a total of 3 arguments, each one is split out by a comma. The first argument (or "localhost" above) is the host your connection is running on. If you are running your server on the computer your running your website off of, it will be localhost for you as well. The second argument (or "db username") is the username that you use to login to mysql. In most cases the default is root. The third argument (or "db password") is the password that you use to login to mysql. Also if you notice, each line its with a ; to tell the browser that you are ending that line of php code and are starting a new line. Without the ; you may come across many errors.
In the $database variable you can see that we are setting it to mysql_select_db, which basically does exactly what it says, it selects the database inside mysql that you are going to be using. This will be your copro, coemu, or w/e you name your database inside of mysql. And that will end with the ; as well. So the most common way you will see this is the following.
PHP Code:
<?php
$conn = mysql_connect("localhost", "root", "mysqlpass");
$db = mysql_select_db("coproj");
?>
PHP Code:
<?php
$conn = mysql_connect("localhost", "root", "mysqlpass");
$db = mysql_select_db("coproj");
if(isset($_POST['submit']))
{
if(isset($_POST['pass']) && $_POST['pass'] != "")
{
if($_POST['pass'] == $_POST['pass2'])
{
if(strlen($_POST['pass']) > 3 && strlen($_POST['pass']) < 32)
{
$sql = "INSERT INTO `tablename` (Table1, Table2) VALUES ('".$_POST['username']."', '".$_POST['pass']."')";
$sql = mysql_query($sql);
echo "Registered Successfully";
} else {
die("Your password must be between 3 and 32 characters.");
}
} else {
die("Your passwords do not match.");
}
} else {
die("Please enter in a password.");
}
}
?>
PHP Code:
if(isset($_POST['submit']))
{
}
HTML Code:
<input type="submit" name="submit" value="Submit" />
HTML Code:
<input type="submit" name="processform" value="Submit" />
PHP Code:
if(isset($_POST['processform']))
{
}
This line of HTML code is where we selected the method post.
HTML Code:
<form method="post" action="">
The next line inside of code checks whether or not the password field has been used or not.
PHP Code:
if(isset($_POST['pass']) && $_POST['pass'] != "")
{
}
The next line of code will check whether or not the password field and password 2 fields are equal to each other.
PHP Code:
if($_POST['pass'] == $_POST['pass2'])
{
}
The next line of code checks the length of the string for password to check if its inside the length you want them.
PHP Code:
if(strlen($_POST['pass']) > 3 && strlen($_POST['pass']) < 32)
{
}
Inside of the brackets you will see the following.
PHP Code:
$sql = "INSERT INTO `tablename` (Table1, Table2) VALUES ('".$_POST['username']."', '".$_POST['pass']."')";
$sql = mysql_query($sql);
echo "Registered Successfully";
The next line is setting the variable $sql to the mysql_query of $sql. This is actually executing the query to insert the information into mysql.
The line after that is the echo, this will basically display the text Registered Successfully onto your website.
So the hole register page should look like this.
PHP Code:
<form method="post" action="">
Username: <input type="text" name="username" value="" /><br/>
Password: <input type="password" name="pass" value="" /><br/>
Repeat Password: <input type="password" name="pass2" value=""/><br/>
<input type="submit" name="submit" value="Submit" />
</form>
<?php
$conn = mysql_connect("localhost", "root", "mysqlpass");
$db = mysql_select_db("coproj");
if(isset($_POST['submit']))
{
if(isset($_POST['pass']) && $_POST['pass'] != "")
{
if($_POST['pass'] == $_POST['pass2'])
{
if(strlen($_POST['pass']) > 3 && strlen($_POST['pass']) < 32)
{
$sql = "INSERT INTO `tablename` (Table1, Table2) VALUES ('".$_POST['username']."', '".$_POST['pass']."')";
$sql = mysql_query($sql);
echo "Registered Successfully";
} else {
die("Your password must be between 3 and 32 characters.");
}
} else {
die("Your passwords do not match.");
}
} else {
die("Please enter in a password.");
}
}
?>
If you have any questions or wish for this to be explained further, please ask questions on this post. Thank you.