This hadeeth was narrated by Ahmad (12173) from Anas ibn Malik (may Allah be pleased with him) from the Prophet (peace and blessings of Allah be upon him), who is reported to have said, “Whoever prays forty prayers in my Mosque, missing no prayer, it will be recorded that he is safe from the Fire, is saved from punishment and is free of hypocrisy.” This is a da’eef (weak) hadeeth.
This was mentioned by al-Shaykh al-Albani in al-Silsilah al-Da’eefah (364), who said: It is da’eef. He also mentioned it in Da’eef al-Targheeb (755) and said, it is munkar (a kind of weak hadeeth).
Al-Albani said in his book Hujjat al-Nabi (peace and blessings of Allah be upon him) (p.185) that it is an innovation (bid’ah) to visit Madeenah and tell the visitors to Madeenah to stay there for a week so that they will be able to offer forty prayers in the Prophet’s Mosque so that they will be free from hypocrisy and saved from the Fire.
Al-Shaykh Ibn Baz said:
With regard to the widespread idea that the visitor should stay for eight days so that he can offer forty prayers in the Mosque is wrong. Although it says in some hadeeths, “Whoever offers forty prayers therein Allah will decree that he is safe from the Fire and free from hypocrisy,” this hadeeth is da’eef according to the scholars and cannot be taken as proof or relied upon. There is no set limit for visiting the Prophet’s Mosque. If a person visits for an hour or two, or a day or two, or for more than that, there is nothing wrong with that.
Fatawa Ibn Baz, 17/406
Instead of this hadeeth we should look at the hasan (sound) hadeeth narrated by al-Tirmithi (241) concerning the virtue of always being present for the opening takbeer (saying Allahu Akbar) of prayer in congregation. It was narrated that Anas ibn Malik (may Allah be pleased with him) said: The Messenger of Allah (peace and blessings of Allah be upon him) said, “Whoever prays for forty days with the congregation, always being present for the first takbeer, it will be written that he will be safe from two things: he will be safe from the Fire and safe from hypocrisy.” Classed as hasan by al-Albani in Saheeh al-Tirmithi, 200.
The virtue mentioned in this hadeeth is general and applies to any mosque where prayers are offered in congregation, in any land; it does not apply only to al-Masjid al-Haram [in Makkah] or al-Masjid al-Nabawi [in Madeenah] in particular.
Based on this, whoever consistently prays forty days in congregation in which he is present for the first takbeer, it will be recorded that he will be safe from two things: safe from the Fire and safe from hypocrisy, whether the mosque is in Madeenah, Makkah or anywhere else.
And Allah knows best.