Page 1 of 1

Password protect web pages

Posted: Sun May 07, 2006 11:02 am
by Pablo
You can password protect your website using two methods:
1) Use authentication of your webserver
http://www.hwg.org/lists/hwg-servers/passwords.html

2) Using PHP scripting.
Here's a small example (your webhost must support PHP)
a. Create a form using Web Builder.
b. Set these properties:
Name: LoginForm
Method: POST
Action: <?php echo($HTTP_SERVER_VARS["PHP_SELF"]);?>
Encoding type: make this field empty
c. Add an editbox to the form for the username:
Name: username
d. Add an editbox to the form for the password:
Name: password
e. Add a button:
Name: Login
Type: submit
f. Open Page HTML and enter this code to Start of Page:
<?php
session_start();
if (!empty($HTTP_POST_VARS))
{
if ($HTTP_POST_VARS["password"] == "WebBuilderIsCool")
{
if (!isset($HTTP_SESSION_VARS["logged_in"]))
{
$username= $HTTP_POST_VARS["username"];
$password = $HTTP_POST_VARS["password"];
$logged_in = "YES";
session_register("username");
session_register("password");
session_register("logged_in");
header("Location: logged_on.html");
}
else
{
echo("<h2>Session expired!<br>Please try again later.<br></h2>");
exit;
}
}
else
{
echo("<h2>Invalid password!<br>You're not logged on.<br></h2>");
}
}
?>

g. Save this page as login.wbp and make sure the publish extension is php
h. Note that once the user has been succesfully logged on it will be redirected to logged_on.html (so make sure this page also exist)
i. Now on every page you want to protect insert this code in the Start of Page section:
<?php
session_start();
if ($HTTP_SESSION_VARS["logged_in"] != "YES")
{
header("Location: http://www.yourdomain.com/login.php");
exit;
}
?>

Posted: Tue Jun 13, 2006 10:21 pm
by collotcorp
In defense of newbies and those who don't talk PHP or MYSQL ( we are quite a few ) let me contribute this javascript provided from http://javascript.internet.com...


<!-- STEP ONE: Copy this code into the HEAD of your login HTML document -->


<HEAD>

<SCRIPT LANGUAGE="JavaScript">

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
function Login(){
var done=0;
var username=document.login.username.value;
username=username.toLowerCase();
var password=document.login.password.value;
password=password.toLowerCase();
if (username=="member1" && password=="password1") { window.location="page1.html"; done=1; }
if (username=="member2" && password=="password2") { window.location="page2.html"; done=1; }
if (username=="member3" && password=="password3") { window.location="page3.html"; done=1; }
if (done==0) { alert("Invalid login!"); }
}
// End -->
</SCRIPT>

<!-- STEP TWO: Paste this code into the BODY of your HTML document -->

<BODY>

<center>
<form name=login>
<table width=225 border=1 cellpadding=3>
<tr><td colspan=2><center><font size="+2"><b>Members-Only Area!</b></font></center></td></tr>
<tr><td>Username:</td><td><input type=text name=username></td></tr>
<tr><td>Password:</td><td><input type=text name=password></td></tr>
<tr><td colspan=2 align=center><input type=button value="Login!" onClick="Login()"></td></tr>
</table>
</form>