0

在根据 cookie 值显示不同内容的网站上工作。例如:

http://peewee.betaforming.com/

对比

http://peewee.betaforming.com/?cu=10010

该值可以在任何页面上设置,因为我在每个页面上都包含一个功能。如果 cookie 已设置或已保存,则加载该 CU 的信息。如果未设置 cookie 值或传递了数据库中不存在的值,则站点将显示默认信息。

这就是问题所在。如果您从没有设置 cookie 值到请求将“?cu=10010”附加到任何页面的站点,则当前页面在刷新之前不会加载当前数据。

根据我的阅读,我需要使用 header("location....

这是函数文件中用于设置/检索 cookie 的相关代码。

// CU cookies

    if (isset($_GET["cu"]) && is_numeric($_GET["cu"])) {

        $pass_cu = $_GET["cu"];

        // See if passed value returns an active CU record

        mysql_select_db($database_peewee, $peewee);
        $query_rs_valid_cu = "SELECT * FROM tbl_cus WHERE cu_id = $pass_cu";
        $rs_valid_cu = mysql_query($query_rs_valid_cu, $peewee) or die(mysql_error());
        $row_rs_valid_cu = mysql_fetch_assoc($rs_valid_cu);
        $totalRows_rs_valid_cu = mysql_num_rows($rs_valid_cu);

        if ($totalRows_rs_valid_cu != 0) {

            // Set cookie

            $peewee_cu_querystring = $_GET["cu"];
            $expire_month = time()+60*60*24*30; //30 days

            //kill current cookie

            setcookie("peewee_cu", "", time()-10);

            //set new cookie

            setcookie("peewee_cu", $peewee_cu_querystring, $expire_month, "/");

        }

        mysql_free_result($rs_valid_cu);

    }

    // See of cookie exists

    if ((isset($_COOKIE['peewee_cu'])) && $_COOKIE['peewee_cu'] != "") {

        $cu_cookie_value = $_COOKIE['peewee_cu'];

        // Set values for getting CU record

        $colname_rs_cu_data = $cu_cookie_value;
        $load_custom_cu = 'true';

    } else {

        // Set defualt CU value

        $colname_rs_cu_data = 10000;
        $load_custom_cu = 'false';

    }

// Get and Set CU Information (CU specific or default)

mysql_select_db($database_peewee, $peewee);
$query_rs_cu_data = "SELECT * FROM tbl_cus WHERE cu_id = $colname_rs_cu_data";
$rs_cu_data = mysql_query($query_rs_cu_data, $peewee) or die(mysql_error());
$row_rs_cu_data = mysql_fetch_assoc($rs_cu_data);
$totalRows_rs_cu_data = mysql_num_rows($rs_cu_data);

$cu_sidebar_image = $row_rs_cu_data['cu_logo'];
$cu_sidebar_name = $row_rs_cu_data['cu_name'];
$cu_sidebar_link = $row_rs_cu_data['cu_link'];
$cu_sidebar_address = $row_rs_cu_data['cu_address'];
$cu_sidebar_city = $row_rs_cu_data['cu_city'];
$cu_sidebar_state = $row_rs_cu_data['cu_state'];
$cu_sidebar_postal = $row_rs_cu_data['cu_postal'];
$cu_sidebar_phone = $row_rs_cu_data['cu_phone'];
$cu_sidebar_toll = $row_rs_cu_data['cu_phone_toll_free'];

$cu_meta_title = $row_rs_cu_data['cu_name'];
$cu_tab_title = $row_rs_cu_data['cu_name'];

mysql_free_result($rs_cu_data);

// Set default error page for all pages except home page

$default_error_page = 10007;
$default_error_page_home = 10005;

谢谢

布雷特

4

1 回答 1

0

重新加载页面只是为了读取您知道其值的 cookie(因为您刚刚设置了它)似乎有点多余。

相反,您需要做的只是将一个变量设置为浏览器发送的当前 cookie 值 ( $_COOKIE['peewee_cu'])您在当前页面上分配给该 cookie 的值 ( $peewee_cu_querystring)。

对于一个非常简单的方法(但请注意:我不特别建议写入超全局变量,最好拥有自己的变量并正确管理范围)请参阅PHP 设置 COOKIE,使用 JQUERY COOKIE 插件更改,无法使用 php 进行编辑?

顺便说一句,在设置新的 cookie 之前,您不需要杀死旧的 cookie,因为任何具有相同名称、域和路径的新 cookie 都会自动覆盖它。

于 2013-03-20T22:22:44.467 回答