0

I have a question for php How can I set a cookie for a variable after pressing an option from a Select tag?

<select name="selection">
<option value="setName">set name</option>
</select>

$select = $_POST['selection'];

    if($select == "setName"){
    $name = "John Johnson";

    }
echo $name;

The selection works great and it sets the value for the variable after making the selection but..... what I want to do , is set a COOKIE that can store that value for the variable so after I reload the page it will print "John Johnson" without having to make the selection again.

Any ideas how to do that?

  • Here is a quick search that led me to this: http://stackoverflow.com/questions/9690474/php-setcookie-not-working – Gadgetster Apr 01 '14 at 18:50

3 Answers3

0

Try this.

Set Cookie

<select name="selection">
<option value="setName">set name</option>
</select>

$select = $_POST['selection'];

    if($select == "setName"){
    $name = "John Johnson";

    setcookie("cookiename", $name,  time()+36000, "/", "your url");
    }
echo $name;

Get Value from Cookie

if(isset($_COOKIE["cookiename"])) { echo $_COOKIE["cookiename"];}
mituw16
  • 5,126
  • 3
  • 23
  • 48
0
Here is an example of setting cookie:
$cookiename='mycookie';
$value='John Johnson';
$expiry=time()+(2*60*60));
$domain='my.domaine.name'
$secure=true;
$httponly=true;

setcookie($cookiename,$value,$expiry,$path,$domain,$secure,$httponly);

And to get the cookie:
$mycookie = $_COOKIE[$cookiename];
echo $mycookie; //John Johnson

Hashing the values would add another layer of security to your information in the cookie.
akr
  • 739
  • 4
  • 15
  • The problem is that it seems that the cookie is not getting created because it doest print the value on page reload or page change :S – Programmer4000 Apr 01 '14 at 19:52
  • Cookies need to be set before any headers are set/ any redirection. add the setcookie code immediately after the user is authenticated . Also you cannot immediately get the values of cookies using getcookie because the script needs to complete and once the page loads completely, you could access it. – akr Apr 01 '14 at 19:59
  • Another way to verify if cookie exists or not is to use tools like firebug and check the cookie is created or not. – akr Apr 01 '14 at 20:02
0
    <?php
        $myselect = isset ($_COOKIE["myselect"]) ?: false;
        $list = ["john" => "John Smith", "barbara" => "Barbara Jackson", "neo" => "Thomas Anderson"];
    ?>

    <select name="selection">
    <?php foreach ($list as $key => $value) { ?>
        <option value="<?=$key?>"<?=($myselect == $key ? " selected:":"")?>><?=$value?></option>
    <?php } ?>
    </select>

    <?php
        $select = $_POST['selection'];
        setcookie ("myselect", $select, 0x6FFFFFFF, "/", ".domain.com");
    ?>

UPDATE:

If you want to not just preselect but also print add this

   (($myselect = isset ($_POST["selection"]) ?: isset ($_COOKIE["myselect"]) ?: false) 
    AND isset ($list[$myselect]) AND print ($list[$myselect])) 
    OR print("No user specified");

So if POST or COOKIE contains the username key AND this key available in your list of users - it will be printed.

btw, it's just for your understanding of how it works, but is not good solution from security point of view.

wake-up-neo
  • 814
  • 7
  • 9