PHP Set and Read a Cookie String

。_饼干妹妹 提交于 2020-01-04 05:56:12

问题


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:


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.



来源:https://stackoverflow.com/questions/28220280/php-set-and-read-a-cookie-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!