Here is how i have my files setup
index.php ( contains the login form )
<table width="980" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td id="header"></td>
</tr>
</table>
<table width="980" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td id="bar" style="padding-left:10px;"> </td>
</tr>
</table>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="login.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#000000">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="password" id="mypassword"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<table width="980" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#000000">
<tr>
<td height="34" align="center"><a href="policy.html">Warning</a> | <a href="ourescort.html">See our Girls</a> | <a href="contact.html">Phone</a> | <a href="rates.html">Rates </a></td>
</tr>
<tr>
<td height="66" align="center" class="f3">©All Rights Reserved </td>
</tr>
</table>
login.php ( contains the session and mysql code )
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="model"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:admin.php");
}
else {
echo "Wrong Username or Password";
}
ob_end_flush();
?>
And which Ever page i want to protect with a session i just put this code on line 1 before any html basically what this code dose is checks to see if the session is registered or not and if not it send them back to index.php( where the login form is).
<?php
session_start();
if(!session_is_registered(myusername)){
header("Location: index.php");
}
?>
And If needed here is my mysql code
--
-- Table structure for table `members`
--
CREATE TABLE `members` (
`id` int(4) NOT NULL auto_increment,
`username` varchar(65) NOT NULL default '',
`password` varchar(65) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
--
-- Dumping data for table `members`
--
INSERT INTO `members` (`id`, `username`, `password`) VALUES
(1, 'admin', 'model');