After having a nightmare
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.
On having a good dream, recite:
"Alhamdulillah" and relate it to an intimate companion of understanding (So that the companion does not interpret the dream badly). If a person has a bad dream, then spit three times over the left shoulder and turn to the other side or stand up to perform Salaat reciting the following dua three times:
أَعُوذُ بِاللَّهِ مِنَ الشَّيْطَانِ الرَّجِيمِ وَمِنْ شَرِّ هَذِهِ الرُّؤْيَا
"I seek refuge in Allah from the accursed Satan and from the evil of this dream. "The dream will not cause any harm. (Misjkat, Hisnul Hasin)
It is a great sin to conjure a false dream and narrate it.