Jump to content

[SOLVED] PHP Search .txt file for hash, go to next line and open in link


Recommended Posts

How would I make a PHP file that searched a .txt file for a hash, went to next line and opened returned the value?

 

For example, I would have

 

a8sdfjujakl29d8ajsdf

blah

29dffajaksl18d8010a

blah2

 

in a .txt file for a register I'm making. The free, but OK server I'm using doesn't support mySQL. (this is a hobby project)

 

How would I accomplish this?

 

Thanks!

$lines = file('your_text.txt');
$hash = "lafl2j23ljlwavjwlvj";
foreach ($lines as $line_num => $line) {
    if(trim($line) == $hash) {
   echo $lines[$line_num+1];
           break;
}
}
?>

 

 

EDIT: added break so it doesn't go through the entire text file unless you want it to.

 

You could also call "in_array()" on $lines to first check if the hash is even in there before you start looping, let me know  ;)

using in_array to see if it's even in the array and then reducing the foreach loop by yanking all the keys of the matches and just looping through those instead of whole array....

$file = file('test.txt'); // your file name inside quotes
$hash = 'a8sdfjujakl29d8ajsdf' . "\n";
if (in_array($hash, $file))
   $hashLine = array_keys($file,$hash);
foreach ($hashLine as $key)
   $values[] = trim($file[$key+1]);
echo "<pre>";print_r($values);

Thanks!

As another question, how would I put a statement that detected if the hash existed in the file at all in an if statement?

 

For example, my (flawed) adaptation of your code in an if statement (I am going to use the code you provided elsewhere not in an if statement) doesn't seem to work.

 

Here is the troublemaking segment:

 

else {
/* if the "submit" variable exists, the form has been submitted - look for and process form data */
    // display result
    $user = $_POST['user'];
$pass = $_POST['pass'];

    function encrypt($enc){
$v1 = '10dksj281ls4a';
$v2 = '81ksja0481dka';
    $token = md5(sha1(md5(base64_decode($v1 . $enc . $v2)).$enc).$v1);
    return $token;
}

$token = encrypt($user . $pass);
$write = $token . '\n' . $user;

$file = file('/host/y/a/n/yanjchan/jonathan/user.txt'); // your file name inside quotes

if (in_array($write, $file))
{
	?>
Sorry, that username has been taken.<br>
Please <a href='register.php'>choose another one.</a>
    <?php
}
elseif (!in_array($write, $file))
{
$file = '/host/y/a/n/yanjchan/jonathan/user.txt'; 
// open file 
$fh = fopen($file, 'a') or die('Error'); 
// write to file 
fwrite($fh, $write) or die('Error'); 
// close file 
fclose($fh);
?>
    Your account has been created!<br />
    Congratulations!</br>
    Click <a href="logintest.php">here</a> to login!

    <?php
}
}




?>

 

Thanks for your help!

 

As another question, how would I put a statement that detected if the hash existed in the file at all in an if statement?

 

Your $write var has a linebreak in it...  I think you want to pass it $token.

Oh, you mean

$write = $token . '\n' . $user;

?

 

Well, the error is that the if statement is not working, and it fails to detect that the instance already exists.

 

This is on the text file:

 

$token\n$userd16a65eb1b6c2fefe3bedd532c8fb76c\nhid16a65eb1b6c2fefe3bedd532c8fb76c\nhid16a65eb1b6c2fefe3bedd532c8fb76c\nhid16a65eb1b6c2fefe3bedd532c8fb76c\nhid16a65eb1b6c2fefe3bedd532c8fb76c\nhid16a65eb1b6c2fefe3bedd532c8fb76c\nhid16a65eb1b6c2fefe3bedd532c8fb76c\nhid16a65eb1b6c2fefe3bedd532c8fb76c\nhid16a65eb1b6c2fefe3bedd532c8fb76c\nhi

 

Lol...

if you use single quotes around things, everything in-between is interpreted literally.  Like variables and escaped characters such as that linebreak character.  Use double quotes for them to actually be parsed.

Found problem... In_array returns TRUE if found, FALSE if not found. My if statement was detecting if it exists, then purposely adding another! Ha...

 

Well, thanks for both of your help!

If I encounter problems, I will post them in this thread.

 

Thanks again!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.