Pretty simple.
Create a login page with this code on it:
Code: Select all
// Login Page
<form method=post action=page2.php>
<input type=text name="username" value="username">
<input type=password name="password">
<input type=submit name="submit">
Then send it to a processing script:
Code: Select all
<?
//If this post is submitted
if($_POSTї'submit']) {
//Check password against selected password (Change "mypassword" to a password of your own...
if($_POSTї'password'] != "mypassword" and $_POSTї'username'] != "myusername"){
die("This username and password combo is incorrect!");
}
//If the password turns out to be right, set cookies
setcookie("LoginUsername" , "myusername");
setcookie("LoginPassword" , "mypassword");
header("Location: forms.php");
}
?>
Then, on the top of each page that you want to be protected:
Code: Select all
<?
//Verify that the user has cookies equal to the username and password you selected
if($_COOKIEї'LoginUsername'] == "myusername" and $_COOKIEї'LoginPassword'] == "mypassword") {
echo "Your page here...";
} else {
die("username and pass are incorrect!");
}
?>
Now to test and make sure this is accurate..
Yep, it works. All right, these are the things in the script you need to change....
Page2.php
Change the "mypassword" and "myusername" to a password and username of your choice in the following code:
Code: Select all
$_POSTї'password'] != "mypassword" and $_POSTї'username'] != "myusername"
Also change the following "myusername" and "mypassword" to the username and password you selected in the above in this code:
Code: Select all
setcookie("LoginUsername" , "myusername");
setcookie("LoginPassword" , "mypassword");
And change the location of the "header" in this code. Basically, type in the URL of the page you would like your user to see after they log in. It's in this code:
And then, the last thing to change is in the code you put on every page.
Once again, just change the "myusername" and "mypassword" to the pass and username you selected.
Code: Select all
if($_COOKIEї'LoginUsername'] == "myusername" and $_COOKIEї'LoginPassword'] == "mypassword")
As a side note, this isn't the most secure way to do this. What you could do is offer each user his/her own username and verify it against a MySQL DB. If you need help with that, just ask.
Hope this helps!