The Messenger of Allah (Peace Be upon Him) said: “Whoever sees an afflicted person then says: ‘All praise is due to Allah who saved me from that which He has afflicted you with, and blessed me greatly over many of those whom He has created, (Al-ḥamdulillāhi alladhī `āfānī mimmabtalāka bihī wa faḍḍalanī `alā kathīrin mimman khalaqa tafḍīla)’ he shall not be struck by that affliction.”
Sunnah al-Tirmidhi (3432), Classed as Saheeh by al-Albani in Saheeh al-Jami' (6248)