3
votes

I'm coding a basic maillist system for our website. The "subscribe.php" page uses the $_GET method for parameters. I add the email addresses in a text file (maillist.txt).

Before adding the address, I check that it's not in the file yet.

Problem: comparing two identical strings returns false..

What I've tried:

  • I made sure that maillist.txt is in UTF-8
  • I tried setting header in UTF-8
  • I tried using strcmp()
  • I tried converting both strings with utf8_encode

Here is the "subscribe.php" code: (I erased all regex and isset checks)

<?php
    // UTF-8 ----> things I've added, trying to solve the problem
    header('Content-Type: text/html; charset=utf-8');
    ini_set('default_charset', 'utf-8');
    ini_set("auto_detect_line_endings", true);

    $email = strip_tags($_GET['email']); // For safety

    $maillist = fopen('maillist.txt', 'r+');

    // Check if email is already in the database
    $insert = true;
    while ($line = fgets($maillist)) {
        $line = rtrim($line, "\r\n");
        if (utf8_encode($line) == utf8_encode($email)) { // $line == $email returns false
            echo $line . "=" . $email . "<br/>";
            $insert = false;
            break;
        } else echo $line . "!=" . $email . "<br/>";
    }

    if ($insert) {
        fputs($maillist, $email . "\n");
        echo 'Success';
    } else echo "Fail";

    fclose($maillist);
?>
3
You might want to close the double quote else echo "Fail; <--- thereSterling Archer
What it shows to you when you do echo $line . "!=" . $email . "<br/>";?Prix
+1 for clearly saying what you have tried.Jordan
@Prix : it shows "[email protected][email protected]".. PRPGFerret : I edited. That mistake comes from the edit I made to post the code here.user2154283
Without having access to the resources themselves i can't really give a good guess. But for the sake of debugging, have you tried filter_var($line, FILTER_SANITIZE_EMAIL); to filter out any non-email valid chars. If that doesn't work, try making a hash of the line and hash of the email, and compare the hashes on an email/line combination that SHOULD match but isn't (might need to add an incrementing line counter, so you can specify a line of the file at which it should make a hash and output them)Lee

3 Answers

0
votes

taking a shot in the dark here...

  1. first, store your values as variables and reuse those, you may be printing different stuff than you're comparing.

  2. trim those variables to make sure there aren't any extraneous whitespaces before or after.

    while ($line = fgets($maillist)) {
        $line = rtrim($line, "\r\n");
    
        //the two variables you want to compare
        $lineValue = trim(utf8_encode($line));
        $email     = trim(utf8_encode($email));
    
        //compare them them out
        if ($lineValue == $email) {
            echo $lineValue . "==" . $email . "<br/>"; //compare the trimmed variables
            $insert = false;
            break;
        } else {
            echo $lineValue . "!=" . $email . "<br/>";
        }
    }
    

that may not even be your problem, but its a good place to start if you're seeing the same string with your eyes..

0
votes
  1. You need to store your values as variables.

  2. use trim those variables to make sure any extra white spaces before or after.

    while ($line = fgets($maillist)) {
        $line = rtrim($line, "\r\n");
    
        //the two variables you want to compare
        $lineValue = trim(utf8_encode($line));
        $email     = trim(utf8_encode($email));
    
        //compare them them out
        // "===" means "Identical" True if x is equal to y, and they are of same type
        if ($lineValue === $email) {
            echo $lineValue . "==" . $email . "<br/>"; //compare the trimmed variables
            $insert = false;
            break;
        } else {
            echo $lineValue . "!=" . $email . "<br/>";
        }
    }
    
0
votes

To summarize everything that was said :

The problem was basically that I didn't filter special email characters, so I fixed it by filtering the variables with filter_var($line, FILTER_SANITIZE_EMAIL);