-1
# 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?

  • Not related to your problem, but you should really use a database for this. – jeroen Nov 28 '18 at 09:23
  • can you add a sample of your database.csv? – yasoh Nov 28 '18 at 09:26
  • format of each line in the file: email,password – frankiemousecatcher Nov 28 '18 at 09:42
  • part of the specification is to use a flat file rather than a database – frankiemousecatcher Nov 28 '18 at 09:43
  • i believe the problem lies within your database.csv file, can't reproduce the problem with your code and given format for the csv. – yasoh Nov 28 '18 at 09:45
  • when I view the database in notepad++ everything looks OK – frankiemousecatcher Nov 28 '18 at 09:47
  • Start by doing a var_dump of your $csv array to see what it actually contains. – misorude Nov 28 '18 at 09:47
  • if I just add an empty line at the beginning of the database or a line saying email,password it works OK, if I start with an empty file it does not work – frankiemousecatcher Nov 28 '18 at 09:48
  • _“if I start with an empty file it does not work”_ - what would you expect it to do in that case? What would “working” look like, when you use an empty input file? And what does the “empty file” test case have to do with the problem you originally described? – misorude Nov 28 '18 at 09:53
  • After reading your comments from one of the answers below i started to feel like your code has a logical problem,please edit the original post and add the parts where you have sample of database.csv and registeration. – yasoh Nov 28 '18 at 10:21
  • I've now tried with multiple csv files saved in different formats, empty or not i cannot reproduce your problem with your given full code. It could be that it's because i'm running it from shell with pre-entered values and not an actual form to post data into this php code. With your code, it will add the email into the csv if it doesn't exist there and if it did it gave the correct error, the csv being empty or not. – yasoh Nov 28 '18 at 14:25
  • is it possible the browser is caching a copy of the file, I'm running a local server on my computer for testing – frankiemousecatcher Nov 28 '18 at 15:33

1 Answers1

0

sir i tested it is working fine no problem there. you can show by this way

foreach($csv as $value)
{
    echo $value[0];
    if($value[0] == $email)
    {

        $errors[] = 'Email address already registered. <a 
        href="login.php">Login</a>';
    }
}   
  • when empty: array (size=1) 0 => array (size=1) 0 => string '' (length=3) after first entry: array (size=2) 0 => array (size=4) 0 => string 'test@domain.com' (length=18) 1 => string 'bruv7wxzBISVw' (length=13) 1 => boolean false – frankiemousecatcher Nov 28 '18 at 09:58
  • it does not match test@domain.com, if I try to register it will register it again on the second line, but works from 2nd line onwards OK, correctly matches – frankiemousecatcher Nov 28 '18 at 10:04