Your First Script: A Password Protected Site
Before I let you see the PHP-script and explain it, I want to fill you in on some the basics.
- All the PHP-code goes between <?php and ?>.
- Variables are used to store any data, anything really. If you see any combination of letters and numbers with a $ in-front of them, it’s a variable.
- //Means that the rest of the line is a comment, and will therefore be ignored by PHP
I’ll explain more after the script.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Restricted area</title>
</head>
<body>
<?php
if($_POST['username'] === 'mr_president' && $_POST['password'] === 'AV3rYg0odp4s5w0rd') { //Here we check if the username AND password are correct
echo '<p>All your government secrets are safely stored here.</p>'; //and greet the users
} else { //and if the username and password aren't correct
echo '<p>Nothing to see here, move along.</p>'; //we pretend like there's nothing interesting here
}
?>
</body>
</html>
There’s not much PHP there, but there’s not many features either. What do you think the correct username and password is? Highlight the next line to find out.
username: mr_president password: AV3rYg0odp4s5w0rd
Let’s look through the PHP. The first line after <?php says ”if(blablabla) {”. If means that if the statement inside the parentheses (the ”()”) is true, the code inside the curly brackets (the “{}”) will be executed. You can also see “else {…}” after ” if(…){…}”, this means that if the statement in the if was false, the else will be executed. but if it was true, the else won’t be executed.
Okay, I’m sorry for that paragraph, I don’t understand what I’ve written myself. Try to read through it and look at the code to see if you understand it. You can also ask me questions about it, I’ll be happy to answer to clear things up.
I mentioned something called statement. A statement is exactly what it sounds like. The statement was ”$_POST[’username’] === ‘mr_president’ && $_POST[’password’] === ‘AV3rYg0odp4s5w0rd’”. This states that the variable $_POST[’username’] is mr_president AND that the variable $_POST[’password’] is AV3rYg0odp4s5w0rd. The statement either returns true or false, here it depends on what the user wrote.
“===” Means that the two values you compare have to be exactly alike. You can also use “==”, but it is slower. With “==” the number 0 (used to calculate) and the letter ‘0′ (data from the user, or output to the user) will be the same, while with “===” the number 0 will only be equal to the number 0, and not the letter. One thing to remember is that input from the users will always be letters (although they can be used as numbers, but more on that later). Note that you don’t use “=” in statements, as “=” means that you set a variable to a value, and that will always be true.
Statements have two ”separators”, && and ||. && means AND, as in both a and b have to be true, while || means OR, a or b has to be true. Lets call four different statements a, b, c and d. “a && b || c && d” will be true if either a and b are true, c and d are true or all of them are. You can also use parentheses in statements. “a && (b || c) && d” will be true if a is true and b or c is true and d is true.
The last piece of PHP I want to explain is echo. echo outputs text to the browser. The text is written between ‘ and ‘, and after the echo ‘text here’, you will need a ; so that PHP knows that you’re finished outputting text to the browser. Note that instead of using echo, you could have just closed the PHP-tag (?>), written the text you wanted to display to the user, and opened PHP again (<?php).
Now you hopefully know more PHP than you did when you started reading this, but you still have got much to learn. I’m not quite finished yet though, as I still have some knowledge I wish to share with you on the next page.
April 13th, 2008 at 1:25
Very good beginners tutorial dtph
I look forward to reading future php tutorials!
April 13th, 2008 at 10:40
Thanks ^^,
If somebody don’t understand all of the tutorial, don’t worry. Try to get the bigger picture, how PHP works. Also, a good way to learn is to ask questions, so please do.