-3

Hello so I have a text file that as this

data.txt

data e.g. username

I need this to out put into a $tag

File.php

if ($username == "$data") { echo "User Found" } else{ echo "User not found";}

Any help on this would really be nice :)?

Tom Buxton
  • 65
  • 2
  • 9
  • Your question is very unclear and I have no idea what you are talking about – Rizier123 Jun 16 '15 at 21:38
  • Im sorry very tired right now. I need php to read the data from the data.txt file and then make it work in the if else command in php – Tom Buxton Jun 16 '15 at 21:42
  • 2
    *"Im sorry very tired right now"* - get some sleep. Answers usually come when using a fresh head; believe me. – Funk Forty Niner Jun 16 '15 at 21:43
  • @Fred-ii- That's what the Canadian programmer said :) – Rizier123 Jun 16 '15 at 21:44
  • 1
    @Rizier123 I've said it all too many times. Answers/solutions *always* come later and when one has "slept on it". Works for *moi* ;-) – Funk Forty Niner Jun 16 '15 at 21:44
  • Haha guess so will try to do this in the morning – Tom Buxton Jun 16 '15 at 21:45
  • @TomBuxton Try operating a motor vehicle in your present state; same thing ;-) – Funk Forty Niner Jun 16 '15 at 21:48
  • By the way Tom; you'd probably enjoy using a database for this instead. It's more secure if you plan on using this for a user login system. If you do plan to further pursue this project, make absolutely sure that you protect that file with your life, and that it's outside the public area of your server. It just takes "one peek" into that file and it's been compromised. – Funk Forty Niner Jun 16 '15 at 21:51
  • Yeah I already have certain permissions so that only the server can access it and also it has been encrypted. All I need now is to get one word from that .txt into php. Oh well thanks for everything as I said I will work it out in the morning :) – Tom Buxton Jun 16 '15 at 21:53
  • I did find a duplicate for your question http://stackoverflow.com/questions/3950622/how-to-search-text-using-php-if-text-contains-world which is on top of your question. If by any chance you can't get that to work, just `@` me. Using `@` followed by my name, will send me a notification. But, your question did yield many similar results. It's a question of opening the file and using `strpos` or `stripos` in order to find an exact instance of the said word. `stripos()` is case-insensitive by the way. – Funk Forty Niner Jun 16 '15 at 22:02
  • @Fred-ii- In that the data isn't coming from a .txt file ? – Tom Buxton Jun 16 '15 at 22:04
  • My fault, sorry about that, I chose the wrong one. See http://stackoverflow.com/q/3686177/ and this one http://stackoverflow.com/q/9059026/ – Funk Forty Niner Jun 16 '15 at 22:06
  • @Fred-ii- None of these seem to be working or are way to compex for what I am doing. My text file only has one word in it. I just need php to get that work from it so I can compare it with what the user has entered in the form. – Tom Buxton Jun 16 '15 at 22:11
  • @TomBuxton Give me a few minutes, I'll get something going for you. – Funk Forty Niner Jun 16 '15 at 22:12
  • @Fred-ii- Thank you, i've been looking for hours but can't find anything :) – Tom Buxton Jun 16 '15 at 22:13

3 Answers3

3

One method that can be used here and that I took from one of my script libraries, is making use of preg_match() and using both the \b word boundary option, and the i switch for case-insenstivity, which will work for single or multi-line data. Will match "john" or "John", as an example.

<?php 
$_POST['name'] = "john";

$var = trim($_POST['name']); // should there be a space entered
// $var = $_POST['name'];

$pattern = "/\b$var\b/i";

$fh = fopen('data.txt', 'r') or die("Can't open file");
while (!feof($fh)) {
    $line = fgets($fh, 4096);
    if (preg_match($pattern, $line)) { 

echo "MATCH FOUND";

    }

else{
echo "NOT FOUND";
}

}

fclose($fh);

However, this will not work if an entry is seperated by a space. I.e.: "john doe".

You will need to use the following, if that is the case and stripos() for case-insensitivity.

<?php 
$_POST['name'] = "john doe";

$search = trim($_POST['name']);
// $search = $_POST['name'];


// Read from file
$lines = file('data.txt');
foreach($lines as $line)
{
  // Check if the line contains the string we're looking for, and print if it does
  if(stripos($line, $search) !== false){
    echo "Found: " . $line; }

}

References:


Footnotes:

  • A database will be much easier to achieve this and will provide a lot more freedom and flexibility than a text-based/flatfile method.
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
1
$data = file_get_contents('filehere');
if($username == $data) { echo "User found"; } else { echo "User not found"; }

user_pass.txt:

thomas password
john password2

Code:

<?PHP
$data = fopen('user_pass.txt', 'r');
while (($line = fgets($data)) !== FALSE) {
    $data = explode(' ', $data);
    if ($username == $data[0] && $password == $data[1]) {
        echo "User found";
    } else {
        echo "User not found";
    }
}
?>

Unless I missed something? If you get an errors please reply.

Thomas
  • 1,401
  • 8
  • 12
  • this would work, but only if OP's file contains only 1 line of text. Their options are very limited by using only 1 single line for a username. I've been at this for a 1/2 hour myself, but seeing their `if($username == $data)` has deemed a harder task. Using `stripos` or a `preg_match` would be better, but your answer works. – Funk Forty Niner Jun 16 '15 at 23:21
  • Why not just explode? – Thomas Jun 16 '15 at 23:23
  • sure, but I'm not going to spend any more time on this than I already have. I already had something in my library ready to go, but again... OP seems to be insistant on `if($username == $data)`. A database, would be much much more simpler, and easier to maintain, rather than hucking about text files. – Funk Forty Niner Jun 16 '15 at 23:25
  • I'd of left your original answer part of your new one, to give them the option. – Funk Forty Niner Jun 16 '15 at 23:36
0

Function search: post function search in PHP

  function search($search, $string) {

    $pos = strpos($string, $search);  

    if ($pos === false) {

      echo "The string '$search' was not found.";       

    } else {

      echo "The string '$search' was found ";   
      echo "and exists at position $pos.";  

    }    

  }

love.txt as file

we
love
php
programming

open file:

  $file = fopen("love.txt", "r");

call function search and function fread for open file 'love.txt'

  search("love", fread($file, filesize("love.txt")));

Result:

The string 'love' was found and exists at position 4.

antelove
  • 3,216
  • 26
  • 20
  • Your answer should include some description of how your solution works or how to implement it rather than just posting the code with no explanation – Paolo Jul 25 '17 at 19:25