Jump to content

Recommended Posts

Hi, I need help with my mail system. I'd like to limit users to 5 message/note sends per Day. My message/note system is already complete and i just need some sample code to look at that checks how many messages a user has sent in a day and to limit him/her to 5msgs.

 

also the code needs to be quick/clean/and designed for performance on a high traffic site. normally i'd post some sample code i've written to start out but I have no clue where to start at with checking to see how many messages were sent in a day and limiting a user to 5 only.

Link to comment
https://forums.phpfreaks.com/topic/153016-limit-user-to-5-message-sends-per-day/
Share on other sites

if($messages <= 5)
   //allow them to PM, add 1 to DB.
} else {
   //don't allow them to PM
}

 

Run a cron job to reset the users messages to 0 every night.

 

running a cron for over 1.2mil users scanning there mail could slow down things a bit. any other option?

as said, every time a member sends a pm, a field gets updated in the database .. then, anytime they try and send a pm, have the system check against the value in the db to see whether they can send on or not.

 

once the user have reached their limit, another field gets updated, ie. canSendPM, to 0 meaning they are to be queued for a cron job.

 

so, now your cron job is only affecting people with the field 'canSendPM' which is set to 0.

 

or something like that.

Have a field in your DB that stores this value each time a msg is sent, then check it each time they want to send a new msg.

 

'this value' what value are you referring too? can you explain a bit more? Things that need to be checked are the Limit : 5, CurrentDate and how many msg have been sent so far.

 

Field: m_msglimit , m_msgdate

 

can a code that checks the m_msglimit field before sending and increases the count by +1 each time a message is sent up to 5?

if $msglimit <= 5 {
send msg...

   if $msgdate == CurrentDate {
   sql update members SET m_msglimit = '$msglimit CODE to increase by +1' where m_user = $Userid
   } elseif $msgdate != CurrentDate {
   sql update members SET m_msglimit = '1', m_msgdate = 'CurrentDate'  where m_user = $Userid

} else {
echo 'msg limit reached';
}

 

thats my thinking right now... pros vs cons pros:not having to run a big cron at midnight along with other system crons that run. cons: this will add two more fields to the member table and require 3 queries.

 

so would a code like that work?

Or if you just store the datetime of each message with each message, you can do a simple count query to get how many there are within the previous 24 hours (a running total) or with the current date for that user and if the count is < 5, allow the new message. There is no need to maintain a count in the table or run any extra code to reset it.

Or if you just store the datetime of each message with each message, you can do a simple count query to get how many there are within the previous 24 hours (a running total) or with the current date for that user and if the count is < 5, allow the new message. There is no need to maintain a count in the table or run any extra code to reset it.

 

can you show me a quick example on how to do this. I would use Select Count and the follow table and fields

 

Table: user_msg

Field: msg_date (format 1231648483)

Field: msg_sender ($userID)

SELECT count(*) FROM user_msg
WHERE msg_date >= UNIX_TIMESTAMP(DATE_SUB(NOW(),INTERVAL 24 HOUR))
AND msg_sender = $userID

 

 

thanks, quick question I'm trying to understand what the last part of the select count query is doing. The INTERVAL 24 HOUR. Is it checking 5 msg in the 24hr period or in a day? I ask this because if a user sends 5msg and the last was at 4/07/09 11:59pm at 4/08/09 12:00am he/she should be able to send again.

Are all your users in the same time zone as the server? If they are not, then using the current day/next day on the server will mean that their next 5 messages could start at whatever time it is where they are when the server's time is midnight.

Are all your users in the same time zone as the server? If they are not, then using the current day/next day on the server will mean that their next 5 messages could start at whatever time it is where they are when the server's time is midnight.

 

no users are in different zones across the world. is there a way to get the user time and not the server time?

You would need to use javascript/AJAX and send a http request with the users time or the users GMT offset to the server.

 

Edit: Don't you already have the user enter their GMT offset or time zone so that you can personalize the display of information in their time, like a forum does?

You would need to use javascript/AJAX and send a http request with the users time or the users GMT offset to the server.

 

Edit: Don't you already have the user enter their GMT offset or time zone so that you can personalize the display of information in their time, like a forum does?

 

i'm going to add a GMT offect section to the User Preference area. thanks for all the help

You would need to use javascript/AJAX and send a http request with the users time or the users GMT offset to the server.

 

Edit: Don't you already have the user enter their GMT offset or time zone so that you can personalize the display of information in their time, like a forum does?

 

i'm have a problem with the query... I'm running the code below and its displaying the Message limit reached texted. But, I haven't sent out any mail in like 3 days. So something is off in the query i think. When i change  $en['msglimit']>=5 to  $en['msglimit']<=5 it loads the message form.

 

<?php $en['msglimit'] = mysql_query("SELECT count(*) FROM rate_messages WHERE msg_time >= UNIX_TIMESTAMP(DATE_SUB(NOW(),INTERVAL 24 HOUR)) AND msg_sender='".$_SESSION['userid']."'"); ?>
<!--[if Start $en['msglimit']>=5]-->
Sorry you have already reached your quota of messages for today.
<!--[if Else]-->

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.