acctman Posted April 7, 2009 Share Posted April 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/153016-limit-user-to-5-message-sends-per-day/ Share on other sites More sharing options...
revraz Posted April 7, 2009 Share Posted April 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/153016-limit-user-to-5-message-sends-per-day/#findComment-803661 Share on other sites More sharing options...
Maq Posted April 7, 2009 Share Posted April 7, 2009 if($messages //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. Quote Link to comment https://forums.phpfreaks.com/topic/153016-limit-user-to-5-message-sends-per-day/#findComment-803662 Share on other sites More sharing options...
acctman Posted April 7, 2009 Author Share Posted April 7, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/153016-limit-user-to-5-message-sends-per-day/#findComment-803666 Share on other sites More sharing options...
mrMarcus Posted April 7, 2009 Share Posted April 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/153016-limit-user-to-5-message-sends-per-day/#findComment-803671 Share on other sites More sharing options...
acctman Posted April 7, 2009 Author Share Posted April 7, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/153016-limit-user-to-5-message-sends-per-day/#findComment-803693 Share on other sites More sharing options...
revraz Posted April 7, 2009 Share Posted April 7, 2009 Well you can build a message table, within that table store the UserId, Date, Message Number Query that table for the person, today's date and the number. Quote Link to comment https://forums.phpfreaks.com/topic/153016-limit-user-to-5-message-sends-per-day/#findComment-803720 Share on other sites More sharing options...
PFMaBiSmAd Posted April 7, 2009 Share Posted April 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/153016-limit-user-to-5-message-sends-per-day/#findComment-803769 Share on other sites More sharing options...
acctman Posted April 7, 2009 Author Share Posted April 7, 2009 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) Quote Link to comment https://forums.phpfreaks.com/topic/153016-limit-user-to-5-message-sends-per-day/#findComment-803901 Share on other sites More sharing options...
PFMaBiSmAd Posted April 7, 2009 Share Posted April 7, 2009 SELECT count(*) FROM user_msg WHERE msg_date >= UNIX_TIMESTAMP(DATE_SUB(NOW(),INTERVAL 24 HOUR)) AND msg_sender = $userID Quote Link to comment https://forums.phpfreaks.com/topic/153016-limit-user-to-5-message-sends-per-day/#findComment-804034 Share on other sites More sharing options...
acctman Posted April 7, 2009 Author Share Posted April 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/153016-limit-user-to-5-message-sends-per-day/#findComment-804036 Share on other sites More sharing options...
PFMaBiSmAd Posted April 7, 2009 Share Posted April 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/153016-limit-user-to-5-message-sends-per-day/#findComment-804050 Share on other sites More sharing options...
acctman Posted April 7, 2009 Author Share Posted April 7, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/153016-limit-user-to-5-message-sends-per-day/#findComment-804062 Share on other sites More sharing options...
PFMaBiSmAd Posted April 8, 2009 Share Posted April 8, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/153016-limit-user-to-5-message-sends-per-day/#findComment-804073 Share on other sites More sharing options...
acctman Posted April 8, 2009 Author Share Posted April 8, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/153016-limit-user-to-5-message-sends-per-day/#findComment-804184 Share on other sites More sharing options...
acctman Posted April 8, 2009 Author Share Posted April 8, 2009 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]--> Quote Link to comment https://forums.phpfreaks.com/topic/153016-limit-user-to-5-message-sends-per-day/#findComment-804994 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.