# Set page title and display header section.
$page_title = 'Register' ;
include ( 'includes/header.html' ) ;
# Check form submitted.
if ($_SERVER['REQUEST_METHOD']=='POST')
{
$csv = array();
$myfile = fopen("database.csv", "r") or die("Unable to open file!");
while (!feof($myfile))
{
$csv[] = fgetcsv($myfile);
}
fclose($myfile);
# Initialize an error array.
$errors = array();
# Check for an email address:
if(empty($_POST['email']))
{
$errors[] = 'Enter your email address.';
}
# Check for a password and matching input passwords.
if (!empty($_POST['pass1']))
{
if ($_POST['pass1']!=$_POST['pass2'])
{
$errors[]='Passwords do not match.';
}
}
else
{
$errors[]='Enter your password.';
}
# Check if email address already registered.
if(empty($errors))
{
$email = $_POST['email'];
foreach($csv as $value)
{
if($value[0] == $email)
{
$errors[] = 'Email address already registered.
<a href="login.php">Login</a>';
}
}
}
# On success register user inserting into 'users' database table.
if ( empty( $errors ) )
{
$myfile = fopen("database.csv", "a") or
die("Unable to open file!");
$email = $_POST['email'];
$password = $_POST['pass1'];
$salt = "bread";
$hashed_password = crypt($password,$salt);
$guesses=0;
$last_login = date('YmdHis');
$values = array($email,$hashed_password,$guesses,$last_login);
fputcsv($myfile, $values);
echo '<h1>Registered!</h1><p>You are now registered.</p><p>
<a href="login.php">Login</a></p>';
# Display footer section and quit script:
include ('includes/footer.html');
fclose($myfile);
exit();
}
# Or report errors.
else
{
echo '<h1>Error!</h1><p id="err_msg">The following error(s)
occurred:
<br>' ;
foreach ( $errors as $msg ){ echo " - $msg<br>" ; }
echo 'Please try again.</p>';
}
}
does not work if e-mail is in the first line of the database, but ok on subsequent lines.
I've added the who code in case there is some sort of logical error but not sure what it might be.
If anyone can spot anything please let me know.
Now I added this extra code it won't let me update it, says something about post being mostly code and have to add more details.
Was not sure if its something to do with the fgetcvs and fputcvs functions?