February 9th 2007

(0) PHP Lesson 5

Comments RSS Feed Educational, Programming, Tutorials

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.

Write a Response Write a Response
  1. (required)
  2. (required)

  3. (required)
  4. (required)
    Anti-Spam Image