Archive for the ‘Tutorials’ category

PHP Lesson 7

February 13th, 2007

Sunny Man’s TutorialsMy lessons are now into their second week and we’ve still got a way to go yet. This week we will be looking into IF and WHILE statements, $_GET, $_POST and we’ll also look into arrays near the end of the week.

In the previous lesson we went into detail about IF and WHILE statements and in Friday’s big round up I introduced you to $_POST and $_GET. Today we’re going to look further at $_GET and what it’s used for.

Lesson 7: $_GET and URLs

When we have a URL that includes the values of certain variables, we can use this to get the values. Say we had a URL like this:

http://www.eop.org.uk/index.php?name=sunny%20man&cool=yes

We use $_GET to grab the values of the name and cool variables. Obviously this isn’t a secure way to transport variables around your site – we’ll look at a more secure way later on into the tutorials – this is the most simple way.

<?php
$name = $_GET['name']; //$name = "sunny man"
$cool = $_GET['cool']; //$cool = "yes"
?>

Tomorrow I’ll be talking about $_POST and how it can be confusing when to use the name and id attributes on form field inputs.

PHP Lesson 6

February 12th, 2007

Sunny Man’s TutorialsMy lessons are now into their second week and we’ve still got a way to go yet. This week we will be looking into IF and WHILE statements, $_GET, $_POST and we’ll also look into arrays near the end of the week.

As always we will find a useful example we can apply everything to at the end of the week as well as introducing some things we are going to look at in the following week. I hope you’ve learnt something in the previous week but don’t worry we will get more advanced as time goes on.

Lesson 6: IF and WHILE

In last week’s final day lesson I introduced you to an IF statement. I’m going to go over them in more detail today as well as introducing you to a WHILE loop. Here’s the IF statement I used in Friday’s example:

if ($username == "username" && $password == "password") {
     ...
}

This says: if $username’s value is “username” and if $password’s value is “password” then do this… If the statement in the ( ) brackets are true then it executes the instructions within the { } brackets.

There are a variety of comparison operators that you can use. The one used in the example is == which means equal to. There is also != – not equal to, === – exactly equal and of the same type, !== not exactly equal and not of the same type, > – is bigger than, < – is smaller than, >= – is bigger than or equal to, <= – is smaller than or equal to.

If you want to check more than one thing in an IF statement then you need to use logical operators, such as in the example I’ve used && to indicate that the statement should be true if the username and the password are both correct. Here are some examples:

if ($username || $password) { ... } // OR
if (!$username) { ... } // NOT
if ($username XOR $password) { ... } // XOR (exclusive OR)

Now we have got the hard bit out of the way we can now look at WHILE statements. WHILE statements are a little different to IF statements because they perform the instructions inside the { } brackets while a comparison remains true.

<?php
$a = 1;
$b = 5;

while ($a < $b) { //while $a is less than $b
     echo $a;
     $a++; //increment $a by 1
}
?>

WHILE loops come in very handy sometimes – for example when we look at arrays we will use a WHILE loop to good effect. A famous example of using ‘the loop’ is the WordPress blogging system. This is the basic form of IF and WHILE loops. Remember them!

PHP Lesson 5

February 9th, 2007

Sunny Man’s TutorialsWell 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.

PHP Lesson 4

February 8th, 2007

Sunny Man’s TutorialsPreviously 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!