Add this login form to a page.
|
<form method="post" action="page.php">
<table> <tr><td>Username:</td><td><input type="text" name="username" /></td></tr> <tr><td>Password:</td><td><input type="password" name="password" /></td></tr> <tr><td align="center"><input type="submit" name="login" value="Log In" /></td></tr> </table> </form> |
page.php
|
<?php
session_start(); //Allows you to use sessions $user = "USERNAME"; //Username to protected page. $pass = "PASSWORD"; //Password to protected page. if($_POST['username'] == $user & & $_POST['password'] == $pass) //If you entered the username and password correctly. { $_SESSION['logged_in'] = "true"; $_SESSION['username'] = $_POST['username']; //Saves your username. echo('<meta name="refresh" content="0; url=page.php" />'); //Refreshes the page. } if(!isset($_SESSION['logged_in'])) //If you aren't logged in and you are trying to view the protected page. { echo('You are not logged in!'); } else { ?> PAGE CONTENT HERE <? } ?> |
If you want, you can add a logout page. Add a link to logout.php in page.php
|
<?php
session_start(); //Allows you to use sessions. session_destroy(); //Deletes all the sessions. echo('You are now logged out.'); ?> |
Finished.

10 Random PHP Tutorials :
10 Random LearnPHP.org Tutorials:



