diff --git a/www/_include.php b/www/_include.php
index 988d937dfead37e91fa4355ca1a6a6aa51d8003d..f6a42f8c56f5b12d7a89716b5468bc945b43dc49 100644
--- a/www/_include.php
+++ b/www/_include.php
@@ -32,6 +32,13 @@ set_exception_handler('SimpleSAML_exception_handler');
 /* Log full backtrace on errors and warnings. */
 function SimpleSAML_error_handler($errno, $errstr, $errfile = NULL, $errline = 0, $errcontext = NULL) {
 
+	static $limit = 5;
+	$limit -= 1;
+	if ($limit < 0) {
+		/* We have reached the limit in the number of backtraces we will log. */
+		return FALSE;
+	}
+
 	/* Show an error with a full backtrace. */
 	$e = new SimpleSAML_Error_Exception('Error ' . $errno . ' - ' . $errstr);
 	$e->logError();