Funk Forty Niner
Funk Forty Niner

Reputation: 74219

Confirm email with activation link then store in file if valid PHP

I realize that this question has been asked many times, but I've yet to find a flatfile version.

I'm looking for a way to achieve the following in PHP or CGI-PERL, preferably PHP:

  1. A person submits their email address via a form.
  2. The email is then stored (temporarily) in a flatfile database (temp_emails.txt)
  3. The person then receives an auto-reply email with a link to confirm their email address.
  4. Upon confirming by clicking the link, their email address is then saved to a new file.

(confirmed_emails.txt for example).

One way that I've thought of, is some form of "opt-in" method.

Is this possible without SQL or a similar database?

Upvotes: 1

Views: 547

Answers (2)

andrewsi
andrewsi

Reputation: 10732

I think you can probably manage this without storing anything, even in a text file.

When you generate the unique code that you're emailing to the user for confirmation, create it by hashing the email address that you're sending it to, and have the link of the format:

http://www.example.com/confirm.php?email=<emailaddress>&confirm=<hashedemail>

Then just re-hash the email address from the link against the hash in the link, and if they match, you've confirmed the email address.

(Though I'm very curious as to why you don't want to store it in a database - presumably you'll need to store the email addressed in one so you can do something useful with them?)

Upvotes: 1

Marcus Recck
Marcus Recck

Reputation: 5063

It's possible.

When writing the email to the temp_emails.txt file open it like such:

$f = fopen('temp_emails.txt', 'a+'); // open for read/write at end of file
fwrite($f, $email); // write the email to the file
fclose($f); // close it
// send new email

They get their link [email protected]

// first check if they already confirmed it
$file = file('confirmed_emails.txt');
if(!in_array($email, $file)){
    // not in file
    $f = fopen('confirmed_emails.txt', 'a+');
    fwrite($f, $email);
    fclose($f);
}else {
    // already confirmed
}

Upvotes: 3

Related Questions