Sick of having a site that's composed of 50 PHP scripts? In this article we will learn the concepts of multipurpose pages with PHP -- a simple yet powerful idea.
Developing Multipurpose Pages With PHP - Multipurpose Web Pages (Page 2 of 4 )
What are multipurpose pages? Put simply, they are dynamic pages that can display completely different content if a certain condition (or conditions) is met. There are both advantages and disadvantages to these types of pages:
Advantages:
Smaller number of pages to keep track of
Less space is taken up on your server
There are less changes to make when you redesign
Disadvantages:
Pages can become very long (number of lines)
Processing time is slowed slightly due to a large number of variable checkings
Overall, I believe that the advantages far outweigh the disadvantages. Isn't it a lot easier to keep track of one index page, instead of two? Also, doesn't one 400-line page take up less space than two 300-line pages? For me, that's usually the case, because I always limit the width of a line to the width of my screen, so I never scroll, and all lines are essentially the same length.
If you use CSS, then redesigning is even easier. You can easily setup a layout of the whole page and just jump into PHP when you want to have changing content, like a login box that is replaced by one of those "Welcome, someone, you last visited..." dialogs.
Let me show you a tiny example of a multipurpose page in action. Let's say that Bob has a page with news, and a little log-in box on the right side. He has used the concepts I will present to you -- and explain with greater detail -- in this article to create his page.
Here's Bob's source code:
<html> <head> <title>Bob's Login Site</title> <body bgcolor="#00cc00"> <?php if ($_POST['login'] == "do") { $name = $_POST['name']; $pass = $_POST['password']; $db_cnct = mysql_connect("localhost", "user", "pass") or die("Could not connect to the database!"); $db = mysql_db_select("users", $db_cnct); $sql = "SELECT user, pass FROM users WHERE user = \"$name\" and pass = \"md5($pass)\""; // This is for an encrypted password-protection database $check_user = mysql_query($sql); $users = mysql_num_rows($check_user); if ($users < 1) { echo ("<center>Invalid Password!</center>"); echo ('<form action="'.$PHP_SELF.'" method="post">'); echo ('<input type="text" name="name"><br />'); echo ('<input type="password" name="password"><br />'); echo ('<input type="hidden" name="login" value="do">'); echo ('<input type="submit" value="Log-in">'); echo ('</form>'); } else { echo ("Welcome, $name!"); } } else { echo ('<form action="'.$PHP_SELF.'" method="post">'); echo ('<input type="text" name="name"><br />'); echo ('<input type="password" name="password"><br />'); echo ('<input type="hidden" name="login" value="do">'); echo ('<input type="submit" value="Log-in">'); echo ('</form>'); ?> </body> </html>