Chris Muench
12-04-2003, 10:01 PM
Are their any major problems you notice with this code: (I have many files because this is just a start to the re write of my program) I just need to know if their are any security issues or bad coding.
login.php
<html>
<head>
</head>
<body>
<?php
session_start();
include ("config.php");
include ("classes/db_functions.php");
include ("classes/security_functions.php");
//create two objects that are needed in this script
$dbf=new db_functions($cfg_server,$cfg_username,$cfg_password,$cfg_database);
$sec=new security_functions();
if(isset($_POST['username']) and isset($_POST['password']))
{
$username = $_POST['username'];
$password = $_POST['password'];
if($sec->checkLogin($dbf,$username,$password))
{
$_SESSION ['user'] = $username;
header ("location: index.php");
}
else
{
echo '<center>username or password are incorrect</center>';
}
}
elseif($sec->isLoggedIn())
{
header ("location: index.php");
}
else
{
?>
<center>
<form name="login" action="login.php" method="POST">
<table width="348" border="0" cellspacing="2" cellpadding="0">
<tr>
<td width="85">
Username:
</td>
<td>
<input type="text" name="username" size="24">
</td>
</tr>
<tr>
<td width="85">
Password:
</td>
<td>
<input type="password" name="password" size="24">
</td>
</tr>
</table>
<input type="submit" name="Submit" value="Log In">
</form>
</center>
<?php
}
?>
</body>
</html>
index.php
<?php
session_start();
include ("config.php");
include ("classes/db_functions.php");
include ("classes/security_functions.php");
$dbf=new db_functions($cfg_server,$cfg_username,$cfg_password,$cfg_database);
$sec=new security_functions();
if(!$sec->isLoggedIn())
{
header ("location: login.php");
}
?>
db_functions.php
<?php
class db_functions
{
//class variable that represents the database connection.
var $conn;
//user-defined constructor
function db_functions($server,$username,$password,$database)
{
//pre: parameters must be correct in order to connect to database.
//post: connects to database.
$this->conn = mysql_connect("$server", "$username", "$password") or die("Could not connect : " . mysql_error());
mysql_select_db("$database",$this->conn) or die("Could not select database <b>$database</b>");
}
}
?>
security_functions.php
<?php
class security_functions
{
//defalt constructor
function security_functions()
{
}
function isLoggedIn()
{
if(isset($_SESSION['user']))
{
return true;
}
return false;
}
function checkLogin($dbf,$username,$password)
{
//pre: $dbf must be a db_functions object and $username and password must be strings
//post: returns boolean based on if their login was succesfull.
$result = mysql_query ("SELECT * FROM users WHERE username='$username' and password='$password'",$dbf->conn);
$num = @mysql_num_rows($result);
if($num > 0)
{
return true;
}
return false;
}
}
Thanks,
Chris Muench
login.php
<html>
<head>
</head>
<body>
<?php
session_start();
include ("config.php");
include ("classes/db_functions.php");
include ("classes/security_functions.php");
//create two objects that are needed in this script
$dbf=new db_functions($cfg_server,$cfg_username,$cfg_password,$cfg_database);
$sec=new security_functions();
if(isset($_POST['username']) and isset($_POST['password']))
{
$username = $_POST['username'];
$password = $_POST['password'];
if($sec->checkLogin($dbf,$username,$password))
{
$_SESSION ['user'] = $username;
header ("location: index.php");
}
else
{
echo '<center>username or password are incorrect</center>';
}
}
elseif($sec->isLoggedIn())
{
header ("location: index.php");
}
else
{
?>
<center>
<form name="login" action="login.php" method="POST">
<table width="348" border="0" cellspacing="2" cellpadding="0">
<tr>
<td width="85">
Username:
</td>
<td>
<input type="text" name="username" size="24">
</td>
</tr>
<tr>
<td width="85">
Password:
</td>
<td>
<input type="password" name="password" size="24">
</td>
</tr>
</table>
<input type="submit" name="Submit" value="Log In">
</form>
</center>
<?php
}
?>
</body>
</html>
index.php
<?php
session_start();
include ("config.php");
include ("classes/db_functions.php");
include ("classes/security_functions.php");
$dbf=new db_functions($cfg_server,$cfg_username,$cfg_password,$cfg_database);
$sec=new security_functions();
if(!$sec->isLoggedIn())
{
header ("location: login.php");
}
?>
db_functions.php
<?php
class db_functions
{
//class variable that represents the database connection.
var $conn;
//user-defined constructor
function db_functions($server,$username,$password,$database)
{
//pre: parameters must be correct in order to connect to database.
//post: connects to database.
$this->conn = mysql_connect("$server", "$username", "$password") or die("Could not connect : " . mysql_error());
mysql_select_db("$database",$this->conn) or die("Could not select database <b>$database</b>");
}
}
?>
security_functions.php
<?php
class security_functions
{
//defalt constructor
function security_functions()
{
}
function isLoggedIn()
{
if(isset($_SESSION['user']))
{
return true;
}
return false;
}
function checkLogin($dbf,$username,$password)
{
//pre: $dbf must be a db_functions object and $username and password must be strings
//post: returns boolean based on if their login was succesfull.
$result = mysql_query ("SELECT * FROM users WHERE username='$username' and password='$password'",$dbf->conn);
$num = @mysql_num_rows($result);
if($num > 0)
{
return true;
}
return false;
}
}
Thanks,
Chris Muench