To say what a website is 'legally' required to do is difficult because there are many websites out there, and the same laws do not apply to all the websites.
A website must legally abide by any law of the country it resides in. Considering the number of porn sites out there it's likely that not all sites are located in the same location, and so not all sites are legally required to abide by the same laws. It's likely that at least some of the porn sites out there reside in countries that don't legally require them to confirm age before allowing someone to enter the site; after all with the number of countries out there some of them likely either are less concerned about underage access to porn or, never thought to explicitly write a law requiring sites to check an age limit, or have laws and/or constitution that would prevent attempts to restrict a website in such a way.
In addition a given website may be viewed from many different countries. A country like the USA or England may want a site located in some other country to restrict access to anyone under the age of 18, but if that website is not hosted in their home country It's possible the country does not have any legal jurisdiction to force that requirement on the site.
So it's quite possible that some of the sites do not legally have to ask you your age. However, they likely consider it safer to do so, for a number of reasons:
First, even if a country can't legally require a site to abide by their rules they can chose to block a site that does not abide by their laws. There are some countries out there that may be willing to block a website that allows anyone to view them that would allow a porn site that restricts access to 18 and up to have access.
Second, porn sites may have have to abide by the laws of multiple countries. For instance a popular site is likely hosted in many different countries spread across the world, this ensures there is always a server close to a potential customer and thus the site is able to give a responsive reply to a request; ie lower latency. The site in question must legally abide by all of these countries laws, and it's possible that at least one of them require age verification. There is also the fact that a country that doesn't currently have laws about checking for age could always change their laws at a later date.
Third, It's possible for individuals to get upset with a site that seems to be allowing any child to view porn without trying to prevent it. A porn site will get fewer angry users complaining, and be less likely to be the target of some hashtag from irate individuals, if they make some basic attempt to restrict access by age.
Finally, the producers of the site may simply believe that individuals under 18 should not view porn and may honestly have the check up to avoid accidental viewing of porn by anyone under age.
The net result is that there are a number of reasons it's generally safer to ask if someone is over 18 first. It avoids having to worry that your site may get in trouble for allowing underage access. Rather or not a specific site is legally required to add those checks it's generally the safer option to do.
As already mentioned the check do not prevent anyone underage from entering the site, but that isn't the point. The point is that the site legally stated that anyone who is under the age of 18 should not enter. If someone lies about their age the site cannot be held accountable for that. They put in a good faith effort to tell someone they should not enter the site, It's unlikely any country that does legally require age checks would require anything more then that, so the site is covered.
On a less cynical note it is possible for a child to accidentally end up on a site they shouldn't have, so these age checks may actually prevent some children from viewing pornography, those who either did not intend to view pornography or those who did, but are honest enough to admit to their age and not enter the website when asked. So the checks likely do something, even if they don't prevent a child committed to viewing pornography form doing so.