Page 1 of 1

having trouble with file manipulation

Posted: Fri Apr 03, 2009 7:05 pm
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?

Re: having trouble with file manipulation

Posted: Fri Apr 03, 2009 7:09 pm
by requinix
You can look a little closer, for one.

Code: Select all

while (!feof($namefile) && !($vname==$uname)

Re: having trouble with file manipulation

Posted: Fri Apr 03, 2009 7:20 pm
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?

Re: having trouble with file manipulation

Posted: Fri Apr 03, 2009 7:30 pm
by sigil
Never mind, I used trim() to get rid of the trailing whitespace in each file entry, and that solved it.