|
|
| Author |
Message |
asiammyself Guest
|
Posted: Sat Feb 28, 2004 5:52 am Post subject: password protect pages??? |
|
|
how do you password protect pages?  |
|
| Back to top |
|
 |
Smith Guest
|
Posted: Sat Feb 28, 2004 3:21 pm Post subject: Hey this is how |
|
|
You 1st have to have PHP. If you dont stop reading.
Ok since am just 14 and not good at PHP so much I can't give u the code. However the BEST site is www.hotscripts.com/PHP |
|
| Back to top |
|
 |
ReV[a]N
Joined: 27 Feb 2004 Posts: 3
|
Posted: Sat Feb 28, 2004 5:27 pm Post subject: PHP Authentication |
|
|
You might want to consider posting this in the help part of the forum not the Howtos section, but here is one way.
| Code: | /*********************************************************
**
** userAuth.php
**
*********************************************************/
/*********************************************************
**
** checkAuth(string "$username", string "$password");
**
** Takes the $username and $password variables
** and checks to see if they match the $PHP_AUTH
** variables.
**
*********************************************************/
function checkAuth()
{
/* Later you can add user permissions so just some users can view certain pages */
/* When you make your own database (if you use one) you can change the table names and field names to match your own database */
$sql = "select * from users where username='$PHP_AUTH_USER' AND password='$PHP_AUTH_PW'";
$query = mysqL_query($sql);
$row = mysqL_fetch_array($query);
$username = $row[username];
$password = $row[password];
if (!isset($PHP_AUTH_USER))
{
header('WWW-Authenticate: Basic realm="CPS Administration"');
header('HTTP/1.0 401 Unauthorized');
echo 'Authorization Required.';
exit;
}
else
{
/* If he has tried to log in but has failed and tries again */
if (($PHP_AUTH_USER != "$username") || ($PHP_AUTH_PW != "$password"))
{
header('WWW-Authenticate: Basic realm="CPS Administration: Incorrect Username/Password"');
header('HTTP/1.0 401 Unauthorized');
echo "Autorization Required.";
exit;
}
else
{
/* If the username and password were correct do nothing*/
break;
}
}
} |
| Code: | /*********************************************************
**
** index.php
**
*********************************************************/
/*********************************************************
**
** Common Files
**
*********************************************************/
require("common/userAuth.php");
/*********************************************************
**
** User Authentication
**
*********************************************************/
checkAuth();
/*********************************************************
**
** User Authentication
**
*********************************************************/
/* Put code here that runs when the user is logged in, $PHP_AUTH_USER is the variable to print the username of the person logged in */
echo "Yay The Page is Displaying!"; |
You can add more functions to userAuth.php, maybe add a user level (user, moderator, admin) or something else. |
|
| Back to top |
|
 |
Nocturnal
Joined: 28 Feb 2004 Posts: 3
|
Posted: Sat Feb 28, 2004 6:01 pm Post subject: |
|
|
You don't necessarily need PHP if you don't have to have secure. Actually, here's a pretty simple way to do a JavaScript one:
| Code: |
<html>
<head>
<title>Password Test</title>
</head>
<script>
function pass() {
var secretword = passwordtext.value;
//This grabs the value element from the input named "passwordtext"
if (secretword=='uberpie') {
location = 'passwordpass.html';
}
//Ok, if they have the right password it sends them to a page
if else {
var back = history.back();
alert('Beep! You got the password wrong!');
location = back;
}
//This sends the user to the last page.
//I can also see a more complex system that writes
//a hidden value keeping the user out of the same page
//for the duration of their visit
</script>
<form>
<input name = "passwordtext" value = "Password"></input><a href = "javascript:pass();">Login</a></form>
|
It may seem complex at first, but if you think about it, it isn't too hard. Also, you could just copy the pass function to say, user(); and add a user thing. Of course, if you want a community site, go with PHP(or perl is pretty cool too). |
|
| Back to top |
|
 |
che100
Joined: 28 Feb 2004 Posts: 20 Location: Lebanon
|
Posted: Sat Feb 28, 2004 11:35 pm Post subject: other scripts |
|
|
here use this little javascript promt that i wrote
the password is calicali. read the script an understand it
<script language="Javascript">
<!--
var password = "calicali";
pwd = window.prompt("This is a restricted area. Please enter password","");
if(pwd != password)
{
alert("Invalid Password. Access denied.");
location.href="index.htm";
}
if (pwd == password)
{
alert("You have been granted access to page.")
}
// -->
</script> |
|
| Back to top |
|
 |
RALLI//ART V3000
Joined: 29 Feb 2004 Posts: 3 Location: Wellington, New Zealand
|
|
| Back to top |
|
 |
|