Umar bin Khattab reported,

Ibn Dinar, the freed slave of the family of Zubayr, reported from Salim bin Abdullah bin Umar, from Ibn Umar, from Umar, that the Messenger of Allah ﷺ said, "Whoever sees someone afflicted with a trial and says, 'Praise be to Allah who has spared me from what He has tested you with and favored me greatly over many of those He has created,' will be protected from that affliction, whatever it may be, for as long as he lives."

Tirmidhi 3431

Daif

حدثنا محمد بن عبد الله بن بزيع حدثنا عبد الوارث بن سعيد

عن عمر

و بن دينار مولى آل الزبير عن سالم بن عبد الله بن عمر عن ابن عمر عن عمر أن رسول الله ﷺ قال من رأى صاحب بلاء فقال الحمد لله الذي عافاني مما ابتلاك به وفضلني على كثير ممن خلق تفضيلا إلا عوفي من ذلك البلاء كائنا ما كان ما عاش

الترمذي 3431ضعيف
Contents
Reference
Commentary
Ayat
Collection
Jami Tirmidhi
Book
Supplications
Chapter
What to say upon seeing an afflicted person
Numbering
Darussalam 3431
Tirmidhi 3431