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);
?>
";`? – Prix Aug 15 '13 at 23:49