Password Protect

Password ProtectAdd this login form to a page.

<form method="post" action="page.php">
<table>
<tr><td>Username:</td><td><input type="text" name="username" /></td></tr>
<tr><td>Password:</td><td><input type="password" name="password" /></td></tr>
<tr><td align="center"><input type="submit" name="login" value="Log In" /></td></tr>
</table>
</form>

page.php

<?php
session_start(); //Allows you to use sessions
$user = "USERNAME"; //Username to protected page.
$pass = "PASSWORD"; //Password to protected page.
if($_POST['username'] == $user & &  $_POST['password'] == $pass) //If you entered the username and password correctly.
{
$_SESSION['logged_in'] = "true";
$_SESSION['username'] = $_POST['username']; //Saves your username.
echo('<meta name="refresh" content="0; url=page.php" />'); //Refreshes the page.
}
if(!isset($_SESSION['logged_in'])) //If you aren't logged in and you are trying to view the protected page.
  {
  echo('You are not logged in!');
  }
else
    {
?>
PAGE CONTENT HERE
<?
    }
?>

If you want, you can add a logout page. Add a link to logout.php in page.php

<?php
session_start(); //Allows you to use sessions.
session_destroy(); //Deletes all the sessions.
echo('You are now logged out.');
?>

Finished.


Related Articles

  • Password Protection
    Although there are many uses for a .htaccess file, by far the most popular, and probably most useful, is being able to reliably password protect directories on websites. Although JavaScript can also be used to do this, only .htaccess has total security, because someone must know the password to get ...
  • JavaScript Password Protection
    OK, I finally got around to writing the javascript password protection tutorial. You will see the example script here is pretty straightforward, but it is also pretty easy to get around. Ill tell you why and give you links to more secure scripts later in this tutorial.
  • Random Password Generator
    Random password generator.
  • Protect Your Domain Name
    Domain names can be registered in increments of 1, 2, 5 or 10 years. The domain remains yours AS LONG AS it is reregistered prior to its expiration date. If you let the registration lapse, someone else can quickly register it and make it their own.
  • Database Connection Strings
    Although our ASP database tutorials all use MS Access connection strings, there are many sorts of database software out there for use with ASP. This tutorial will show you how to connect with them...
  • Random Password Generation
    Web programmers will enjoy this article. It is a quick and effective way to create Random Password Generation - needed for security and anti-spam measures. The author provides concise lines of code, screenshots and simply read texts.
  • Introduction to Htaccess
    What is
  • Domain Names - Free Privacy - How to Protect your Privacy When You Register a Domain Name
    Are you worried about your privacy online when starting a website? You should be. When you register domain names - free privacy settings from your registrar can protect you from spam, prying eyes, or worse.
  • Is Your Domain Name Safe?
    Your domain name is registered somewhere out in cyberspace through an authorized domain name registrar. Do you know which one? Do you know who is listed as the registrant / owner of your domain names?
  • Stop Right Mouse Clicks
    Stop people from getting at your source by right clicking on your web page. Sounds good, but doesnt protect you totally, you can never fully protect your work, but it will make it harder.

Contact Web Design Outsource and get started today

Need Website Designing, Development, Redesigning, Maintenance and SEO services or help growing your company's web presence? Request a free Quote Now.