【问题标题】:How to create a cookie to identify the user_IDs如何创建 cookie 来识别 user_IDs
【发布时间】:2017-12-13 20:45:39
【问题描述】:

在我网站的消息中心,用户可以互相评分和反馈。反馈和评级系统以弹出窗口的形式出现在屏幕上。我已经为此弹出窗口创建了 cookie,如果用户关闭弹出窗口或提交评分/反馈,则此弹出窗口仅在 24 小时后再次显示(Cookie 在 1 天后过期)。这里的问题是,一旦弹出 关闭/提交有关对特定用户进行评分的窗口,然后 cookie 不会让弹出窗口为聊天列表中的其他用户打开(Cookie 不会识别聊天列表中的用户)。我想用 cookie 来识别聊天列表中的用户。例如,如果我提交或关闭特定用户的弹出窗口,那么我只能在第二天给该特定用户提供反馈/评分。但我现在可以选择聊天列表中的其他用户并给出反馈/评分。

下面看代码。

<?

                if($chain_author->id == Yii::$app->user->identity->id){
                $form = ActiveForm::begin(['method' => 'POST', 'action' => ['/message/send-feed/'], 'options' => ['id' => 'send_feed_form', 'title' => Yii::t('title', 'evaluate'), 'style'=>'display:none']]);

                // show star & feedback form
                if(isset($_COOKIE['send-feed-form']))
                {
                    echo 'show nothing';
                }
                else {                  
                echo $this->render('/message/desktop/send-feed', ['chain' => $chain], ['style'=>'display:none']);
                echo \frontend\widgets\FeedStar::widget(['type' => 'form', 'active_form' => $form]);
                $button_options = (!$chain)?['class' => 'button button-send', 'disabled' => 'disabled']:['class' => 'button button-send'];
                echo Html::submitInput(Yii::t('button', 'send'), $button_options);
                echo Html::hiddenInput('chain', $cur_chain);
                echo '<script type="text/javascript">'; 
                echo '$(document).ready(function() {
                        $( function() {
                        $( "#send_feed_form" ).dialog({
                            modal: true,
                            width: 700,
                            height: 600,
                        });
                      });
                    });';

                echo '</script>';
                setcookie ('send-feed-form', 'yes', time() + 86400);
                }
                ActiveForm::end();
                }
               ?>

【问题讨论】:

    标签: javascript php cookies yii2


    【解决方案1】:

    如果将用户 ID 附加到 cookie 名称中呢?

    $id = $chain_author->id;
    setcookie ('send-feed-form'.$id, 'yes', time() + 86400);
    

    然后选择那个特定的 cookie:

    if(isset($_COOKIE['send-feed-form'.$id]))
        {
            echo 'show nothing';
        }
    

    看起来像这样 $userid 是接收者的 id:

    $userid = $msg->sentFrom->id;
    $form = ActiveForm::begin(['method' => 'POST', 'action' => ['/message/send-feed/'], 'options' => ['id' => 'send_feed_form'.$userid, 'title' => Yii::t('title', 'evaluate'), 'style'=>'display:none']]);
    
    if(isset($_COOKIE['send-feed-form'.$userid]))
    {
        echo 'show nothing';
    }
    else {                  
    echo $this->render('/message/desktop/send-feed', ['chain' => $chain], ['style'=>'display:none']);
    echo \frontend\widgets\FeedStar::widget(['type' => 'form', 'active_form' => $form]);
    $button_options = (!$chain)?['class' => 'button button-send', 'disabled' => 'disabled']:['class' => 'button button-send'];
    echo Html::submitInput(Yii::t('button', 'send'), $button_options);
    echo Html::hiddenInput('chain', $cur_chain);
    echo '<script type="text/javascript">'; 
    echo '$(document).ready(function() {
            $( function() {
            $( "#send_feed_form"'.$userid.' ).dialog({
                modal: true,
                width: 700,
                height: 600,
            });
          });
        });';
    
    echo '</script>';
    setcookie ('send-feed-form'.$userid], 'yes', time() + 86400);
    }
    

    【讨论】:

    • 非常感谢您的回答@tbedner。我已经按照你的建议尝试了,但恐怕它没有用。如果您有任何其他解决方案,请与我分享您的意见。提前谢谢!
    • @RasimAghayev 好的,$id 应该等于接收评级的人的用户 ID,所以也许我的 $id 值错误?这样,将为每个唯一的反馈/评级实例设置一个 cookie。你能确认cookie被设置了吗?
    • 亲爱的@tbednar,你是对的,那是错误的 id。我试图为 $id 赋予接收评级/反馈的人的 id 值。我可以说以这种方式设置了 cookie,但问题是虽然我将 $id 等同于 "$id = $msg->sentFrom->id" ,但在开发人员工具中我只看到 cookie 名称为 "send-饲料形式1”。这里的数字 1 是接收反馈的用户的 ID 号。不幸的是,其他应该接收的用户的 ID 无法识别。
    • 一旦关闭/提交关于评价特定用户的弹出窗口,cookie 仍然不会让聊天列表中的其他用户打开弹出窗口
    • @RasimAghayev,因此提交反馈的用户将无法为该接收者提交更多信息,对吗?还应该接收的其他用户是谁?您是说要限制反馈,以便接收者在 24 小时内只能有 1 个反馈实例,或者单个用户每天每个接收者不能提供超过一次的反馈?如果你能澄清,我相信我们可以解决这个问题。谢谢。
    猜你喜欢
    • 2018-03-02
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 2011-04-25
    • 1970-01-01
    • 1970-01-01
    • 2012-06-28
    • 2023-03-28
    相关资源
    最近更新 更多