Page 1 of 1

Need help with filtering on my PHP Message board

Posted: Wed Jan 07, 2009 12:44 pm
by tinoda
i have the following code below that i have constructed. the code enters in 10 names. How can I code it in such a way that a user will fail to enter the same name they have entered before. Danke Thanks:

Code: Select all

<?php
// messages.php
if (isset($_POST['message'])) {
    if (file_exists('messageCount.txt')) {
        $count = fopen('messageCount.txt','r+');
        $n = fread($count,1024);
    }
    else {
        $count = fopen('messageCount.txt','a+');
        $n = 0;
    }
    if (($e = ($n + 1)) <= 10) {
        rewind($count);
        fwrite($count,$e);
        fclose($count);
        $fp = fopen('messages.txt', 'a');
        fwrite($fp, "{$_POST['message']}<br />");
        fclose($fp);
    }
}
readfile('messages.txt');
?>
<form action="<?=$_SERVER['PHP_SELF'];?>" method="POST">
    <input type="text" name="message">
    <input type="submit" value="Namen eingeben">
</form>
 

Re: Need help with filtering on my PHP Message board

Posted: Wed Jan 07, 2009 1:15 pm
by sergio-pro
Hallo

This code will check the name.
But do not ever use this code to save data on server. It is not flexible and safe.

Code: Select all

 
<?php
// messages.php
if (isset($_POST['message'])) {
    if (file_exists('messageCount.txt')) {
        $count = fopen('messageCount.txt','r+');
        $n = fread($count,1024);
    }
    else {
        $count = fopen('messageCount.txt','a+');
        $n = 0;
    }
    
    $contents = file_get_contents('messages.txt');
    $arr = split("<br />", $contents);
    if (in_array($_POST['message'], $arr)) {
       echo "<font color='red'>Error: name already exists</font><br/><br/>";
    } else {
      if (($e = ($n + 1)) <= 10) {
          rewind($count);
          fwrite($count,$e);
          fclose($count);
          $fp = fopen('messages.txt', 'a');
          fwrite($fp, "{$_POST['message']}<br />");
          fclose($fp);
      }
    }
}
readfile('messages.txt');
?>
<form action="<?=$_SERVER['PHP_SELF'];?>" method="POST">
    <input type="text" name="message">
    <input type="submit" value="Namen eingeben">
</form>
 
Bitte please )

Re: Need help with filtering on my PHP Message board

Posted: Wed Jan 07, 2009 1:33 pm
by tinoda
sergio-pro - thanks it really works. but why would it not be safe to use on the server

Re: Need help with filtering on my PHP Message board

Posted: Wed Jan 07, 2009 1:40 pm
by sergio-pro
There could be a problem if two (or more) users will try to add their names at the same time.

Use databases for that.