I am working on a multilingual site so I tried this approach:
echo $_COOKIE["lg"];
if (!isset($_COOKIE["lg"]))
setcookie("lg", "ro");
echo $_COOKIE["lg"];
The idea is that if the client doesn't have an lg
cookie (it is, therefore, the first time they've visited this site) then set a cookie lg = ro
for that user.
Everything works fine except that if I enter this page for the first time, the first and second echo
return nothing. Only if I refresh the page is the cookie set and then both echo
print the "ro" string I am expecting.
How can I set this cookie in order to see its value from the second echo
on the first visit/page load of the user? Should be without needing to refresh the page or create a redirect.