<?
session_start();
$dbh = new PDO("mysql:host=localhost;dbname=mmaaaco_cjorgensen", "mmaaaco_cjorgen", "TD}*;vhNWkT$");
$email = $_POST['uname'];
$submit = $_POST['submit'];
if ($submit == 'Submit'){
$password = md5(md5( md5(trim($_POST['pword']))));
if ($password){
$sql = $dbh->prepare("select id from users  where email =? and credentials = ? and active = '1'");
$sql->execute(array($email,$password));
$match = 0;
while ($row = $sql->fetch()){
	$id = $row['id'];
$match++;	
}
if ($match > 0) {
$_SESSION['approved'] = 'yes';
$_SESSION['id'] = $id;
echo '<script>
var exdate=new Date();
var exdays = 10000;
exdate.setDate(exdate.getDate() + exdays);
var value = '.$id.'; 
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie="memberid=" + c_value</script>'; 


$upsql = $dbh->prepare("update users set lastlogin = now() where email = ?");
$upsql->bindValue(1,$email);
$upsql->execute();


}
else
{
echo 'try it again!';	
}
}
}
 ?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Login</title>
</head>
<body>
<form action = "login.php" method = "post">
<?
$memberid = $_COOKIE['memberid'];
$sql= $dbh->prepare("select email from users where id = '$memberid'");
$sql->execute();
$sqlrow = $sql->fetch();
$email = $sqlrow[0]; 

?>

Email<input type = "text"  id ="email"  name="uname" value="<?= $email;?>"><br/>
Password <input type = "text" name="pword" /><br/>
Remember Me <input type = "checkbox" name = "remember" id = "remember" value = "" />
<input type = "submit" name = "submit" value = "Submit"/>
</form>
</body>
</html>