Page 1 of 2

Retrieve account system not working

Posted: Sat Sep 16, 2006 3:17 pm
by toasty2
I started on a website that gives you a username and password from a huge text file. The way this is supposed to work is:
1) goto the page
2) PHP reads a file that contains a number, by default the number will be 1
3) PHP reads the db file and retrieve's the line number based on what was in step 2
4) PHP echos the line's contents
5) PHP edits the file that step 2 read from and adds 1 to the number.

This system is not working properly. (note, I am using php4, take this in mind when helping me)

Here is "get.php":

Code: Select all

<?php
// Get current line number
$filename = "l.txt";
$handle1 = fopen($filename, "w+");
$linenum = file($filename); // Get line num
$file = file($filename);

// Read the DB
$filename = "db.php";
$handle2 = fopen($filename, "r");
$lines = file($filename); //$lines is an array 
// $file = fread($handle, filesize($filename));
echo htmlentities($lines[$linenum]);
fclose($handle2);

// Change the line # for next request
$file = $file + 1;
fwrite($handle1, $file);
echo htmlentities($lines[$linenum]);
fclose($handle1);
?>
Here is l.txt (the line number file):

Code: Select all

1
Here is db.php:

Code: Select all

<?php echo "Trying to peak? Attempt logged."; exit; ?>
Account1 Password1
Account2 Password2
Account3 Password3
Account4 Password4
Account5 Password5
Account6 Password6
Account7 Password7
Account8 Password8
(The contents of db.php have been changed to protect the contents that I wish for yall to not see, but it will not affect how the system operates. The reason db.php is a php file and has code on the 1st line, is to not output the database for a person that may figure out the name of the database file.)
It gives me these errors:

Code: Select all

Warning: Illegal offset type in /usr/export/www/hosting/abc/stuff/get.php on line 13

Fatal error: Unsupported operand types in /usr/export/www/hosting/abc/stuff/get.php on line 17

Posted: Sat Sep 16, 2006 3:31 pm
by volka
$linenum = file($filename); // Get line num
[...]
echo htmlentities($lines[$linenum]);
file always returns an array (or false).

Posted: Sat Sep 16, 2006 3:33 pm
by toasty2
And your point is? I don't get what you're trying to tell me. I do want the file stored in an array. The line number to print out corresponds with the key in the array.

Posted: Sat Sep 16, 2006 3:41 pm
by volka
You want something like $line[1], $line[4] ....
But you have $line[array()] becauae $linenum is an array.

edit: Same with
$file = file($filename);
[...]
$file = $file + 1;

Posted: Sat Sep 16, 2006 3:53 pm
by toasty2
Sorry, but could you explain that better? :( I'm not very smart, and I'm new to programming.

Edit: are you suggesting I read each line of the file seperately and save it into an array?
That won't work, I have 1,500 lines.

Posted: Sat Sep 16, 2006 4:44 pm
by volka
No, sorry, I cannot explain it any better.

Code: Select all

<?php
// Get current line number
$filename = "l.txt";
$handle1 = fopen($filename, "w+");
$linenum = file($filename); // Get line num
$file = file($filename);

var_dump($linenum);
var_dump($file);
if ($linenum==$file) {
	echo ' $linenum is equal to $file. ';
}

// Read the DB
$filename = "db.php";
$handle2 = fopen($filename, "r");
$lines = file($filename); //$lines is an array
// $file = fread($handle, filesize($filename));

echo ' index for $lines is ', gettype($linenum);
echo htmlentities($lines[$linenum]);
fclose($handle2);

// Change the line # for next request
$file = $file + 1;
fwrite($handle1, $file);
echo htmlentities($lines[$linenum]);
fclose($handle1);
?>

Posted: Sat Sep 16, 2006 4:58 pm
by toasty2

Code: Select all

array(0) { } array(0) { } $linenum is equal to $file. index for $lines is array
Warning: Illegal offset type in /usr/export/www/hosting/rrstuff/scdb/get2.php on line 21

Fatal error: Unsupported operand types in /usr/export/www/hosting/rrstuff/scdb/get2.php on line 25
is the output, so what do I need to change?

Posted: Sat Sep 16, 2006 4:59 pm
by volka
According to this output $linenum is an array with no entries.
So waht is $lines[$linenum] supposed to do?

Posted: Sat Sep 16, 2006 5:10 pm
by toasty2
I based that part of my code off of this snippet: http://www.hawkee.com/snippet.php?snippet_id=1076

Posted: Sat Sep 16, 2006 5:54 pm
by volka
And in this snippet $linenum is a number
$linenum = 2;
not an array.

Posted: Sat Sep 16, 2006 6:00 pm
by toasty2
Ok.

Then how can I do what my plan (the numbered list in 1st post) was?

Isn't there a php function that only reads a certain line from a file? If it exists, I am unable to find it.

Posted: Sat Sep 16, 2006 6:02 pm
by volka
http://www.hawkee.com/snippet.php?snippet_id=1076 wrote:$linenum = 2; //will read 2nd line of the file.
That's not correct, acutally it will reference the third line. But which line of the data file do you want to read/use?

Posted: Sat Sep 16, 2006 6:08 pm
by toasty2
I realize it starts at 0, but in my case, that is good.

db.php (as seen above) has some php code on the 1st (0 in array) line , keeping anyone except php from reading it. The actual information starts on line 2 (1 inside the array).

Posted: Sat Sep 16, 2006 6:12 pm
by volka
So you should set $linenum to .... ?

Posted: Sat Sep 16, 2006 6:27 pm
by toasty2
1?