1

Simple Question: Trying to set a cookie for capturing user city in a PHP form - not able to get cookies set despite referring to all related SO threads and tutorials.

cityform.php

<p>Enter Your City?</p>
Enter City:
<?php    
echo "<table align='center'><tr><td><form action='https://example.com/getcityform.php' method='get' target='_top'><input type='text' name='cityname' value='' /><input type='submit' name='submit' value='Submit'></form></table><br>";
?>

getcityform.php

<!DOCTYPE html>
<?php
if (!empty($_GET))
{
    $cookie_name = "city";
    $cookie_value = $_GET["cityname"];
    echo "Value Captured: ".$cookie_value."<br>";
    setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
    header("Location:getcityform.php"); //Included this based on SO thread, but not working
}
?>
<html>
<body>

<?php
if(!isset($_COOKIE[$cookie_name])) {
    echo "Cookie named '" . $cookie_name . "' is not set!";
} 
else
{
    echo "Cookie '" . $cookie_name . "' is set!<br>";
    echo "Value is: " . $_COOKIE[$cookie_name];
}
?>

</body>
</html>

However, the output is always (even after multiple refresh of the page):

Value Captured: New York
Cookie named 'city' is not set!

PS: Referred to W3Schools (https://www.w3schools.com/php/func_network_setcookie.asp) , and also to getting cookie value from php form SO thread. Based on the later, I included the header("Location:getcityform.php");, but still it shows cookie not set error message. Including !empty($_GET) check has also not helped. Other threads, like set cookie in php directly from form input, are also not relevant as they have minor errors like variable names mismatch.

I also tried to set the cookie without taking inputs from the form - i.e. - directly set the cookie name/value in the single php file, but it is still not working.

What may be going wrong here please?

Aquaholic
  • 863
  • 9
  • 25
  • Hello Please remove header("Location:getcityform.php"); and it will work, i have run your code and remove this line, after that the cookies is generating, even i tried to get the cookie city on the cityfom.php page, and its working – Faiz Sandhi Jun 16 '22 at 07:52
  • @FaizSandhi what if a user want to navigate to another page after he set his cookie ? – gsharew Jun 16 '22 at 07:57
  • @FaizSandhi - Thanks, but removing that line, it is still not working. – Aquaholic Jun 16 '22 at 07:58
  • No problem, they will get that cookies – Faiz Sandhi Jun 16 '22 at 07:58
  • is your browser is allowing to store cookies, becouse the code is completely fine and its working well as well – Faiz Sandhi Jun 16 '22 at 07:59
  • @FaizSandhi - yes, browser is set to allow & store cookies. Tried in 3 different browsers, including on Desktop and Mobile – Aquaholic Jun 16 '22 at 08:01
  • @Aquaholic nope if the header is removed it works fine. – gsharew Jun 16 '22 at 08:04
  • @gsharew - somehow, it is not working for me. Is there any live example which you can share please? – Aquaholic Jun 16 '22 at 08:05
  • 2
    @Aquaholic Finally got your answer. the variable $cookie_name is declared as localVariable. declare it outside the if statement. this works for me flawlessly but if you still encountered an error, probably make sure your configuration files works fine. – gsharew Jun 16 '22 at 08:15
  • @gsharew - works perfectly now. Please put it as an answer, and I'll mark it. – Aquaholic Jun 16 '22 at 08:33
  • @Aquaholic answer field is ristricted due to your question. – gsharew Jun 16 '22 at 08:40

0 Answers0