Hello all, I'm a financial planner who happens to tinker with PHP in my spare time. I've created the following login script. Let me know what the security issues and general stupidness of this code is. I'm a minimalist and I like to keep the code as simple as possible. I have no formal training in this.
The code is in two parts. The first is a pretty standard login script:
login.php
// login form
print "<form method=post action=\"userauthenticate.php\">";
print "<table>";
$aq = mysql_query("select * from players order by NAME asc");
// list all members
print "<center><br><table><tr><td align=right colspan=2>";
print "<b>Login</b><br>";
print "<tr><td><select name=userid style=\"width: 100%\"><option SELECTED>- Select Name -</option>";
while ($a = mysql_fetch_array($aq))
{ print "<option value='$a[NAME]'>$a[NAME]"; }
print "</select></td>";
print "<td>Name</td></tr>";
print "<tr><td><input type=password name=password size=20></td><td>Password</td></tr>";
print "</table><br>";
print "<input type=submit value=\"Log in\"></form>";
I use a pull down menu for the users (since there are only seven users). I'm sure its not totally smart to do it that way, but with non-important data (my racquetball league stats), I don't think it matters too much.
Next is the "bulk" of my code. I use include("userauthorize.php"); on every page I consider "members only". Here is the code for you guys to read.
userauthenticate.php
include("dbconnectionfile.php");
if (isset($userid) && isset($password))
{
$aq = mysql_query("select * from players where NAME = '$userid' and PASSWORD = '$password' ");
$a = mysql_fetch_array($aq);
if (mysql_num_rows($aq) > 0 ) // check info
{
setcookie("user", "$a[NAME]", time()+1209600, "", "www.sois.com") or die("Could not set cookie");
setcookie("sess", $PHPSESSID, time()+1209600, "", "www.sois.com") or die("Could not set cookie");
$sets = mysql_query("update players set SESSID = '$PHPSESSID' where NAME = '$userid' ") or die (mysql_error());
// tell user that he is logged in here
}
else // info incorrect
{
print "<h2>Wrong Password</h2>";
include("loginscript.php"); // send them back to login
} // end check info
}
// use the recently set cookie to "verify" player data and then use that stuff later
$aq = mysql_query("select * from players where SESSID = '$_COOKIE[sess]'");
$a = mysql_fetch_array($aq);
Like I said, if I want a page to be secure I just include this page. Here is an example of implementation:
samplememberpage.php
include("user authenticate.php");
// un comment this if this user has power to enter stats
$membercheck = $a[MEMBERSTATUS]; if ($membercheck < 1) { print "<center><h2>This page is for site league members only.</h2></center>"; exit(); }
// put member stuff here
So it's pretty simple, but I think it is going to get the job done. Feel free to put me down or suggest reasons why this is totally stupid. Thanks all!!!!!!