Need help with filtering on my PHP Message board

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
tinoda
Forum Commoner
Posts: 33
Joined: Mon Dec 29, 2008 3:32 am

Need help with filtering on my PHP Message board

Post 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>
 
User avatar
sergio-pro
Forum Commoner
Posts: 88
Joined: Sat Dec 27, 2008 12:26 pm

Re: Need help with filtering on my PHP Message board

Post 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 )
tinoda
Forum Commoner
Posts: 33
Joined: Mon Dec 29, 2008 3:32 am

Re: Need help with filtering on my PHP Message board

Post by tinoda »

sergio-pro - thanks it really works. but why would it not be safe to use on the server
User avatar
sergio-pro
Forum Commoner
Posts: 88
Joined: Sat Dec 27, 2008 12:26 pm

Re: Need help with filtering on my PHP Message board

Post 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.
Post Reply