php - String compare returns false -
i'm coding basic maillist system our website. "subscribe.php" page uses $_get
method parameters. add email addresses in text file (maillist.txt).
before adding address, check it's not in file yet.
problem: comparing 2 identical strings returns false..
what i've tried:
- i made sure maillist.txt in utf-8
- i tried setting header in utf-8
- i tried using strcmp()
- i tried converting both strings utf8_encode
here "subscribe.php" code: (i erased regex , isset checks)
<?php // utf-8 ----> things i've added, trying solve 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']); // safety $maillist = fopen('maillist.txt', 'r+'); // check if email in 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); ?>
taking shot in dark here...
first, store values variables , reuse those, may printing different stuff you're comparing.
trim variables make sure there aren't extraneous whitespaces before or after.
while ($line = fgets($maillist)) { $line = rtrim($line, "\r\n"); //the 2 variables want compare $linevalue = trim(utf8_encode($line)); $email = trim(utf8_encode($email)); //compare them them out if ($linevalue == $email) { echo $linevalue . "==" . $email . "<br/>"; //compare trimmed variables $insert = false; break; } else { echo $linevalue . "!=" . $email . "<br/>"; } }
that may not problem, place start if you're seeing same string eyes..
Comments
Post a Comment