Upon having a nightmare, recite:
أَعُوذُ بِكَلِمَاتِ اللَّهِ التَّامَّةِ مِنْ غَضَبِهِ وَعِقَابِهِ وَشَرِّ عِبَادِهِ وَمِنْ هَمَزَاتِ الشَّيَاطِينِ وَأَنْ يَحْضُرُونَ
With the total words of Allah, I seek protection from His wrath, from His punishment and from the servants evil and from the whispers of Satan and I seek protection from (these) coming to me.
Bad dreams are those in which the sleeper sees bad things; they come from the shaytaan. It is Sunnah to seek refuge with Allaah from them and to spit to the left three times, and not to talk about them, but if a person does that it will not harm him. It is also mustahabb to turn onto one’s other side, and to pray two rak’ahs.
The Prophet (peace and blessings of Allaah be upon him) told us how to rid ourselves of the influence of Shaytaan during sleep. This can be achieved by reciting verses from the Qur’aan and du’aa’s narrated from him, before going to sleep.
It was reported that Abu Hurayrah (may Allaah be pleased with him) said: “The Messenger of Allaah (peace and blessings of Allaah be upon him) put me in charge of guarding the zakaah of Ramadaan. Someone came and started to scatter the food. I took hold of him and said, ‘I will take you to the Messenger of Allaah (peace and blessings of Allaah be upon him).’… He said, ‘When you go to bed, recite Aayat al-Kursi and you will be protected by Allaah, and no shaytaan (devil) will come near you until morning.’ The Prophet (peace and blessings of Allaah be upon him) said, ‘He told you the truth even though he is a liar. That was a shaytaan.’” (Narrated by al-Bukhaari, 3101).