11.4. Creating a Login Form

Now that you require authorization for a user to view administrative links, you need to build in the functionality that allows your administrators to log in and gain access to those links.

To do this, you first need to create a login form where a user can enter her credentials to request access to the administrative links.

A logical location to place your login form is at http://localhost/simple_blog/admin. For the moment, admin.php shows a blank page if the user hasn't logged in because authorization is required before the page will do anything at all. You can fix that by placing the login form at the bottom of admin.php, inside an else block. Doing so shows a login screen to anyone who isn't logged in already.

Your login form requests a username and password and uses the POST method to send this information to update.inc.php, along with a hidden input named action that passes the value, login.

At the bottom of admin.php, just after the closing </html> tag, modify the file with the code in bold:

</html>

<?php

/*
 * If we get here, the user is not logged in. Display a form
 * and ask them to log in.
 */
else:

?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <meta http-equiv="Content-Type"
        content="text/html;charset=utf-8" />
    <link rel="stylesheet"
        href="/simple_blog/css/default.css" type="text/css" />
    <title> Please Log In </title>
</head>

<body>

    <form method="post"
        action="/simple_blog/inc/update.inc.php"
        enctype="multipart/form-data">
        <fieldset>
            <legend>Please Log In To Continue</legend>
            <label>Username
                <input type="text" name="username" maxlength="75" />
            </label>
            <label>Password
                <input type="password" name="password"
                    maxlength="150" />
            </label>

<input type="hidden" name="action" value="login" />
            <input type="submit" name="submit" value="Log In" />
        </fieldset>
    </form>

</body>

</html>

<?php endif; ?>

Now you can navigate to http://localhost/simple_blog/admin to see your login form in action (see Figure 11-5).

Figure 11.5. Users not logged in see a login screen.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset