1

I'm using this code to check if the entered email is existe or not in text file

$handle = fopen("http://mywebsite/u.txt", "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        $line = preg_replace('/\s+/', ' ', $line);
        if($line=$email){
            echo "email existe";
        }
    }

    fclose($handle);
} else {
    // error opening the file.
}

the output is the same for exsiting emails and not exsiting email in the text file

email existeemail existeemail existe

the text file contains as a test emails

  • test@test.com
  • hello@world.com
  • test@t.com

if i did == it didn't enter the condition when the $line = $email

trying with

echo $line." ".$email;

OUTPUT

test@test.com test@test.comtest@test.com test@test.comtest@test.com test@test.com

18
  • 1
    Good code indentation would help us read the code and more importantly it will help you debug your code Take a quick look at a coding standard for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end.
    – RiggsFolly
    Feb 19, 2020 at 19:37
  • 3
    if($line=$email) < is an assignment and will always be TRUE. You're looking for if($line==$email) with an extra =. Give that a try. If it works, there's a duplicate for this. Feb 19, 2020 at 19:37
  • 2
    You might need to trim() both the line and the email to make sure there's no whitespace/new lines
    – aynber
    Feb 19, 2020 at 19:44
  • 1
    No, it's not enough, because when I run your code but put var_dump($line); after your preg_replace, it still has a space after it.
    – aynber
    Feb 19, 2020 at 19:47
  • 1
    Hm.... now that complicates things then. If you can .htaccess, you can allow only your server to access the file and not let anyone else get access. See this answer and this Q&A which could be of help. And you're welcome :) Glad to know things worked out. Feb 19, 2020 at 19:53

2 Answers 2

1

Your code failed for 2 reasons.

1) if($line=$email) is an assignment method and will always be TRUE. You were looking to use the == comparison method.

2) You need to use trim() since that will take care of new line characters that are getting included in your file. This was outlined by another member here, being Aynber. Kudos to them.

Per:

"You might need to trim() both the line and the email to make sure there's no whitespace/new lines – aynber".

and

"put var_dump($line); after your preg_replace, it still has a space after it."

3
  • i've find out that it work only for the first line, test@test.com but for hello@world.com it don't work, i need to do foreach..., how can i do that please Feb 19, 2020 at 23:16
  • @user11845248 Oh I see. Why do you need to do a foreach though? Feb 19, 2020 at 23:17
  • @user11845248 Have a look at this Q&A and this one and this one. Maybe there's something in one of those that will help fix the new problem you're having. I found those when Googling "if user password exists in file php -mysql". The - before "mysql" in Google means to "not include mysql". Feb 19, 2020 at 23:28
1

I don't know if I understood you correctly, but here you have a slightly different solution to the problem

// File from http
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$handle = curl_exec($ch);
curl_close($ch);   

// or
$handle = file_get_contents("u.txt");
//or
$handle = "test@test.com \n hello@world.com";


$find = "hello@world.com";

if (strpos($handle, $find) > -1){ 
    echo "Found" ; 
} else{ 
    echo "Not Found"; 
} 

1
  • This is probably faster than looping line by line. But even if it works, I'm not sure I like the >-1 thing. Strpos returns false if nothing is found. Make it !== false
    – Andreas
    Feb 19, 2020 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.