PHP设置并读取Cookie字符串 [英] PHP Set and Read a Cookie String

查看:311
本文介绍了PHP设置并读取Cookie字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成所有表格,我知道如何使用PHP设置单个cookie,但是设置cookie字符串的最佳格式是什么?我想要一个这样的Cookie(或类似的格式,我只是一个示例);



Prefs [theme = this& layout = that]



我该如何设置一个cookie,然后从我的字符串中获取信息?



到目前为止的代码:



 <?php 
if(isset($ _ POST ['submitted'])){
$ a = gmdate( M d YH: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);
};
标头(位置:http://admin.rafflebananza.com/incex.php);
};
?>


解决方案

PHP将允许您使用<$设置字符串值 setcookie()中的c $ c> [] 表示法,您可以多次调用 setcookie(),其中包含两个子键,并以 Prefs 作为名称。



从技术上讲,PHP会设置用于数组元素的多个cookie,但是当从 $ _ COOKIE 读回时,PHP将按照您希望读取数组的方式进行完全排列。



因此您可以将其设置为:

  //并在cookie的 Prefs $ b中设置每个$ b setcookie('Prefs [theme]','this'/ *,$ timeout,$ path,$ domain ... * /); 
setcookie('Prefs [layout]',’that'/ *,$ timeout,$ path,$ domain ... * /);

它将作为 $ _ COOKIE ['Prefs' ]

  print_r($ _ COOKIE ['Prefs']); 
//数组(
// [theme] => this,
// // layout] => that
//)

您可以手动遍历一个$ c>

现有数组。如果只有一层嵌套,这很方便。

  //定义数组
$ prefs = array( 'theme'=>'this','layout'=>'that');
//循环创建键
foreach($ prefs as $ key => $ value){
setcookie( Prefs [$ key],$ value,$ timeout,$ path ,$ domain);
}

如果出于某些原因必须以查询字符串样式& 分隔的字符串,例如 theme = this& layout = that ,您可以先将其解析为数组使用 parse_str()

  parse_str('theme = this& layout = that',$ prefs); 
// $ prefs现在与前面的示例一样。继续使用foreach循环设置
// cookie值...

如果您决定您想以字符串格式存储cookie,可以将该字符串传递到 setcookie(),然后使用 parse_str() $ _ COOKIE 中读回。不过我不喜欢这种方法,我宁愿将cookie设置为上面的数组值。

  //将其设置为字符串
setcookie('Prefs','theme = this& layout = that');
//从$ _COOKIE解析为$ prefs
parse_str($ _ COOKIE [’Prefs’],$ prefs);

更多示例可用 setcookie()文档中。


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");
    };
?>

解决方案

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.

这篇关于PHP设置并读取Cookie字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆