-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 :)?

15
  • Your question is very unclear and I have no idea what you are talking about Commented Jun 16, 2015 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 Commented Jun 16, 2015 at 21:42
  • 2
    "Im sorry very tired right now" - get some sleep. Answers usually come when using a fresh head; believe me. Commented Jun 16, 2015 at 21:43
  • @Fred-ii- That's what the Canadian programmer said :) Commented Jun 16, 2015 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 ;-) Commented Jun 16, 2015 at 21:44

3 Answers 3

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.
Sign up to request clarification or add additional context in comments.

Comments

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.

4 Comments

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.
Why not just explode?
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.
I'd of left your original answer part of your new one, to give them the option.
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.

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.