February 9th 2007
Educational, Programming, Tutorials
Well we’ve reached the end of week one of my series of lessons on PHP. We are going to incorporate everything we have learnt in lessons one, two, three and four.
In today’s lesson we’re going to create a login script in PHP and you’ll see all you’ve learnt in it as well as some new things that we are going to cover in next week’s lessons - 6 to 9. Enjoy and try it out yourself on your own server. We will take another look at this script later on and implement a mySQL connection into the script - but don’t worry about this until later.
Lesson 5: Simple Login Script
First of all we will take the simple username and password form from lesson 4 and use this. This will be our login.php file. Here it is again:
<form action="process.php" method="post">
Username: <input type="text" name="username" size="25" />
Password: <input type="password" name="password" size="25" />
<input type="submit" value="submit form" />
</form>
Now we’ll extend this with some extra PHP code which will be used to display any error messages if the user inputs an invalid username or password. This will be added above the form code.
<?php
if($_GET['e']) {
$error = "Wrong username or password. Please try again.";
}
if($error) {
?>
<p style=”padding:10px;border:red;color:black;”>
<?php echo $error; ?>
</p>
<?php
}
?>
Okay so now we can create the process.php file which will digest the submitted form data. Take a look at the file and go over the previous four lessons to see what you can remember how to do.
<?php
$username = $_POST['username'];
$password = $_POST['password'];
if(!$username || !$password) {
header(”Location:login.php?e=1″);
}
else {
if ($username == “username” && $password == “password”) {
header(”Location:members.php”);
}
else {
header(”Location:login.php?e=1″);
}
}
?>
There may be things in there you don’t understand just yet but I assure you we’ll get to them. IF statements, $_GET global and some other things are what we’re going to visit in next week’s tutorials. When we’ve learnt functions and mySQL connections we will revisit this form and make it more dynamic. Hope you enjoyed these extremely simple and basic tutorials. See you next week.
February 8th 2007
Educational, Programming, Tutorials
Previously we looked at some of the methods PHP offers to sort and manipulate strings and we specifically looked at str_replace() and encryption methods like md5() and sha1().
In this lesson, the final one before a big project, we’re going to take a look at how to capture inputs from form fields. Also I’m going to talk about the secure and insecure ways to transfer the data.
Lesson 4: Form Field Input
Getting the input from a form is pretty simple and in this lesson we are going to keep it that way by using two files: form.php and process.php. The first will contain the form we are going to create which a user will fill in. The latter file will take the input, process it and output it.
Here’s the sample form we’re going to use. In this case it’s just a simple username and password form. This form does not validate and is in no way conforming to accessibility either.
<form action="process.php" method="post">
Username: <input type="text" name="username" size="25" />
Password: <input type="password" name="password" size="25" />
<input type="submit" value="submit form" />
</form>
Once the user submits the information by the button they’ll be taken to process.php along with the values of the form fields. We use the $_POST global to take the form values. For example if we wanted to take the value of the username field and assign it to $username:
$username = $_POST['username'];
//getting the value of the form field with the name of ‘username’
The variable in between the square brackets refers to the name=”" part of the inputs in the form. So now we can create the process.php file from what we’ve learned.
<?php
$username = $_POST['username'];
$password = $_POST['password'];
echo “You’re username is “.$username.” and your password is “.$password.”.”;
?>
So there is the simple way to collect and assign form variables sent over pages. The next lesson, lesson 5, is where we are going to put all we’ve learnt from lessons 1 to 4 into practise. You’ll use what you’ve learnt and some new stuff that will introduce you to lessons 6 to 9. Learn it!
February 7th 2007
Educational, Programming, Tutorials
In lesson 2 we touched on variables in PHP and how to create one and then change it via a number of processes. Today we’re going to use some of the functions that PHP provide which allow us to manipulate and query strings and that will most likely become very handy in the future.
If you wish to take a look at the functions it offers then head on over to the PHP Manual, specifically the string functions.
Lesson 3: String Functions
Last week we touched on how to find the length of a string using the strlen() function. Today we’re going to look at a few more functions and put them into a practical example. To find the length of a string all we have to do is employ this function as is shown here:
<?php
$five = strlen("mouse"); //$five’s value is 5
$word = “mousemat”;
$eight = strlen($word); //$eight’s value is 8
?>
Now we’re going to look at replacing characters or phrases within a string with something else. To do this in the simplest way we are going to use the str_replace() function. There’s several parameters to this function. This means that we have to provide more information within the functions. In this case the format is str_replace(string to search for, string to replace it with, string to search within).
$string = "Sunny Man’s Blog";
$string = str_replace("Man","Boy",$string); //replace ‘Man’ with ‘Boy’
echo $string; //displays Sunny Boy’s Blog
Later on when we get to talking about SQL and databases you’ll need to be able to encrypt strings. There are several different types of encryption available in PHP. All of them are simple enough to use but, as we will will discuss later on, knowing when to use the correct one is vital. First let’s take a look at the different types.
$password = md5("password");
echo $password; //5F4DCC3B5AA765D61D8327DEB882CF99
$password = sha1(”password”);
echo $password; //5BAA61E4C9B93F3F0682250B6CF8331B7EE68FD8
Both md5 and sha1 have their advantages and disadvantages but they’re both quick to generate and once stored in a variable, easy to remember. Say we wanted to take a raw password that was given to us by a form (we’ll cover this soon), replace the letter ‘i’ with ‘z’ and encrypt it for storage in a database, this is how we would do it:
<?php
$password = "birmingham";
$password = str_replace("i","z",$password);
$password = md5($password);
?>
This is the end of lesson 3. In the next lesson we’ll talk about taking variables from a form, collecting them and manipulating them in a real life situation. After than we’re going to put all we’ve learnt into a logging in script in lesson 5 which will introduce you to salts, database queries and some other stuff we will cover in lessons 6 to 10.
February 6th 2007
Educational, Programming, Tutorials
In a previous post I started you off on the basics of PHP and you managed to get Hello World! displayed on the screen. This lesson we are going to learn about how to create, use and manage variables. Hope you enjoy it…
Lesson 2: Variables
Unlike programming languages such as C++, in PHP you don’t have to state what type of variable you’re creating when you create one. A variable such as $myvar could just as well be a number as it could be a word. It’s as easy as pie to create different variables:
<?php
$age = 15;
$name = "John";
?>
Here we created 2 variables: $age and $name which values’ are 15 and ‘John’ respectively. It’s that easy to create variables. Now that we have created variables we can manipulate them. Say for example we wanted to increase the $age variable and assign the new value to $age again, this is what we would add before the end of the script:
$age = $age+1;
If we now output the $age variable we would find that it displays 16 and not 15.
echo $age; //this would display ‘15′
We can also attach strings together. Take the $name variable we created earlier. We can take that and also create a last name in a different variable and then string them together to make the $name variable include the first and last name. Here we go:
$lastname = "Smith";
$fullname = $name.$lastname; //this would create “JohnSmith”
$fullname = $name.” “.$lastname; //this would create “John Smith”
So, let’s try and put this all together and make a useful program out of it. Here we manipulate the age by turning it into dog years. We also here string the first and last names together and then as a step towards the next lesson we’ll count the number of characters in the names.
<?php
$age = 15;
$firstname = "John";
$lastname = "Smith";
$i = 0; //used later on as a counter
$dog = $age*7;
echo “You’re “.$age.” human years old and “.$dog.” dog years!”;
echo “<br />”; //create a new line; just regular html
$name = $firstname.” “.$lastname;
$i = $i + strlen($firstname); //0 + 4
$i = $i + strlen($lastname); //4 + 5
echo $name.”, you have “.$i.” letters in your name!”;
?>
Try it out and play around with it! Next lesson we’re going to look at some of the functions we can apply to strings such as getting the length of the string and searching for a specific item within a variable.