Your First Script: A Password Protected Site

April 12th, 2008 by dtph

This is how I started learning PHP - writing a password protected site. This isn’t nearly as handy and advanced as your ordinary password protection, but it’s still cool, cooler than “Hello World“-programs anyways.

The first thing we do is to make a login-form in plain HTML, something like the following:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<title>Sample login</title>
</head>

<body>
<form action="passwordprotected.php" method="post">
<div>
<strong>Username:</strong> <input type="text" name="username"><br>
<strong>Password:</strong> <input type="password" name="password"><br>
<input type="submit" value="Log in">
</div>
</form>
</body>
</html>

Now, on this page there are several important things we have to know for our PHP-script. The first thing is the url/name of the PHP-file, in this case passwordprotected.php as seen in the <form>-tag. Remember, when you only write the filename of the PHP-file (as I have done) as the “action”, it has to be in the same directory as the html-page, the same goes for links etc.

The <form>-tag also holds another piece of valuable information, the method to send the data. There are two main methods that you will be using, post and get. The only real difference between those two is how you access the data in you PHP-script. Also, get will produce ugly addresses, while post won’t. There is a common misconception that post is more safe than get, but that is not true.

The last two things you have to know are the names of the username- and password-fields, if you look at the <input>-tags, you’ll see that the names are username and password.

Now you know all you need to know from the form, time to code some PHP at the next page.

What is PHP

March 28th, 2008 by dtph

I’m writing at this blog to teach other people PHP, but I figured it wouldn’t hurt explaining what PHP is.

PHP is a recursive acronym for Hypertext Preprocessor. The meaning of PHP might not tell you anything though. Have you ever wondered how you can write something in a textbox, press a button, and suddenly everybody can see what you wrote? How pages can constantly change even though you’re visiting the same address? In many cases, PHP is the reason for that.

PHP is a programming language you can use to make dynamic homepages. It can receive input from the user and make actions according to the input. It can store data, either in ordinary files, or in databases, for later use. I could go on about what you can do with PHP, but instead I’m going to say this - your imagination is (almost) the limit. You can even make images or flash animations with PHP.

Want to learn PHP? Keep reading my blog.

Before you start learning - the server

March 22nd, 2008 by dtph

PHP isn’t something you can write and then just run by double clicking the file (well you can, but please don’t think about that right now). I’m going to assume that most of you that read this article use Windows, but if you’re using something else (like myself - using ubuntu linux, which I recommend btw) and want help, feel free to message (write a comment or something) me.

Okay, let us get on with it. There’s three thing you need to write, execute and see PHP. First - an editor. You can write with the usual notepad, but I recommend something else, like Notepad 2. Downloading and installing that is pretty easy, you download Notepad 2, open the .zip-file and put the file Notepad2.exe wherever you want it.

Notpad 2 is for writing PHP, but to actually see the PHP in action you need a webserver and a browser (Opera, firefox, safari, IE etc.). If you wonder what a webserver is, it’s simply the program that sends you webpages you request. When you requested this page, you connected to another computer’s webserver to get the page.

Anyways, enough explaining what a webserver is.  If you’re using Windows, which I assume you do, the most convenient thing to do is to install Wamp. The installation shouldn’t be too hard, and when the installation’s done, it’s time to start programming PHP.

If you have questions, feel free to ask them to me and I’ll answer as soon as possible.