Can a person who has a cold or a cough be prevented from attending prayers in congregation for fear of harm and infection?
Question

We know that the Messenger (blessings and peace of Allah be upon him) did not allow those who had eaten onions or garlic to attend prayers in congregation, so what about those who have the flu, if their attending prayers in congregation will lead to others becoming infected because of the spread of germs due to their continual sneezing? What about those who have continual coughing, if they attend the khutbah on Friday and keep coughing and disturbing the people around them so that they cannot hear the khutbah because of that?.
Praise be to Allah.Firstly: 
Al-Bukhaari (855) and Muslim (564) narrated from Jaabir ibn ‘Abdillah (may Allah be pleased with him) that the Prophet (blessings and peace of Allah be upon him) said: “Whoever eats garlic or onions, let him keep away from us, or keep away from our mosque and stay in his house.” 
Muslim (567) narrated that ‘Umar ibn al-Khattaab (may Allah be pleased with him) delivered a khutbah one Friday in which, among other things, he said: O people, you eat two plants which I find to be nothing but repugnant, this onion and garlic. I remember the Messenger of Allaah (blessings and peace of Allah be upon him), if he noticed their smell coming from a man in the mosque, he would issue orders that he taken out to al-Baqee‘. Whoever eats them, let him cook them to death. 
The fuqaha’ have stated that it is makrooh for the one who has eaten garlic or onions to attend the mosque and that it is mustahabb to expel him therefrom. Some of them are of the view that it is haraam for him to attend and it is obligatory to expel him. They added to that one who has an offensive smell such as body odour or halitosis and those who work in slaughterhouses and the like, if they have a smell that bothers other worshippers. 
Ibn ‘Abd al-Barr (may Allah have mercy on him) said in at-Tamheed (6/422):
The hadeeth mentioned also indicates that the one who has eaten garlic should be kept away from the mosque and should be expelled therefrom, because the Messenger of Allah (blessings and peace of Allah be upon him) said: “He should not approach our mosque or our mosques because he annoys us with the smell of garlic.” If the reason for expelling him from the mosque is that he causes annoyance, then by analogy anyone who annoys his neighbour in the mosque because he has a sharp tongue and insults the people in the mosque, or he has a foul odour because of the nature of his work, or he has a disease that may harm others such as leprosy and the like, and anything that causes annoyance to people if it is present in one of their neighbours in the mosques and they want to expel him from the mosque and keep him away from it, they have the right to do that so long as the reason for doing so is present, until it is no longer present. Once that reason is no longer present, he has the right to return to the mosque. End quote. 
Thus it is known that it is essential to ward off annoyance and harm from the worshippers. If they are bothered by one who has a cold or a cough, and that cannot be treated by means of medicines that would reduce it and lessen the annoyance and harm – of which there are many nowadays – then he should not attend the mosque until that which is bothering other worshippers has gone away. If he can pray at the edge of the mosque or in the courtyard, he may do so. 
In the margin of Asna’l-Mataalib (1/262) it says: If he has an offensive odour and he is able to stand outside the mosque in such a way that he does not cause annoyance, then it is appropriate that he should be obliged to attend Jumu‘ah. End quote. 
Secondly: 
If a man has a sickness that Allah, may He be exalted, has made mixing with people affected by it a cause of contracting that sickness, which is what is called a contagious disease, then he is excused for not attending Jumu‘ah and prayers in congregation, lest he harm other worshippers. In fact he may be prevented from entering the mosque until his illness has gone away, because the Prophet (blessings and peace of Allah be upon him) forbade bringing a sick individual among healthy ones, as al-Bukhaari (6771) and Muslim (2221) narrated from Abu Hurayrah (may Allah be pleased with him) that he said: The Prophet (blessings and peace of Allah be upon him) said: “No sick one should be put with a healthy one.” 
For more information on the issue of contagion, please see the answer to question no. 45694 
Dr. Sulaymaan ibn Waa’il at-Tuwaijri, a member of faculty at Umm al-Qura University was asked about a man affected with a contagious disease (such as measles) – is he obliged to pray in the mosque with the congregation?
He replied: One of the excuses that waive the obligation to pray in congregation and to attend Jumu‘ah is sickness, if recovery from this sickness will be delayed or if it will be made worse. That also includes contagious diseases, the harm of which may be passed to others. In this case the individual is excused and is not obliged to pray in congregation because of his sickness and because of the fear of contagion, because the Prophet (blessings and peace of Allah be upon him) forbade the one who has eaten garlic or onions to come to the mosque lest the people be bothered by his smell, and this one (i.e. the one who is sick) is obviously more bothersome than one who has eaten something that has an offensive smell. And Allah knows best. May Allah send blessings and peace upon our Prophet Muhammad and upon his family and companions. 
End quote from Islam Today website, 
And Allah knows best.