PDA

View Full Version : file checking problems


[m]
05-09-2003, 06:22 AM
ok I have this script that I'm working on where I get input of an email the script validates it and is supposed to check weather or not the information is already present if it is not then the script writes down the new address. the problem is that I can't get it to check if the address is already there

can you fix it for me?
I have found out that the problem has something to do with the IF statement:
if ($list == $email){
maby it is that the $list is made from an array or something?

I can't seem to figure it out but I'm learning...I have searched through one of my books but found nothing to help me with this problem
<?php
if (eregi(".+@.+\..+", $email)) {
$TheFile = "data.php";
$Data = file($TheFile);
$n = 0;
while ($n < count($Data)){
$list = $Data[$n];
if ($list == $email){
print "$list <BR>\n";
print "";
$absent = FALSE;
print "The address $email already exists on the list<br>\n";
}
$n++;
}
if ($absent) {
$open = fopen($TheFile, "a+");
if ($open) {
fwrite ($open, "$email\n");
fclose($open);
print "your address: $email has been recived<br>\n";
} else {
print "there was an error in the process<br>\n";
}
}
} else {
print "$email is not a valid e-mail address\n";
}

?>f

mordred
05-09-2003, 02:21 PM
The problem might be that file() splits a text file into an array consisting of complete lines - complete in the sense that a newline, linefeed or carriage return is appended at the end of the line. A simple test for equality won't return true unless that (useless) whitespace is not stripped from the end of the line.

You could try


if (trim($list) == trim($email)){


That strips whitespace both at the start and the end of the string variable.

[m]
05-10-2003, 09:29 PM
you are a life/ time save'r!!!

I added in what you said and one other thing that I was missing and now it woks how I want it to!
$absent = TRUE;
$list = trim($Data[$n]);
$email = trim($email);
if ($list == $email){


<?php
if (eregi(".+@.+\..+", $email)) {
$TheFile = "data.php";
$Data = file($TheFile);
$lista = count($Data);
print "$lista <br>\n";
$n = 0;
$absent = TRUE;
while ($n < count($Data)){
$list = trim($Data[$n]);
$email = trim($email);
if ($list == $email){
print "$list <BR>\n";
print "";
$absent = FALSE;
print "The address $email already exists on the list<br>\n";
}
$n++;
}
if ($absent) {
$open = fopen($TheFile, "a+");
if ($open) {
fwrite ($open, "$email\n");
fclose($open);
print "your address: $email has been recived<br>\n";
} else {
print "there was an error in the process<br>\n";
}
}
} else {
print "$email is not a valid e-mail address\n";
}
print "<style>";
?>

[m]
05-11-2003, 03:23 AM
new fav code'r = mordred :D