having trouble with file manipulation

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

Post Reply
sigil
Forum Newbie
Posts: 5
Joined: Thu Apr 02, 2009 2:43 pm

having trouble with file manipulation

Post by sigil »

Hi,

I have a text file with a list of names in it. I want to make a web page with a form so that when the user submits a name, my PHP script returns that the name either is or isn't on the list.

Here is the HTML for my index.htm page, which has the form on it:

Code: Select all

<html>
<title>You have come to my website.</title>
<body>
Please enter your name below.
<form action="login.php" method="post">
 <p>Your name: <input type="text" name="name" /></p>
 <p><input type="submit" value="Login" /></p>
</form>
</body>
</html>
And here is the PHP code for the file reader, login.php:

Code: Select all

<?php
$vname=$_POST["name"];
$uname="";
$namefile=fopen(("users.txt"),"r+");
while (!feof($namefile) && !($vname==$uname)
    {
    $uname=fgets($namefile);
    }
if ($vname==$uname)
    {
    echo "user name found";
    }
else
    {
    echo "user name not found";
    }
fclose($namefile);
?>
The .htm and .php files are both in my public_html directory.

If I comment out the file-reading part of login.php (lines 5-17), the script works fine. But when that part is executed, Explorer just gives me an HTTP 500 "The page cannot be displayed" error.

I don't have any kind of debugging environment, all I can do at present is write the code in a text editor and run it by putting the page's URL in my browser. So I don't get any specific error messages.

How can I make this work?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: having trouble with file manipulation

Post by requinix »

You can look a little closer, for one.

Code: Select all

while (!feof($namefile) && !($vname==$uname)
sigil
Forum Newbie
Posts: 5
Joined: Thu Apr 02, 2009 2:43 pm

Re: having trouble with file manipulation

Post by sigil »

Thanks, it ran without crashing once I fixed that.

My joy was short-lived, though--it didn't successfully find a name that's in the file. "users.txt" is set up like this:
  • Albert
    Bob
    Carrie
Is there a problem with the way this name list is set up, or the type of data returned by fgets(), that make it impossible to find a list item?
sigil
Forum Newbie
Posts: 5
Joined: Thu Apr 02, 2009 2:43 pm

Re: having trouble with file manipulation

Post by sigil »

Never mind, I used trim() to get rid of the trailing whitespace in each file entry, and that solved it.
Post Reply