How I can hide error messages for anonymous users?
Is there any module which handle that?
Especially errors generated by PHP and MySQL syntax errors.

Comments

sumitshekhawat7331’s picture

hi just remove this line from your page.tpl.php

  if ($show_messages && $messages): print $messages; endif; ?>
ultimateboy’s picture

Status: Active » Postponed

The above will remove error messages for all users, which usually is not a good thing. The problem is that drupal has a very narrow way of classifying messages. There really are only three options: status, error, and warning. Therefore there is no easy way to classify that a certain type of message be removed without harmful side effects. PHP and mySQL errors have a type of "error", but so does not filling in a required field in a form... so you cannot classify that way. I honestly do not think there would be a way to achieve the effect you are looking for without adding more parameters to drupal_set_message(), which is an interesting idea, but at this time, I do not see this as being a huge problem that needs solving at this point in time. It is my opinion that a site should not launch with php and mysql errors...

ultimateboy’s picture

Status: Postponed » Closed (won't fix)

Actually, won't fix is probably a better classification.

kenorb’s picture

Status: Closed (duplicate) » Postponed (maintainer needs more info)

Looking for it as well.
You can classify it by looking for some phrases and ignoring all messages which contains some errors or security information:

Unknown column
user warning:
/home/
query: SELECT
INSERT INTO
node_title
module on line
inc on line
php.ini

etc.
I think it can be done somehow.

kenorb’s picture

Status: Closed (won't fix) » Postponed (maintainer needs more info)

My temporary quick-fix in page.tpl.php file;)

global $user;
if (strpos($messages,'node.nid')!==FALSE && $user->uid > 1) {
    $messages = '';
}

if ($show_messages && $messages != ""):
print $messages
endif;

Related to this bug: #331651: db_rewrite_sql improperly rewriting a subquery

ultimateboy’s picture

Status: Postponed (maintainer needs more info) » Closed (duplicate)

Error message handling has been improved in d7.

Marking as duplicate of the now committed patch: http://drupal.org/node/291026

kenorb’s picture

Still looking for some solutions for 6.x

grobemo’s picture

Keeping in mind the warnings that everyone gave above, if you still want to hide messages from anonymous users, you could change this line in page.tpl.php:

<?php if ($show_messages && $messages): print $messages; endif; ?>

to this:

<?php if (user_is_logged_in() && $show_messages && $messages): print $messages; endif; ?>

This would hide all messages from anonymous users, though—not just error messages. Anyone know how to refine it to show, say, status messages, but not error messages or warnings?

szy’s picture

Sorry, now I see it's duplicate :/

Szy.

kenorb’s picture

grobemo: I know how to refine it to show, look #4

mariagwyn’s picture

Status: Postponed (maintainer needs more info) » Closed (duplicate)

@grobemo: can this be refined to specify user role? Like only for admin or site editor roles?

Thanks,
Maria

grobemo’s picture

Yes, though anything beyond checking for administrator status starts getting to the point where you should think about moving the code to template.php.

To restrict messages to the administrator:

<?php if ($is_admin && $show_messages && $messages): print $messages; endif; ?>

To restrict to a particular role (e.g., 'editor'):

<?php
  global $user;
  if (in_array('editor', array_values($user->roles)) && $show_messages && $messages): print $messages; endif; 
?>

Restricting to any of several roles is a bit more complicated. See the PHP documentation for in_array(), especially the first comment (by Thingmand).

kenorb’s picture

Component: base system » other
Status: Closed (duplicate) » Active

For me it's not duplicate, can't use patches from 7.x on 6.x
There are already some solutions in this thread, any more ideas?

dropchew’s picture

To #8

Is using Css class to hide status, error or warnings a good idea? I can see there's an css class 'error' for error messages, not sure about status and warnings...

Btw subscribing, what I need is to restrict messages on certain pages only, does anyone have a solution?

grobemo’s picture

Hmm...I'm #8, but I'm not sure why this question is directed at me. I'm also not sure whether the question is rhetorical or not—that is, whether you mean to suggest that using CSS is not a good idea. But for what little it's worth, here's my two cents:

Hiding messages with CSS is easy. Messages come wrapped in divs with the class messages. Errors, as you note, have the class 'error', warnings have class 'warning', and I'm pretty sure status messages have class 'status'. The following CSS declaration would hide all messages:

div.messages {
  display: none;
}

As I see them, the advantages of hiding messages via CSS are that it's an easy to do and that you can still see the messages by looking at source code, which means that you (the developer) can still see what Drupal is telling you, even when viewing your site as an anonymous user. The main disadvantage is that "real" anonymous users could also see what Drupal is telling you by looking at the source code. At best, that's slightly unprofessional; at worst, it might make security breaches easier.

If your site is really working the way you want it to, it shouldn't really matter, since users shouldn't be generating messages that you don't want them to see anyway. (I realize that this is easier said than done!)

If other people have thoughts on that, please share.

As for hiding messages on certain pages, I don't know whether Drupal feeds messages through a specific template. You could try something like the following in the THEME_preprocess_page function of your theme's template.php:

<?php

if (CONDITION) {
  unset($variables['messages']);
}

?>

That should prevent messages from displaying on any page that meets CONDITION. (For instance, if CONDITION is !user_is_logged_in(), this snippet should hide messages from all anonymous users.)

kenorb’s picture

Status: Closed (fixed) » Fixed

As discussed in #4 I finally found some time:
Drupal Tweaks

You can select which messages you can hide and for which role from UI (done programmatically, not by CSS).

Status: Active » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

ikeigenwijs’s picture

Status: Fixed » Closed (fixed)

Just to keep track of this issue

Ross-Hunter’s picture

#16 solves this problem pretty well. Thanks!

interestingaftermath’s picture

Any way to just hide error messages from anonymous users not ALL messages?

tourendal’s picture

subscribe

tyler.frankenstein’s picture

Use theme_status_messages(), for example:

Inside the for loop...

foreach (drupal_get_messages($display) as $type => $messages) {
  if (!user_is_logged_in() && $type == "error") { continue; }
  ...
  ...

The above code will skip the rendering of all error messages for anonymous users.

anoopjohn’s picture

You can use the module - http://drupal.org/project/disable_messages - to hide messages from anonymous users.

ywarnier’s picture

#22 works well for me. Thanks @tyler.frankenstein

beto_beto’s picture

Go to Administer > Site configuration > Error reporting (admin/settings/error-reporting). There you have a dropdown list under 'Error reporting' with the two choices you have for the error logging, being:

Write errors to the log.
Write errors to the log and to the screen.

The page even contains a hint to guide you in the right direction:

Where Drupal, PHP and SQL errors are logged. On a production server it is recommended that errors are only written to the error log. On a test server it can be helpful to write logs to the screen.

Just choose 'Write errors to the log.' and your visitiors won't be bugged by (to them) meaningless error messages.

you can read this link it's useful :

http://www.drupalcoder.com/blog/disable-error-reporting-to-the-screen-on...

giorgio79’s picture