Retrieve account system not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Retrieve account system not working

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

$linenum = file($filename); // Get line num
[...]
echo htmlentities($lines[$linenum]);
file always returns an array (or false).
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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;
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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);
?>
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post 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?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

According to this output $linenum is an array with no entries.
So waht is $lines[$linenum] supposed to do?
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

I based that part of my code off of this snippet: http://www.hawkee.com/snippet.php?snippet_id=1076
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

And in this snippet $linenum is a number
$linenum = 2;
not an array.
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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?
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post 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).
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

So you should set $linenum to .... ?
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

1?
Post Reply