PHP Tutorial Lesson 2
Introduction:
During this tutorial I will teach you:
1. Using Variables
2. Comparing
3. If / Thens
4. POST / GET
By the end you should be able to make a simple user login form.
Some Stuff You Need To Know (Basic)
* <?php -Starts the php
* ?> -Ends the php
* Anything after a // is a comment and will not be shown on runtime
* All lines should end in a semi-colon (

Except for If/Thens and Comments
* Echo outputs text to the browser
* Brackets ({ and }) let the code know that the if/then has started/finished
Using Variables:
Variables are objects that hold a chunk of information for you to use later on. Variables are always started with a dollar sign ($) and can have anything after that ($name, $password, etc.) One important thing to remember is that variables ARE case sensitive! A variable such as $name is different then a variable called $Name. An important thing to remember is that variables must start with either a letter or underscore (_). A variable name such as $1stGuy is invalid, however $_1stGuy is able to be used. Displaying variables with other strings is something unique to php, lets see this code for example.
Code:
<?php
$name = "Mike";
echo $name; //puts out Mike
echo "My name is $name"; //puts out My name is Mike
echo 'My name is $name'; //puts out My name is $name
?>
Comparing:
One important thing to remember about PHP is it's way of comparing and setting variables.
If you look in the code above you'll see that I used
The single equal sign means that you want $name to have the value Mike. Now if I wanted to see if $name equalled Mike I would use the following code
Code:
<?php
$name = "Mike";
if($name == "Mike"){
echo "Your name is Mike!";
}
?>
I used two ='s because I was comparing the two instead of making them equal. This is somewhat hard to explain so I hope you follow me.
If / Then
As you have seen before i've been using many if(... statements. These are fairly easy to comprehend and are in this format
This is very usefull for creating a hard coded login and is essential if you ever plan on coding anything.
POST / GET
NOTE: Some HTML knowledge is required for this section.
POST and GET are two different ways of transfering information from one page to another. Here is our HTML form for POST on a page that I will call login.html
Code:
<HTML>
<BODY>
<FORM ACTION = "login.php" METHOD = "POST">
<INPUT TYPE = "text" NAME = "name">
<BR>
<INPUT TYPE = "password" NAME = "password">
<BR>
<INPUT TYPE = "submit" VALUE = "Login!">
</FORM>
</BODY>
</HTML>
Now you should see some key things "ACTION = "login.php"" for example is showing that it will submit this information to login.php which I will show you later on. "METHOD = "POST"" is saying to use the POST information transfer. Please remember that you need to have it in all uppercase, typing just "post" will give you an error. Lets get on with our login.php code
Code:
<?php
$name = $_POST['name'];
$password = $_POST['password'];
if($name == "Mike" && $password = "Password"){
echo "Welcome!";
}else{
echo "Invalid Login!";
}
?>
OK one thing you may notice is the $_POST bit. What that is saying is the information that was POSTed will be assigned to a variable. Notice that the part inside the brackets is the same as the input field names in our html form? In my If/Then statement I used && to signify AND. Using GET is pretty much the same but with one small twist. Here again is our HTML code
Code:
<HTML>
<BODY>
<FORM ACTION = "login.php" METHOD = "GET">
<INPUT TYPE = "text" NAME = "name">
<BR>
<INPUT TYPE = "password" NAME = "password">
<BR>
<INPUT TYPE = "submit" VALUE = "Login!">
</FORM>
</BODY>
</HTML>
Notice the METHOD = "GET" part? Here is our login.php code
Code:
<?php
$name = $_GET['name'];
$password = $_GET['password'];
if($name == "Mike" && $password = "Password"){
echo "Welcome!";
}else{
echo "Invalid Login!";
}
?>
Yet again the only thing changed is POST to GET. The difference between POST and GET is the URL. If I were to login correctly using GET my address bar would show
http://www.server.com/login.php?nam...ssword=Password
However if I was using POST it would just show
http://www.server.com/login.php
GET is good if you want something that can be bookmarked, etc.
Conclusion:
Hopefully you understand what this code does and how it does it. Copying and pasting will get you no where. You must understand what it is doing, try experimenting. See what you can do with this simple snippet, the possibilities are endless.
Next Tutorial:
Possibly writing to a file, and opening from a file. Maybe a multiple user login via txt files, possibly a quotes system?