MyBB Community Forums

Full Version: Count errors PHP
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Guys Is there a code that can count how many times user got error message on the same page?
Please anyone how can I count how many times error message showed!

if //something 
{
error("some error");
}

Maybe update something every time error shows like:

if //something 
{
error("some error");
//update something or set something
}
What type of error are you looking at counting?

PHP errors, I'm not sure about, you could always use a service like New Relic to monitor them.

If you're wanting to monitor calls to the error() function, there is a plugin hook there that you can use to do something like logging the user+message or something:

$plugins->add_hook('error', 'log_error');
function log_error($e) {
    global mybb;
    $log = "{$mybb->user['name']}: {$e}";
    // Then write $log to a file or the database or something.
}
Thanks buddy I am trying to create a plugin that will block users for X hours if they get 5 errors during creating of new thread! Something similar to login attempt redirect!

The plugin I have basically checks for URLs in posts and if it finds one it returns inline error you know the error shows on the same page!
There is a Hook in the function error.

$plugins->add_hook('error', 'logger');

However, i don't think this is what you need.

What kind of errors you wish to log? ALL ERRORS or only those which redirect you to a new page that is handled by mybb's error function?

If you to log those errors that cause redirection, then the above hook will work for you.
just create a function:

function logger()
{
     if(!isset($mybb->cookies['myplugin_errors']))
     {
           $count = 0;
     }
     else
     {
           $count = $mybb->cookies['myplugin_errors'];
     }
     $expiry = TIME_NOW + (60*60*24);  // Expiry = 1 day
     my_setcookie("myplugin_errors", "{$count}", $expiry, true);
}

Then you can place a global hook to check if the cookie's value is > 5.

I hope i helped Smile
(2014-04-01, 03:04 PM)marcus123 Wrote: [ -> ]Thanks buddy I am trying to create a plugin that will block users for X hours if they get 5 errors during creating of new thread! Something similar to login attempt redirect!

The plugin I have basically checks for URLs in posts and if it finds one it returns inline error you know the error shows on the same page!

Checking if error() was called during thread creating seems like a bit of a roundabout way of doing it, why not make the code part of the actual URL-checking plugin instead of trying to monitor its output?
I read your post again, looks like you are talking about inline errors. So just hook into some Newthread hook and use the code i gave above.

Also, please add $count++. I forgot to add it, to increment your errors Smile
Thanks you so much yeah I am talking about inline errors! Please where do I add this "$count++" and how to make this code block people after they get 5 inline errors?
Damn Marcus! You need to learn baby!

Here:
function logger()
{
     if(!isset($mybb->cookies['myplugin_errors']))
     {
           $count = 0;
     }
     else
     {
           $count = $mybb->cookies['myplugin_errors'];
     }
     $count++;
     $expiry = TIME_NOW + (60*60*24);  // Expiry = 1 day
     my_setcookie("myplugin_errors", "{$count}", $expiry, true);
} 

Now, this function will log your error count in the cookie "myplugin_errors". Therefore you can check anywhere this conditon:

if($mybb->cookies['myplugin_errors'] > 5)
{
/* Go to hell, User */
}
Thanks very much man I am sorry for being so dumb with PHP but I am learning it on my own. Your help is very appreciated Smile

How do I implement your code into mine?

$regex = "some filter";

    if(preg_match($regex, $post->data['message'], $match))
    {
        error("some error");
      }
Pages: 1 2