border image
logo border image







Your basket has: Total items: 0
Subtotal: $0.00




border image
 
corner image
corner image

Article


 Easy Password Protection for any page
After a brief search for examples of password protection code for a page, I found that my preferred solution was actually a combination of features by others. This script is a modified version of samples which offers a single password line of code to be installed into any page for protection.

Here is an easy way to provide password protection to any page. Save the following script into a file called "password_protect.asp". Put this page into the root of the application. Change the password in the file to the password of choice. Finally place this line of code into any page that requires protection.


<!-- #Include Virtual="Password_protect.asp" -->.


<%
    Response.Buffer=true
    Response.AddHeader "cache-control", "private"
    Response.AddHeader "pragma", "no-cache"
    Response.ExpiresAbsolute = #January 1, 1990 00:00:01#
    Response.Expires=Now()-1
    Response.AddHeader "Cache-Control", "must-revalidate"
    Response.AddHeader "Cache-Control", "no-cache"


'Set the password that is to be accepted


'*******************************
'Change your password here
    MyPWD = "guest"
'*******************************

'-----------------------------------------------------
'Get the page name for use in the script
'-----------------------------------------------------


strScriptName = Request.ServerVariables("SCRIPT_NAME")


'-----------------------------------------------------
'Perform check to see if password is submitted
'-----------------------------------------------------

'strPW1 <> ""
strPW1 = Request("MyPWD")
if strPW1 <> "" then
    Response.Cookies("MyPWD") = strPW1
end if

'-----------------------------------------------------
'Perform check to see if password is correct
'-----------------------------------------------------

'strPW2 <> MyPWD
strPW2 = Request.Cookies("MyPWD")
If strPW2 <> MyPWD Then
     Call PWDScreen()
End if

'-----------------------------------------------------
'Show password entry screen
'-----------------------------------------------------

Sub PWDScreen()
    %>

    <html>
    <head>

    </head>
    <body bgcolor="navy" text="gold">
    <div align="center">
    <form action="<%=strScriptName%>" method="POST">
    <h2>This page is access controlled.<BR>Please enter the password key<br></h2>
     <input type="password" name="MyPWD" size="10"><br><br>
     <input type="submit" value="Submit">
    </form>
    </div>
    </body>
    </html>

    <%
    response.end
End sub
%>


That's it. Name this page "Password_protect.asp". Put this page into the root of the application and put the following line of code into the page that needs protection.

< !-- #Include Virtual = "Password_protect.asp" -- >

Thanks to Ken Baumbach and Jonas Gorauskas for their example code which enabled this solution.

Rate this article
Low High
Return To Top
corner image
corner image