To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
Code CritiqueHaving someone critique your code is always a great way to hone the skills. Stop in and post your code to see what your peers may have done differently.
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
PHP Code:
<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>';
}
}
<?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 Code:
<?php
class security_functions
{
//defalt constructor
function security_functions()
{
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
Last edited by Chris Muench; 12-04-2003 at 10:07 PM.
Im a bit weary about this function "isLoggedIn() "
You are only checking if the user has a session registered. You should also check that the user session matches maybe a userid or username to make sure they are not only logged in but registered.
Also you must encrypt your passwords either by md5() which just hashes or there is mcrypt not sure on others
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 19,122
Quote:
Originally posted by planetsim Also you must encrypt your passwords either by md5() which just hashes or there is mcrypt not sure on others
There's also PHP's native support of SHA1, which happens to be what the NIST recommends, but that's a more recent addition.
__________________
On two occasions I have been asked [by Members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 19,122
Quote:
Originally posted by Chris Muench How do you dycrpt passwords inserted into the database (md5)
You can't. That's the idea.
__________________
On two occasions I have been asked [by Members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
Originally posted by planetsim Im a bit weary about this function "isLoggedIn() "
You are only checking if the user has a session registered. You should also check that the user session matches maybe a userid or username to make sure they are not only logged in but registered.
i disagree. if the user types the correct username and password, the session-variable is set. otherwise it is not.
consider this:
PHP Code:
$sql = "SELECT the, fields, you, need FROM your_table WHERE Username='".validiate_function($_POST['username'])."' AND Password='".md5($_POST["password"])."'";
$result = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($result) == 1) {
// the user is authenticated
$_SESSION['authed'] = 1;
} else {
echo "wrong username/password";
}
then, on top of every page that needs authentication, just check if $_SESSION['authed'] == 1.
checking a single session value is fine, as long as u only use it for authentication. whenever you call session_start() your session vars will overwrite any the user has passed in. just make sure that you're checking $_SESSION['user'] not just $user.
one thing: your redirects are incorrect: the location header requires a fully qualified url, e.g. http://www.example.com/index.php
Originally posted by daveyboy one thing: your redirects are incorrect: the location header requires a fully qualified url, e.g. http://www.example.com/index.php
in theory yes, but there's near total backward compatibiliy with HTTP/1.0 (that's a 1.1 thing)
__________________
there's no place i can be, since i found serenity.
Suppose should update my code.. I remember writing that function like when i first learnt i thought it was the best thing i made and never went and made it better.