2

I've got my form all completed, I know how to set a singular cookie using PHP however what is the best format to set a cookie string. I would like to have a cookie like so (or similar, my formatting is just an example);

Prefs[theme=this&layout=that]

How would I set a cookie like so and then get the information from my string?

Code So Far:

<?php
    if (isset($_POST['submitted'])) {
        $a = gmdate("M d Y H:i:s");
        $b = "Cookies=true&Cookies_Accepted=" . $a . "";
        $c = $_POST["APT_SELECTED"];
        $d = $_POST["APPT_SELECTED"];
        if ($d == 'Custom') {
            $d = $c;
        };
        $e = $_POST["APL_SELECTED"];
        $f = $_POST["APTNP_SELECTED"];
        $g = $_POST["APSNP_SELECTED"];
        $h = $_POST["APSNM_SELECTED"];
        $i = $_POST["ScreenTimeout"];
        $j = time() + (10 * 365 * 24 * 60 * 60);
        $k = "/admin/";
        $l = "rafflebananza.com";
        $m = array(
            'APCA' => 'true',
            'APCAW' => $a,
            'APT' => $c,
            'APPT' => $d,
            'APL' => $e,
            'APTNP' => $f,
            'APSNP' => $g,
            'APSNM' => $h,
            'APLSA' => $i
        );
        foreach ($m as $n => $o) {
            setcookie("RBAP_Prefs[$n]", $o, $j, $k, $l);
        };
        header("Location: http://admin.rafflebananza.com/incex.php");
    };
?>
1
  • You know you can call setcookie multiple times, one for each value you want to set, right? Commented Jan 29, 2015 at 17:13

1 Answer 1

5

PHP will allow you to set string values using [] notation in setcookie(), You may make multiple calls to setcookie() with your two sub-keys, and Prefs as the name.

Technically, PHP will set multiple cookies for array elements, but when read back from $_COOKIE, PHP will arrange it exactly as you would expect to read the array.

So you may set it as:

// And set each in the cookie 'Prefs'
setcookie('Prefs[theme]', 'this' /*, $timeout, $path, $domain... */);
setcookie('Prefs[layout]', 'that' /*, $timeout, $path, $domain... */);

And it will be readable as an array in $_COOKIE['Prefs']

print_r($_COOKIE['Prefs']);
// Array (
//   [theme] => this,
//   [layout] => that
// )

Rather than manually calling setcookie() for each one, you may loop over an existing array. This is handy if you have only one level of nesting.

// Define your array
$prefs = array('theme' => 'this', 'layout' => 'that');
// Loop to create keys
foreach ($prefs as $key => $value) {
  setcookie("Prefs[$key]", $value, $timeout, $path, $domain);
}

If for some reason you must begin with a query-string style & delimited string like theme=this&layout=that, you may first parse it into an array using parse_str().

parse_str('theme=this&layout=that', $prefs);
// $prefs is now as in the previous example. Proceed to set
// cookie values with the foreach loop...

If you decide you would like to store the cookie in the string format, you may pass that string into setcookie() and then use parse_str() to read it back out of $_COOKIE. I don't like this method though, I would rather see the cookie set as array values above.

// Set it as a string
setcookie('Prefs', 'theme=this&layout=that');
// And parse it from $_COOKIE into $prefs
parse_str($_COOKIE['Prefs'], $prefs);

More examples are available in the setcookie() documentation.

Sign up to request clarification or add additional context in comments.

9 Comments

Just configuring my array, getting all the values. I'll comment with some real feedback soon @MichaelBerkowski
@TimMarshall See also the addition about parse_str() if you want to stick with your string format as the originating format, but still store them as an array.
I have no SSL at the moment so the last option seems best as I'm limited to two cookies. I'm still getting this ready and once I'm done I'll edit question with my code @MichaelBerkowski
updated question with my PHP code. Do i need to set $timeout, $path, $domain?
@TimMarshall It's in $_COOKIE as a 2D array, so for example echo $_COOKIE['RBAP_Prefs']['APSNM']
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.