yanjchan Posted March 7, 2009 Share Posted March 7, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/148312-solved-php-search-txt-file-for-hash-go-to-next-line-and-open-in-link/ Share on other sites More sharing options...
Maq Posted March 7, 2009 Share Posted March 7, 2009 $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 Quote Link to comment https://forums.phpfreaks.com/topic/148312-solved-php-search-txt-file-for-hash-go-to-next-line-and-open-in-link/#findComment-778676 Share on other sites More sharing options...
.josh Posted March 7, 2009 Share Posted March 7, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/148312-solved-php-search-txt-file-for-hash-go-to-next-line-and-open-in-link/#findComment-778680 Share on other sites More sharing options...
Maq Posted March 7, 2009 Share Posted March 7, 2009 Ah nice, that would be a lot more efficient Especially when dealing with text files. Quote Link to comment https://forums.phpfreaks.com/topic/148312-solved-php-search-txt-file-for-hash-go-to-next-line-and-open-in-link/#findComment-778683 Share on other sites More sharing options...
yanjchan Posted March 7, 2009 Author Share Posted March 7, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/148312-solved-php-search-txt-file-for-hash-go-to-next-line-and-open-in-link/#findComment-778695 Share on other sites More sharing options...
Maq Posted March 7, 2009 Share Posted March 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/148312-solved-php-search-txt-file-for-hash-go-to-next-line-and-open-in-link/#findComment-778698 Share on other sites More sharing options...
yanjchan Posted March 7, 2009 Author Share Posted March 7, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/148312-solved-php-search-txt-file-for-hash-go-to-next-line-and-open-in-link/#findComment-778703 Share on other sites More sharing options...
.josh Posted March 7, 2009 Share Posted March 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/148312-solved-php-search-txt-file-for-hash-go-to-next-line-and-open-in-link/#findComment-778709 Share on other sites More sharing options...
yanjchan Posted March 7, 2009 Author Share Posted March 7, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/148312-solved-php-search-txt-file-for-hash-go-to-next-line-and-open-in-link/#findComment-778727 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.