Protecting your files with a PHP Login Script
Getting Started
First, lets create the files we need. The password protect file itself is only one file, but we'll be styling the page as well. Create these files:
- protect.php - Protect Script
- index.php - File you want to protect
- style.css - Stylesheet
The PHP
Now lets write the important part of the script. The PHP will make the script function and define the necessary credentials needed to log in.
In your protect.php file, place this script at the top:
<?php 'demo' => 'demo', // Format is Username, then Password 'username' => 'password' // Each line is new user ); // CHANGE THIS- User will be redirected to this page after logout // Time out after _ minutes of inactivity. Set to 0 to not timeout // Timeout in seconds // Logout? } // Show login form function showLoginPasswordProtect($error_msg) { ?>
Basically, the script above defines user(s) who can login. If you want to add multiple users, just copy the line in the top where there are users and define some new usernames and passwords. Then there is a cookie timeout that you can change.
Great, now that we have the basic PHP all set up, lets write the HTML.
The HTML
Lets give our login form some style. Wrap your HTML around the form element, which is all you actually need (but its always nice if it looks good).
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="style.css" media="screen" /> </head> <body> <div id="header"> <div id="message"> </div> </div><!--header--> <div id="wrapper"> <div id="page"> <form method="post"> <input type="submit" name="Submit" value="Login" class="submit" /> </form> </div><!--page--> </div><!--wrapper--> </body> </html>
We aren't done with the basic markup yet. There's still a bit of PHP we need to add.
The Final PHP
We need to close our PHP functions, so let's place this code right after the HTML.
<?php // stop at this point } } // user provided password $pass = $_POST['access_password']; || (USE_USERNAME && ( !array_key_exists($login, $LOGIN_INFORMATION) || $LOGIN_INFORMATION[$login] != $pass ) ) ) { showLoginPasswordProtect("Incorrect password."); } else { // set cookie if password was validated // Some programs (like Form1 Bilder) check $_POST array to see if parameters passed // So need to clear password protector variables } } else { // check if password cookie is set showLoginPasswordProtect(""); } // check if cookie is good $found = false; foreach($LOGIN_INFORMATION as $key=>$val) { $lp = (USE_USERNAME ? $key : '') .'%'.$val; $found = true; // prolong timeout if (TIMEOUT_CHECK_ACTIVITY) { } break; } } if (!$found) { showLoginPasswordProtect(""); } } ?>
So, your markup for the protect.php page should go like this:
- PHP (Defining timeout and user credentials)
- The HTML
- Closing PHP
You may also have noticed that we link to the stylesheet (style.css) in the HTML. But before we start styling the CSS, lets make the images we need.
The Images
For the purposes of this page, I whipped up some simple images (with the submit button from our other tutorial: Creating Awesome Buttons with CSS and Image Sprites.
Here are the images I created:
- header-bg.jpg - Background for the Header
- body-bg.gif - Textured Body Background
- btn-green.png - Button Background for our other tutorial
- message.png - Access Denied Message
- logo.png - Sample Logo
Great! Now that we have the images all set, lets move on to the CSS.
The CSS
The stylesheet I wrote is pretty basic. Just copy the code below (or write your own CSS) and put it in style.css. If you have a question about any of the styles, feel free to ask a question in the comments.
* { margin: 0; padding: 0; } body { background: transparent url(images/body-bg.gif) repeat top left; font-family: Helvetica, Arial, sans-serif; font-size: 14px; color: #FFFFFF; } #header { background: #010101 url(images/header-bg.jpg) no-repeat top center; width: 100%; height: 150px; position: relative; clear: both; border-bottom: 1px solid #000000; } #header-wrapper { width: 960px; margin: 0 auto; } #message { background: transparent url(images/message.png) no-repeat top left; width: 540px; height: 80px; display: block; margin: 0 auto; position: relative; top: 35px; } #logo { background: transparent url(images/logo.png) no-repeat top left; width: 280px; height: 80px; display: block; margin: 0 auto; position: relative; top: 35px; } #wrapper { border-top: 1px solid #444444; position: relative; width: 100%; clear: both; } #page { width: 600px; margin: 20px auto; text-align: center; } input { -moz-border-radius: 4px; -webkit-border-radius: 4px; border-radius: 4px; -khtml-border-radius: 4px; background: #FFFFFF; border: none; margin: 5px 0; padding: 4px 6px; box-shadow: 0 2px 20px #141414; -moz-box-shadow: 0 2px 20px #141414; -webkit-box-shadow: 0 2px 20px #141414; font-size: 18px; } label { margin-right: 10px; font-size: 18px; } .submit { display: inline-block; background: transparent url(images/btn-green.png) repeat-x 0 0; border: 1px solid #1C3600; -moz-border-radius: 20px; -webkit-border-radius: 20px; border-radius: 20px; -khtml-border-radius: 20px; padding: 4px 8px; margin: 6px 0; color: #213E01; text-decoration: none; font-weight: bold; font-size: 16px; text-shadow: 0 -1px #6FD700; line-height: 18px; cursor: pointer; box-shadow: 0 1px 12px #141414; -moz-box-shadow: 0 1px 12px #141414; -webkit-box-shadow: 0 1px 12px #141414; } #error { background: #FFC1C1; border: 1px solid #B93B3B; color: #B93B3B; padding: 4px; width: 100%; }
Awesome! That takes care of the basic functionality of our login script.
Protecting a File
Let's say you want to protect your index.php file. All you need to do is include the protect.php script in the top of any document you want to protect.
Like so:
<?php include("protect.php"); // Just include this in any file you want to protect ?>
Questions? Concerns? Feedback?
I highly recommend checking out the demo and downloading the source files before asking questions. After that, I'd be glad to help you. Just post a comment below!
Internet Nerd, Design Freak, oh and Designmess Admin.
- 2803 reads
Sign up
Register for an account to be able to post, critique, upload designs, and much more. What are you waiting for? Join in on the fun!


















CommentsAdd your comment
offline
Member since 12/31/1969
131 posts
very very very useful.thanks Works perfect
offline
Member since 12/31/1969
131 posts
Great script, works perfect. But how to implement the logout feature? There's the line "define('LOGOUT_URL', 'http://designmess.com');" in protect.php but there's no action or button opting to logout.
offline
Member since 12/31/1969
131 posts
I'm new to php and trying to learn it quick because I'm designing a page for a client who wants users to be able to enter a password to access a personalized schedule. Would this script be able to be modified to direct a user to a certain page based on their username?
Post new comment