Help with PHP members-only pages?

Dragon

New member
Hello all,
I am relatively new to PHP and just finished up coding a basic login system with the aid of a very good little tutorial- but now I need to make certain pages visible only to a member once they have successfully logged in.

Code:

//checklogin.php

<?php
$host="hostname";
$username="username";
$password="password";
$db_name="daname";
$tbl_name="tablename";
//I left out my actual host and what not on purpose- the information is filled in on the actual code

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

$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);

$count=mysql_num_rows($result);

if($count==1){

session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

//main_login.php

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<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></P>

//login_success.php

<?
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>
//this is at the top of a css/html page for the successful login

//logout.php

<?
session_start();
session_destroy();
?>
//this is at the top of a css/html page for the successful logout

I don't know if there is a problem with the php I have already coded, but I have tried several different methods and have been unable to successfully make a members-only page.

Help is much appreciated!
~The Paper Dragon
 
Back
Top