<?
session_start();
include 'dbconnect.php';
$submit = $_POST['submit'];
if ($submit == 'Register'){
	$captcha = $_POST['captcha'];
	if ($captcha != $_SESSION['captcha'] ){
	header("Location: http://davidrtello.com	");
	}
	
	
$fullname = $_POST['fullname'];
$email = $_POST['email'];	
//check
$checksql = $dbh->prepare("select id from users where email = ? "); 
$checksql->bindvalue(1,$email);
$checksql->execute();
$nummembers = 0;
while ($row = $checksql->fetch()){
	$nummembers++;
	}
if ($nummembers > 0){
	echo 'We are sorry, it appears your email is already in our database. Please click the following link to reset your password : <a href = "resetpw.php">Reset</a><br/>';  
	exit();
	}

$pass =makepass();
echo "unencrypted pass is $pass";
$encpass = md5(md5(md5($pass)));
$ins = $dbh->prepare("insert into users   (fullname,email,credentials,signupdate) values (?,?,?,now())");
$ins->execute(array($fullname,$email,$encpass));
$content = 'Welcome to my site. Your username is  '.$email.' and your initial password is '.$pass."\n";
$link = base64_encode($email);

$link = 'http://mm214.com/wdm3304materials/wdm3304week5/members/activate.php?link='.$link;  
$content .= 'Please click on the following link to activate your account .'.$link;
$bool = mail ($email,'Membership to mm214.com',$content);
if ($bool) echo  'Thank you, check '.$email.' for instructions '; 

}

//connect to the db
//get data & validate
//insert into table
//create link for user to activate
//print message


/*
CREATE TABLE IF NOT EXISTS `users` (
  `id` int(5) NOT NULL auto_increment,
  `fullname` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `credentials` varchar(255) NOT NULL,
  `signupdate` datetime NOT NULL,
  `lastlogin` datetime NOT NULL,
  `active` int(11) NOT NULL);
  
  select email from users where email = ?
    */

?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<form action = "register.php" name = "register" method = "post" class="pure-form pure-form-stacked">
Fullname: <input type = "text" name = "fullname" required/><br/>
Email:<input type = "email" name = "email" required/><br/>
<img src = "captcha.php"/><br/>
  Enter the code you see above: <input type = "text" name = "captcha"/> <br/> 
<input type = "submit" name = "submit" value ="Register" class="pure-button pure-button-primary"/>
</form>
</body>
</html>
