• PHP Help PHP Newbies
  • Shoutbox , ridding SPAM, adding features, Syntax, explanations of my shoutbox code, h

Here's where I'm at. I managed to write this much.

$count = substr_count($message, 'href');

if ($count > 2) 
    die ();

Problem is... the die kills the rest of the php on the page, I also had to insert it in after the 'message' has been given the value $message, so the content is already shot to the database before it dies....

I need to know how to assign the text inside my text area named 'message' can be assigned as a variable before being sent to the db, and also preventing the info from being sent to the db with out killing all php on the page...

    Honestly... where did you get that shoutbox? Its a wonder it still work hehe.... the code looks real old.

    Anyway... try.

    if($submit)
    {
        putenv('TZ=America/New_York');
        $time=date("F j, Y, g:i a", time());
    
    $count = substr_count($message, 'href');
    if ($count < 2) {
      $result=MYSQL_QUERY("INSERT INTO shoutbox (id,name,message,time)".
      "VALUES ('NULL','$name', '$message','$time')");
    }
    
    }
    

      YOU are AWSOME 😃

      Didn't occur to me to do that. Question though, the $message variable hasn't been defined at the point where we check it? how does that work?

      Don't remember where I got it, but I got it to work so I was very happy. It was my first PHP use.

      Maybe you can help me with my other big issue. When my user submits, then say wants to reload the page to see if someone else wrote anything, you get the pop-up box asking "do you want to submit this data again?". Is there a way around that? This stems from my question #3.

        Alright... because you made an effort to read, and I was board. I have redone your shoutbox. Most of the features you want are in there... your users will no longer need to refresh the page to see there newly added items. Users cannot post more than 1 link. I have even built a simple badword filter. Just add your bad words to the array at the top.

        Ive also cleaned up the code a fair bit and brought it back into the 21st centuary. I havent tested it so make sure you save your current one before you replace anything.

        <?php
        
        
        $badwordlist = array("add","bad","words","here");
        
        mysql_connect("localhost","xxxx","xxxx");
        mysql_select_db("xxxx");
        
        if ($_POST['submit']) {
            putenv('TZ=America/New_York');
            $time = date("F j, Y, g:i a", time());
        
            if (substr_count($_POST['message'],'http') > 1) {
                $tomanylinks = true; 
        
            }       
        
            foreach($badwordlist as $badword) {
                if (strpos($_POST['message'],$badword) {
                    $badwordfound = true; 
                    break;  
                }       
            }       
        
            if (!$tomanylinks || !$badwordfound) {
                $result = mysql_query("
                    INSERT INTO shoutbox (
                        name,
                        message,
                        time
                    ) VALUES (
                        '{$_POST['name']}',
                        '{$_POST['message']}',
                        '{$_POST['time']}'
                    )");
            }       
        
            if ($result) {
                header("Location: {$_SERVER['PHP_SELF']}");
            } else {
                die("There was an error adding to the shoutbox");
            }       
        
        } else {
            echo "<form action=\"{$_SERVER['PHP_SELF']}\" method=\"post\">";
            echo "  <input id=\"name\" type=\"text\" name=\"name\" />";
            echo "  <input type=\"submit\" name=\"submit\" value=\"Submit\" />";
            echo "  <textarea rows=\"2\" cols=\"35\" name=\"message\"></textarea>";
            echo "</form>";
            if ($result = mysql_query("select * from shoutbox order by id desc limit 40")) {
                while($r = mysql_fetch_array($result)) {    
                    echo $r["time"]."<br />"; 
                    echo $r["id"]."<br />";
                    echo $r["message"]."<br />";
                    echo $r["name"]."<br />";
                }       
            } else {
                die("There was an error displaying the shoutbox");
            }       
        }
        
        ?>
        

          I get a syntax error on this line.
          if (strpos($_POST['message'],$badword) {

          Parse error: syntax error, unexpected '{' in /home/irekevin/public_html/index2.php on line 112

          I checked what line it was just by making new lines down the page to see when it jumped.

          The part that loops, can I add html into that? if so how? This is how I currently have it set up.

          <div class="weblog_comment">
          <div class="weblog_comment_name"><? echo $name ?></div>
          <div class="weblog_comment_time"><? echo $time ?></div>
          <div class="weblog_comment_message"><? echo $message ?><br /><br /></div>
          </div>

          Thank You very much for your help.

            missing a ')'

            if (strpos($_POST['message'],$badword)) { 
            

              That syntax error is easy fixed... just missing a )

              if (strpos($_POST['message'],$badword)) {
              

              As for adding that html...the easiest solution would be...

              while($r = mysql_fetch_array($result)) {    
              ?> <div class="weblog_comment"> <div class="weblog_comment_name"><?php echo $r['name'] ?></div> <div class="weblog_comment_time"><?php echo $r['time'] ?></div> <div class="weblog_comment_message"><?php echo $r['message'] ?><br /><br /></div> </div> <?php }
                thorpe wrote:

                #1 [man]strpos[/man].
                #2 [man]substr_count[/man].
                #3 ajax.
                #4 install.

                Where can I download that for Windows?

                  Where can I download that for Windows?

                  What for windows?

                    Thanks guys alot, i'm really trying to get a hang of the syntax.

                    I do however get the "There was an error adding to the shoutbox"

                    Not sure, but i'd take a guess it may have to do with the

                    header("Location: {$_SERVER['PHP_SELF']}");

                    I saw you took out the Null / ID for inserting to the table... would that have anything to do with it ?

                    As far as understanding the code.

                            foreach($badwordlist as $badword) { 
                                if (strpos($_POST['message'],$badword)) { 
                                    $badwordfound = true; 
                                    break;   
                    }

                    What is the break; for? Are variables defaulted to false?

                            if (!$tomanylinks && !$badwordfound) { 

                    Does the ! mean if $tomanylinks is false?

                      Ok I tried goofing around a little.

                              if ($result) { 
                                  header("Location: {$_SERVER['PHP_SELF']}"); 
                              } else { 
                                  die("FIRST There was an error adding to the shoutbox"); 
                              } 

                      I replaced $result with "$result = mysql_query("select * from shoutbox order by id desc limit 40")"

                      and got this error, but it did write to the db...

                      Warning: Cannot modify header information - headers already sent by (output started at /home/irekevin/public_html/index2.php:1) in /home/irekevin/public_html/index2.php on line 132

                        Post what you have now, ive no idea what youve moved / haven't moved.

                          Sorry, not near my computer, but I didn't change anything. Tried a few things (very blindly i might add) and just changed it back.

                          I added my name/pass/db_name etc... it displays fine, but upon adding to it i get the first error "There was an error adding to the shoutbox"

                            My fault... should have been watching what I was doing. Change this...

                            '{$_POST['time']}'
                            

                            to

                            '$time'
                            

                              Thanks I'll have a gander at it later tonight when i get home and hopefully all will go smoothly, then I'll take a shot at trying to make alternating row attributes :rolleyes: using some sort of if even then this if odd then that using the ID.

                                        if (!$tomanylinks && !$badwordfound) { 
                                            $result = mysql_query(" 
                                                INSERT INTO shoutbox ( 
                                                    name, 
                                                    message, 
                                                    time 
                                                ) VALUES ( 
                                                    '{$_POST['name']}', 
                                                    '{$_POST['message']}', 
                                                    '$time' 
                                                )"); 
                                        } 

                                Made the change and it looks like the above. Same error though. ?

                                  Write a Reply...