Pakistan on Thursday shut its porous border with landlocked Afghanistan after opening it for two days, saying the measure was necessary to save Pakistanis from attacks from militants operating inside Afghanistan.

Nafees Zakaria, spokesman for Pakistan's foreign ministry, told reporters the country was forced to close the border because of the threat of terrorism from militant groups like Tehrik-e-Taliban, the Islamic State group and Jamaat-ul-Ahrar, which are located in Afghanistan and trying to carry out attacks in Pakistan.

He spoke hours after Pakistan shut the two main border crossings with Afghanistan after a temporary, two-day opening that enabled more than 35,000 people to cross the border.

Pakistan, which shares the 2,200 kilometer (1,375 mile) long border with Afghanistan, initially shut all the border crossings after a string of militant attacks in the country last month killed over 125 people. Islamabad has accused Kabul of failing to take action against militant sanctuaries in Afghanistan's border regions.

Kabul has denied the allegation.

According to border management officials Fayyaz Khan and Irfan Toor, the Torkham and Chaman crossings would be shut indefinitely, despite the pleas of stranded Afghans who want to return to their country.

The crossings are also major arteries for trade and commerce between the two neighbors. Businessmen say they are suffering losses because trucks carrying supplies to Afghanistan were not allowed to cross the border during the brief reopening of the crossings.

Pakistan and Afghanistan have long accused each other of failing to combat extremists who operate along the porous frontier between the two nations.

On Thursday, Zakaria said militants were still launching attacks from Afghanistan that targeted the Pakistan army. "We continue to lose soldiers" as a result of these attacks, he said.

"In order to provide some relief to the stranded people on both sides, we opened the border for two days," he said and added that the border management was the most important aspect to control cross border movement of terrorists, which was hurting both sides.

Zakaria said it was in the interest of both Pakistan and Afghanistan to install an effective border management mechanism and he hoped that Kabul will cooperate with Islamabad in managing the border.