Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 4 of 4

Thread: check for duplicate names in text file

  1. #1

    check for duplicate names in text file

    I've got a form that submits email addresses to a text file.
    I need the same script that appends these emails to this text file, to check for duplicate emails in the text file and do something if there is a duplicate.

    Here's the code I have now:
    PHP Code:
    $filename "emails.txt";

    $fd fopen ($filename"a") or die ("Can't open $filename");
    $fname $email;
    $fout fwrite ($fd"$fname\n");
    fclose($fd);

    header("Location: index.php?msg=success");
    exit(); 
    So i've already got the emails adding to the text file separate by new lines. I just need some type of if/else to check if an email is already in there, and if not, add the email. I started reading up on explode but it's the end of the day and I'm fried

    Thanks.

  2. #2
    1,839
    posts
    Registered User
    just do a file read on it and store the file as a string and use preg_match to see if the email is found

  3. #3
    so what I guess you can do is read in the file and then explode and then run in_array something like ...
    PHP Code:
    <?php
    $file 
    "emails.txt";
    $handle fopen($file"r");
    $contents fread($handlefilesize($file));
    fclose($handle);

    $list = array();

    $list explode("\n",$contents);
    $list array_map("trim"$list);

    $current "foo@bar.com";

    echo 
    in_array($current,$list) ? $current.' exists' $current.' does not exist';
    ?>
    would probably work
    Let us live so that when we come to die even the undertaker will be sorry. - Mark Twain
    Don't PM me your CSS, xHTML, JS or PHP questions. I will not reply to ANY IE6 questions.

  4. #4
    Thanks Simp, that did the trick!

    Here's what i ended up with:
    PHP Code:
    <?php

    if (empty($_POST['email']) || empty($_POST['freq']) || empty($_POST['choice1']) || empty($_POST['choice2']) || empty($_POST['choice3']))
    {
    header("Location: index.php?msg=not_enough_info");
    exit();
    }


    if (!
    preg_match("/[a-zA-Z0-9_\\.-]+@[a-zA-Z0-9_\\.-]+/"$_POST['email']))
    {
        
    header("Location: index.php?msg=invalid_email");
        exit();
    }


    $email $_POST["email"];
    $freq $_POST["freq"];
    $choice1 $_POST["choice1"];
    $choice2 $_POST["choice2"];
    $choice3 $_POST["choice3"];

    $date date('l F d, Y - h:i:s A');


    //check to see if email already exists.

    $filename "emails.txt";

    $handle fopen($filename"r");
    $contents fread($handlefilesize($filename));
    fclose($handle);

    $list = array();

    $list explode("\n",$contents);
    $list array_map("trim"$list);

    $current $email;

    if (
    in_array($current,$list)){ 
        
    header("Location: index.php?msg=already_in_db");
        exit();
        
    }else{

    $fd fopen ($filename"a") or die ("Can't open $filename");
    $fname $email;
    $fout fwrite ($fd"$fname\n");
    fclose($fd);

    //send email

    $body "Sent from $email \n\n Frequency of Event: $freq \n\n Choice 1: $choice1 \n\n Choice 2: $choice2 \n\n Choice 3: $choice3 \n\n";


    $sent mail("addresshere","Survey Submission","$body");

    if (
    $sent)
    {
        
    header("Location: index.php?msg=success");
        exit();
    }
    else
    {

        
    header("Location: index.php?msg=sending_failed");
        exit();
    }

    }

    ?>
    One more thing... Should I worry about any injection to the file? Is there a good way to guard against it? Should i be checking for special characters or something?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012