Javascript is very insecure - you can just view the source code to grab the password - so it will be best to use a server-side language, such as PHP.
Very simple example...
"admin-login.php" (include the PHP and HTML)
PHP Code:
<?php
session_start(); // must go right at the top of the page - to track login
$logins = array('myUsername' => 'myPassword'); // your username and password
if($_POST['Submit'] == 'Submit') { // check for form submit
$user = $_POST['user'];
$pass = $_POST['pass'];
if (isset($logins[$user]) && ($logins[$user] == $pass)) { // check login and compare credentials
$_SESSION['username'] = $user; // set the session
header('Location: another-page.php'); // redirect to protected page
}
}
?>
HTML Code:
<form name="loginform" method="post" action="admin-login.php">
Username :<input type="text" name="user" /><br />
Password :<input type="text" name="pass" /><br />
<input type="submit" name="Submit" value="Submit" />
</form>
Then in your protected page...
"another-page.php"
PHP Code:
<?php
session_start(); // must go right at the top of the page - to track login
if($_SESSION['username']) { // check that session
if($_POST['Logout'] == 'Logout') { // check for form submit
session_destroy(); // destroy the session to logout
header('Location: admin-login.php'); // redirect back to login page
}
?>
<!-- START - put all your content to be protected here -->
Another page.
<br/><br/>
<form name="logout" method="post" action="another-page.php">
<input type="submit" name="Logout" value="Logout" />
</form>
<!-- END - put all your content to be protected here -->
<?php } else {
echo "Access Denied";
header('Location: admin-login.php'); // redirect back to login page
}
?>
I've included a logout button for your convenience.
If you want to carry the login session across other pages of your website you must include
PHP Code:
<?php session_start(); ?>
right at the top and end with the .php extension too
Bookmarks