David Cameron has warned British troops in Afghanistan that they face the “struggle of our generation” against Islamic extremists around the world.
The prime minister told troops at Camp Bastion their work would continue elsewhere in the world, but said “maybe not in the same way” as in Afghanistan.
Troops should be “incredibly proud” of their efforts in the country, he added.
The PM also told new Afghan President Ashraf Ghani that Britain will always be a “strong partner and good friend”.
His visit to Camp Bastion comes as the base is due to close in a few weeks' time, with British combat operations in Afghanistan due to finish by the end of this year after a 13-year campaign.
There are about 1,300 British troops still present at Camp Bastion. A small number of soldiers will remain in the country to train Afghan army officers.
Mr Cameron said the actions of groups like Islamic State (IS) in Iraq and Syria and Boko Haram in Nigeria showed there remains a long battle ahead.
“I am afraid the work that you are doing here will go on in other parts of the world, maybe not in the same way, maybe not with troops on the ground,” he said.
“It is about protecting ourselves in our streets, in our homes, in our towns.
“These people have declared war upon us and we must make sure, as Britain always does in our history, that we respond with robustness and resolution,” he added.
His visit to Afghanistan comes after he visited RAF Akrotiri in Cyprus, from where RAF Tornados are launching air strikes against IS militants in Iraq.
Two more RAF Tornado jets are being deployed, joining the six already based in Cyprus.
The planes have carried out air strikes on four of their missions so far, hitting eight targets at five locations.
Holding a joint press conference with Mr Ghani in Kabul, Mr Cameron said British armed forces had paid a “very high price” for their involvement in Afghanistan,
British forces have gone “a long way” towards achieving their objectives in the country Mr Cameron said, while Mr Ghani thanked the families of those who had lost their lives there.
“We all share a common goal – which is a more secure, stable and prosperous Afghanistan,” Mr Cameron said.
He spoke of the progress made in Afghanistan in recent years, adding: “The people of Afghanistan deserve an effective and legitimate government that will build a better future for them and their families.”
“Together we have made Afghanistan safer, we have made Britain safer,” he added.
There is no prospect of the UK going back to fight in Afghanistan because there is now an effective army and police force in place, a process that had taken “hard, patient work”, he added.
Mr Cameron also paid tribute to the 453 UK servicemen and women who have died while serving in the country, as well as those who have been injured.
“We should remember those who paid the ultimate price and those who were injured through the work they did,” he said.
Mr Ghani said British troops had stood “shoulder to shoulder” with Afghanistan and thanked those injured while serving in the country.
“Your presence has meant London has been safe, as well as the rest of the world,” he said, adding: “We face joint threats.”
Mr Cameron said the UK would continue to support the Afghan National Officer academy and would provide £178m a year, until 2017, to “sustain the major progress” the country had made on public services like health and education.
The prime minister is the first world leader to meet Afghanistan's new government.
BBC deputy political editor James Landale said Mr Cameron, who will also be holding talks with new Chief Executive Abdullah Abdullah – effectively Afghanistan's prime minister – wanted to “get in early” with the new leadership.
Mr Ghani took office four days ago after coming to a power-sharing agreement with presidential rival Mr Abdullah.
The UK is one of the largest financial donors to the Afghan government.
During the visit, Mr Cameron and Mr Ghani are also due to discuss a crucial conference to be held in London next month which will consider future aid to Afghanistan.