- No database needed;
- Each member can be redirected to a specified page;
- Members can be assigned to hierarchical levels;
- Editable guiding messages.
At least two pages are needed: the login page and a protected page. Usually there will be more protected pages, depending on your website structure.
I - The login page
The page name should be: login (using the Site Manager)
The file extension should be: php (using the Page Properties dialog)
First we make a login form.
1. Draw a Form Area and change the Form Properties to:
- Action: empty (remove all text)
- Method: POST
- Encoding type: empty (remove all text)
2. Put an Editbox onto the Form Area. Bring up its Editbox Properties dialog and change it to:
- Name: username
3. Put another Editbox into the Form Area. Bring up its Editbox Properties dialog and change it to:
- Name: password
- Password Field: Yes
4. Put a Push Button onto the Form Area. Bring up the Button Properties dialog and change it to:
- Button type: Submit
Second we create a Text object for the messages, preferably just above the form.
5. Draw a Text Object and insert:
Code: Select all
'.$message[$status].'
- Select Before Tag and insert:
Code: Select all
<?php echo '
Code: Select all
'; ?>
7. Bring up the Page HTML dialog, select Start of Page and insert:
Code: Select all
<?php
$member['John'] = array('pw'=>'1234' , 'level'=>2 , 'pp'=>'./red_page.php');
$member['Tim'] = array('pw'=>'pw33' , 'level'=>2 , 'pp'=>'./blue_page.php');
$member['Lisa'] = array('pw'=>'OhNo' , 'level'=>1 , 'pp'=>'./green_page.php');
$message[0] = 'Please log in.';
$message[1] = 'Bad login. Please try again.';
$message[2] = 'You have been logged out.';
# No edits beyond this line
session_start();
$status = 0;
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$username = isset($_POST['username']) ? $_POST['username'] : '';
$password = isset($_POST['password']) ? $_POST['password'] : '';
if (isset($member[$username]) && $member[$username]['pw'] == $password) {
$_SESSION['logged_in'] = true;
$_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
$_SESSION['level'] = $member[$username]['level'];
header('Location: ' . $member[$username]['pp']);
exit;
}
$status = 1;
}
elseif (isset($_SESSION['logged_in'])) {
unset($_SESSION['logged_in']);
unset($_SESSION['ip']);
unset($_SESSION['level']);
$status = 2;
}
?>
- Please study the first lines of the script and discover its structure.
- Each member has one line of data. If you create more member lines, be sure that each member has a unique name (case sensitive!).
- After 'pw'=> the members password comes.
- After 'level'=> the members level comes.
- After 'pp'=> the members protected page comes.
- What is the use of the 'level' option? Imagine you have two pages: staff and management. Then give all staff members level 1, and give all management members level 2. This way staff members can only visit their pages. But management members can visit both pages, because they have a higher level.
- If you don't want to use levels, set the level to 1.
- The lines holding $message[x] can be edited.
II - A protected page
Now we create one protected page. Before a page is sent, some checkes are done to be sure that the visitor is allowed to see the page.
- The page names must correspond with the protected pages (as defined in the main script).
- The file extension should be: php
1. Bring up the Page HTML dialog, select Start of Page and insert:
Code: Select all
<?php
$required_level = 1;
# No edits beyond this line
session_start();
if (!isset($_SESSION['logged_in'],$_SESSION['ip'],$_SESSION['level'])
|| $_SESSION['ip'] != $_SERVER['REMOTE_ADDR']
|| $_SESSION['level'] < $required_level ) {
header('Refresh: 5; url=./login.php');
echo '<b>You are not allowed for this page.</b><br>';
echo '<a href="javascript:history.back()">Go Back</a> or <a href="./login.php">Login</a>';
exit;
}
?>
Notes:
- For each page that you want to protect, these steps should be followed.
- The variable $required_level holds the required level for this page.
- The sentences can be edited or translated, but take care of the right syntaxis!
Download the example here
Updated
14-04-08 Simplified code for protected page (step II-1).
23-05-08 Added example download.