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

user11845248
  • 131
  • 8
  • 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](http://www.php-fig.org/psr/psr-2/) 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 '20 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. – Funk Forty Niner Feb 19 '20 at 19:37
  • @FunkFortyNiner i tried that but the problem of == is that is not enter inside the if condition when the $line is equal to the $email – user11845248 Feb 19 '20 at 19:41
  • *"and i have a second question"* - That makes your question too broad. One question at a time please. – Funk Forty Niner Feb 19 '20 at 19:42
  • @FunkFortyNiner sorry about that, i deleted it also please check my upated question – user11845248 Feb 19 '20 at 19:44
  • 2
    You might need to `trim()` both the line and the email to make sure there's no whitespace/new lines – aynber Feb 19 '20 at 19:44
  • @aynber i did `$line = preg_replace('/\s+/', ' ', $line);` is that not enough ? – user11845248 Feb 19 '20 at 19:45
  • No worries @user11845248 To answer that though, you can place that file outside the public area and somewhere under a hidden folder in the root, just outside the public folder. – Funk Forty Niner Feb 19 '20 at 19:46
  • @FunkFortyNiner i'm accessing that file from different server, so how can i access it if it's not in public folder? is that possible? – user11845248 Feb 19 '20 at 19:47
  • 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 '20 at 19:47
  • Actually, your preg_replace is messing it up. You're replacing all spaces with a single space. It should be `$line = preg_replace('/\s+/', '', $line);` – aynber Feb 19 '20 at 19:49
  • Thank you so much @aynber that worked, i did trim for `$line` and `$email` – user11845248 Feb 19 '20 at 19:50
  • 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](https://stackoverflow.com/a/42381048/1415724) and [this Q&A](https://stackoverflow.com/q/409496/1415724) which could be of help. And you're welcome :) Glad to know things worked out. – Funk Forty Niner Feb 19 '20 at 19:53
  • 1
    @aynber I'd place an answer for what you suggested and add the fact about the missing `=` sign. This one's yours if you want it. – Funk Forty Niner Feb 19 '20 at 19:55
  • wow, didn't think of that, i will try that and open a question if it didn't worked the way i like – user11845248 Feb 19 '20 at 19:55
  • 1
    @user11845248 Seeing someone posted an answer that doesn't really outline the problems that prohibited your code to work properly, decided to post an answer myself instead. – Funk Forty Niner Feb 19 '20 at 20:18
  • @FunkFortyNiner thanks for adding it. I’d stepped away from the computer for awhile – aynber Feb 19 '20 at 21:44
  • @aynber You're welcome. Oh, I didn't know. Gave you kudos on it and something else earlier :-) *Cheers!* – Funk Forty Niner Feb 19 '20 at 21:45

2 Answers2

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"; 
} 

  • 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 '20 at 20:29
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."

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • 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 – user11845248 Feb 19 '20 at 23:16
  • @user11845248 Oh I see. Why do you need to do a `foreach` though? – Funk Forty Niner Feb 19 '20 at 23:17
  • @user11845248 Have a look at [this Q&A](https://stackoverflow.com/q/36808229/1415724) and [this one](https://stackoverflow.com/q/15154011/1415724) and [this one](https://stackoverflow.com/q/15189470/1415724). 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". – Funk Forty Niner Feb 19 '20 at 23:28