Jump to content

why wont this work? :S


Recommended Posts

its so a user cant flood other users with spam messaging, so it is to prevent them from mailing less than 15 seconds since there last oneheres my code

 

<?php
session_start();
$user = $_SESSION['kt_login_id'];
    
    //Are they logged in or not?
    if(!$user)
        {
        echo "<br><p>Blah blah you arent logged in and stuff, you should do that or something</p><br>";
        }
        
    else
        {
        //Get your private message count
        $sql = mysql_query ("SELECT pm_count FROM members WHERE member_id='$user'");
        $row = mysql_fetch_array ($sql);
        $pm_count = $row['pm_count'];
        
        $percent = $pm_count/'50';
        $percent = $percent * '100';
        ?>
        
        <?php
        //So here we get the variable submitted through the form to this page
        $reciever = $_POST['username'];
        $subject = $_POST['subject'];
        $message = $_POST['message'];
	$datetime = $_POST['datetime'];
        $error = '0';
        
        //If they are all blank we jsut say to compose a message
        if(!$reciever AND !$subject AND !$message)
            {
            ?> <?php
            }
        
        //Since this form was partially filled out we need to return an error message
        else
            {
            if (!$reciever)
                {
                $error = 'You must enter a reciever to your message';
                }
            
            if (!$subject)
                {
                $error = 'You must enter a subject';
                }
            
            if (!$message)
                {
                $error = 'You must enter a message';
                }
            
            //If the variable error is not set to zero, we have a problem and should show the error message
            if($error != '0')
                {
                echo "<p>$error</p><br>";
                }
            
            //There are no errors so far which means the form is completely filled out    
            else
                {
                //Are the trying to send a message to a real user or to something they just made up?
                $user_check = mysql_query("SELECT member_id FROM members WHERE member_id='$reciever'");
                $user_check = mysql_num_rows($user_check);
                
                //The user is real and not made up if this is true
         if($user_check > '0')
{
    if(!isset($_SESSION['time']) || ((time()-$_SESSION['time']) > 15))
    {
        $_SESSION['time'] = time();
    }
}
                        //Get their private message count
                        $sql = mysql_query ("SELECT pm_count FROM members WHERE member_id='$reciever'");
                        $row = mysql_fetch_array ($sql);
                        $pm_count = $row['pm_count'];
                        
                        //You cant have more than 50 private messages, if they try sending a message to a user with a full inbox return an error message
                        if(pm_count == '50')
                            {
                            $error = 'The user you are trying to send a message to has 50 private messages, sorry but we cant send your message untill that user deletes some of their messages.';
                            }
                            
                        else
                            {    
                            //And not we stick the message in the database with all the correct information
                            mysql_query("INSERT INTO messages_test (reciever, sender, subject, message, datetime) VALUES('$reciever', '$user', '$subject', '$message', '$datetime')") or die (mysql_error());
                            }
                            
                        //Let the user know everything went ok.
                        echo "<p><b>Message sent successfully!</b></p><br>";
                        }
                    
                    //Since they are trying to send messages faster than every 15 seconds, give them an error message    
                    else
                        {
                        $error = 'You must wait 15 seconds before sending another private message';
                        }
                    }
                
                //If they mis spelled or, made up a username, then give an error message telling them its wrong.
                else
                    {
                    $error = 'That username does not exist, please try again. Remember to check your spelling, and don\'t make stuff up at random.';
                    }
                }
            }
            
        //Since we may have set the error variable to something while trying to send the messae, we need another error check
        if($error != '0')
            {
            echo "<p>$error</p><br>";
            }
            
        else
            {
            //Here's the form for the input
            ?>
      <form name="send" method="post" action="mail_compose.php">
            <table width="100%" border="0" cellpadding="0" cellspacing="0">
              <tr>
                <td width="89" align="left" valign="top" class="message_rows_subject">Username</td>
                <td width="" align="left" valign="top"><input name="username" type="text" id="username" value="<?php echo "$reciever"; ?>"></td>
              </tr>
              
              <tr>
                <td width="89" align="left" valign="top" class="message_rows_subject">Subject</td>
                <td width="" align="left" valign="top"><input name="subject" type="text" id="subject" value="<?php echo "$subject"; ?>"></td>
              </tr>
              
              <tr>
                <td width="89" align="left" valign="top" class="message_rows_subject">Message Body</td>
                <td width="" align="left" valign="top"><textarea name="message" type="text" id="message" value="" cols="20" rows="20"style="width:470px;resize:none;"></textarea></td>
              </tr>
                  
              <tr>  
                <td><input type="submit" name="Submit" value="Send Message" /></td>
                <td><span class="message_rows"><?php echo $datetime; ?></span></td>
              </tr>
            </table>
      </form>
      <?php
            }
        }    
    ?>

Link to comment
Share on other sites

try this:

 

if($user_check > '0')
                   {
                   if(!isset($_SESSION['time']) || (time()-$_SESSION['time']) > '15') 
                        {
                        $_SESSION['time'] = time();
                        }
                   }

Link to comment
Share on other sites

try this:

 

<?php
session_start();
$user = $_SESSION['kt_login_id'];
    
    //Are they logged in or not?
    if(!$user)
        {
        echo "<br><p>Blah blah you arent logged in and stuff, you should do that or something</p><br>";
        }
        
    else
        {
        //Get your private message count
        $sql = mysql_query ("SELECT pm_count FROM members WHERE member_id='$user'");
        $row = mysql_fetch_array ($sql);
        $pm_count = $row['pm_count'];
        
        $percent = $pm_count/'50';
        $percent = $percent * '100';
        ?>
        
        <?php
        //So here we get the variable submitted through the form to this page
        $reciever = $_POST['username'];
        $subject = $_POST['subject'];
        $message = $_POST['message'];
	$datetime = $_POST['datetime'];
        $error = '0';
        
        //If they are all blank we jsut say to compose a message
        if(!$reciever AND !$subject AND !$message)
            {
            ?> <?php
            }
        
        //Since this form was partially filled out we need to return an error message
        else
            {
            if (!$reciever)
                {
                $error = 'You must enter a reciever to your message';
                }
            
            if (!$subject)
                {
                $error = 'You must enter a subject';
                }
            
            if (!$message)
                {
                $error = 'You must enter a message';
                }
            
            //If the variable error is not set to zero, we have a problem and should show the error message
            if($error != '0')
                {
                echo "<p>$error</p><br>";
                }
            
            //There are no errors so far which means the form is completely filled out    
            else
                {
                //Are the trying to send a message to a real user or to something they just made up?
                $user_check = mysql_query("SELECT member_id FROM members WHERE member_id='$reciever'");
                $user_check = mysql_num_rows($user_check);
                
                //The user is real and not made up if this is true
         if($user_check > '0')
{
    if(!isset($_SESSION['time']) || ((time()-$_SESSION['time']) > 15))
    {
        $_SESSION['time'] = time();
    }
                        //Get their private message count
                        $sql = mysql_query ("SELECT pm_count FROM members WHERE member_id='$reciever'");
                        $row = mysql_fetch_array ($sql);
                        $pm_count = $row['pm_count'];
                        
                        //You cant have more than 50 private messages, if they try sending a message to a user with a full inbox return an error message
                        if(pm_count == '50')
                            {
                            $error = 'The user you are trying to send a message to has 50 private messages, sorry but we cant send your message untill that user deletes some of their messages.';
                            }
                            
                        else
                            {    
                            //And not we stick the message in the database with all the correct information
                            mysql_query("INSERT INTO messages_test (reciever, sender, subject, message, datetime) VALUES('$reciever', '$user', '$subject', '$message', '$datetime')") or die (mysql_error());
                            }
                            
                        //Let the user know everything went ok.
                        echo "<p><b>Message sent successfully!</b></p><br>";
                        }
                    
                    //Since they are trying to send messages faster than every 15 seconds, give them an error message    
                    else
                        {
                        $error = 'You must wait 15 seconds before sending another private message';
                        }
                    }
                
                //If they mis spelled or, made up a username, then give an error message telling them its wrong.
                else
                    {
                    $error = 'That username does not exist, please try again. Remember to check your spelling, and don\'t make stuff up at random.';
                    }
                }
            }
            
        //Since we may have set the error variable to something while trying to send the messae, we need another error check
        if($error != '0')
            {
            echo "<p>$error</p><br>";
            }
            
        else
            {
            //Here's the form for the input
            ?>
      <form name="send" method="post" action="mail_compose.php">
            <table width="100%" border="0" cellpadding="0" cellspacing="0">
              <tr>
                <td width="89" align="left" valign="top" class="message_rows_subject">Username</td>
                <td width="" align="left" valign="top"><input name="username" type="text" id="username" value="<?php echo "$reciever"; ?>"></td>
              </tr>
              
              <tr>
                <td width="89" align="left" valign="top" class="message_rows_subject">Subject</td>
                <td width="" align="left" valign="top"><input name="subject" type="text" id="subject" value="<?php echo "$subject"; ?>"></td>
              </tr>
              
              <tr>
                <td width="89" align="left" valign="top" class="message_rows_subject">Message Body</td>
                <td width="" align="left" valign="top"><textarea name="message" type="text" id="message" value="" cols="20" rows="20"style="width:470px;resize:none;"></textarea></td>
              </tr>
                  
              <tr>  
                <td><input type="submit" name="Submit" value="Send Message" /></td>
                <td><span class="message_rows"><?php echo $datetime; ?></span></td>
              </tr>
            </table>
      </form>
      <?php
            }
        }    
    ?>

Link to comment
Share on other sites

Can you please elaborate on that "It's not working" ? Because according to your code, if the session variable doesn't exits OR it does exist but (time-var) is more than 15..it just sets a session variable...and that's it.  Is that the "it" you're trying to do, only it's not working, or is "it" something else?

Link to comment
Share on other sites

Sorry if I had confused you, by "it" I meant my code in general

 

And thanks for the new code Mattal; unfortunately it doesnt work; ive no idea why. once i upload it to my server my page doesnt appear in my browser  :-\

 

and ive tried it against your syntax check link, apparently its all in working order and ok  :-X

Link to comment
Share on other sites

Just out of curiousity is $user_check an integer(number)? Because from what I understand in PHP, you can only use comparison operators (other than == and !=) on an integer. However, you're using it on a string.

 

if($user_check > '0') // '0' == a string

if($user_check > 0) // 0 == an integer

Link to comment
Share on other sites

so...you're saying that not a single piece of that code works? What have you done to try and fix it, other than just post it here and hope for a magic wand to be waved?  What have you done for debugging?  Have you inserted echoes inside your conditions to see if they are ever being met? Have you echoed variables to see if they are being passed and/or are holding what you expect them to be holding? 

 

You need to tell us what you have done and what happened, because we obviously don't have access to your files/dbs/etc.. people offer some help and you respond by saying "it" doesn't work.  Well, what didn't work about it? Start over from square one, checking the variables, etc.. because that magic wand just doesn't exist.

 

I'm not trying to be an arse, but you can't just post code and say "It doesn't work, fix it."

Link to comment
Share on other sites

Okay Crayon

 

First off I'm a complete beginner with php, I'm currently trying to get a basic grasp of it and then intend to study it to further my knowledge, I only started working with it properly 2 weeks ago, where I've been using online tutorials and messing about with pre-made scripts; as you can tell the page I'm currently working on is a script in which I'm trying to understand and edit to my needs; I may have come across as bone idle at first and so apologies to everybody whose tried there best to help me.

 

Now my situation is; that my the script running on my page once uploaded to my server upon initial visit works fine, Allowing users to compose a message to each other; Only once a message has been sent and you then decide to send another the User gets the error message "You must wait 15 seconds before sending another private message" despite waiting a good 5 minutes. I'm not sure what causes this and have been trying to understand it better by searching around the net but to no avail. and so on haven't really worked with the code myself thus leading me to ask other people opinions on why it wont work, and how I can get it to work.   

 

Again, sorry if I came across as bone idle early on; I do appreciate it  :)

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.