The House voted by a wide margin Wednesday to end the National Security Agency's bulk collection of Americans' phone records and replace it with a system to search the data held by telephone companies on a case-by-case basis.

The 338-to-88 vote set the stage for a Senate showdown just weeks before the Patriot Act provisions authorizing the program are due to expire.

If the House bill becomes law, it will represent one of the most significant changes stemming from the unauthorized disclosures of former NSA contractor Edward Snowden. But many Senate Republicans don't like the measure, and Senate Majority Leader Mitch McConnell has introduced a separate version that would keep the program as is. Yet, he also faces opposition from within his party and has said he is open to compromise.

President Barack Obama supports the House legislation, known as the USA Freedom Act, which is in line with a proposal he made last March. The House passed a similar bill last year, but it failed in the Senate.

Most House members would rather see the Patriot Act provisions expire altogether than re-authorize NSA bulk collection, said Rep. Adam Schiff, ranking Democrat on the intelligence committee. "I think the Senate is ultimately going to pass something like the USA Freedom Act," he said.

The issue, which exploded into public view two years ago, has implications for the 2016 presidential contest, with Republican candidates staking out different positions.

The revelation that the NSA had for years been secretly collecting all records of U.S. landline phone calls was among the most controversial disclosures by Snowden, a former NSA systems administrator who in 2013 leaked thousands of secret documents to journalists.

The program collects the number called, along with the date, time and duration of call, but not the content or people's names. It stores the information in an NSA database that a small number of analysts query for matches against the phone numbers of known terrorists abroad, hunting for domestic connections to plots.

Officials acknowledge the program has never foiled a terrorist attack, and some within the NSA had proposed abandoning it even before it leaked — on the grounds that its financial and privacy costs outweighed its counterterrorism benefits.

Proponents of keeping the program the way it is argue that the rise of the Islamic State group and its efforts to inspire Westerners to attack in their own countries make it more important than ever for the NSA and FBI to have such phone records at their disposal to map potential terrorist cells when new information surfaces. And they say there is no evidence the program has ever been misused.

Under the House measure, the NSA would no longer collect and store the records, but the government still could obtain a court order to obtain data connected to a specific number from the phone companies, which typically store them for 18 months.

If the legislation is enacted, "Americans will now rest easy knowing that their calls and other records will not be warehoused by the government, no matter how careful the government is in their procedures to access those files," said Rep. Jim Himes, a Connecticut Democrat on the intelligence committee.

The House measure also provides for a panel of experts to advocate for privacy and civil liberties before the secret intelligence court that oversees surveillance programs. And it allows the government to continue eavesdropping on foreign terrorists without a warrant for 72 hours after they enter the U.S., giving authorities time to obtain such a warrant.

The Senate will have a short window to act before Patriot Act provisions authorizing the phone records program and other counterterrorism-related measures expire June 1. If McConnell's bill passes to reauthorize the law with no changes, that would be seen as a crushing defeat for surveillance opponents.

On Tuesday, NSA Director Admiral Mike Rogers and FBI Director Jim Comey briefed senators on the program. Afterward, Sen. Bob Corker of Tennessee told reporters the NSA was not collecting all the data it should be. He declined to be specific, saying the briefing was classified, but he appeared to be addressing the fact that the collection does not include most mobile calls in an era when many people have stopped using landlines.

"The way it's being implemented today, I don't see how it's ... useful at all to the American people," said Corker, who wants to reauthorize the current law. "And I'm shocked, shocked ... by the small amount of data that is even part of the program. It needs to be ramped up."

U.S. officials have confirmed the mobile records gap, saying it stemmed from technical and policy issues that ultimately would have been addressed absent the Snowden leak. Under the House's USA Freedom Act, they said, the NSA would expand its queries to include mobile records, creating a potentially more effective program. But they have expressed concerns about working out an arrangement with phone providers to standardize the data so the information can quickly be searched.

Those officials, not authorized to comment publicly by name, spoke only on condition of anonymity.