PX : code

Simple, Full, PHP/MySQL based login screen by Graham
Download this code


<?
if($username && $password) {
  mysql_connect() or die ("Whoops");
  $password = md5($password);
  $sql = "select * from login where username='$username'";
  $r = mysql_db_query("reg_users",$sql);

  if(!mysql_num_rows($r)) {
    header("Location: $SCRIPT_NAME");
  }
  $user = mysql_fetch_array($r);
  if($user["password"] == $password) {
    $password = serialize($password);
    setcookie("login_cookie","$username $password");
    $msg = "<meta http-equiv=\"Refresh\" content=\"0;url=./mainpage.php\">";
  }else{
     header("Location: $SCRIPT_NAME");
  }
}
if($msg) echo $msg;
?>
<html>
<title>Login to PHP Coders DB</title>
<body bgcolor="yellow" text="black">
<form method="post" action="<?echo $SCRIPT_NAME;?>">

<center><font size=+5><b>Welcome!</b></font></center>
<br>
<br>
<br>
<table cellspacing=0 cellpadding=0 width=320 align="center">
<tr><td>
Username:
</td><td>
<input name="username" type="text" width=10>
</td></tr>
<tr><td>
Password:
</td><td>
<input name="password" type="password" width=10>
</td></tr>
<tr><td colspan=2 align="center">
<input name="login" type="submit">
</td></tr>
</table>
</form>
</html>

<? /* Here is that include file code */ ?>

<?
if(!$login_cookie)
  header("Location: ./login.php");

if($phpcoders) {
  mysql_connect() or die ("Whoops");
  $user = explode(" ","$login_cookie");
  $sql = "select * from login where username='$user[0]'";
  $r = mysql_db_query("reg_users",$sql);

  if(!mysql_num_rows($r)) {
    header("Location: ./login.php");
    mysql_free_result($r);
  }

  $chkusr = mysql_fetch_array($r);
  mysql_free_result($r);
  if(unserialize($user[1]) != $chkusr[1])
    header("Location: ./login.php");
}
?>

Comments or questions?
PX is running PHP 5.2.17
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.