[英]In Next.js getServerSideProps, how can I fetch cookie from browser to identify the user
[英]How to create a cookie to identify the user_IDs
在我網站的消息中心,用戶可以評分並互相反饋。 反饋和評分系統在屏幕上顯示為彈出窗口。 我已經為該彈出窗口創建了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();
}
?>
將用戶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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.