使用会话或 cookie 显示/隐藏 div

发布于 2024-10-30 05:29:21 字数 332 浏览 3 评论 0原文

我需要隐藏或显示一个内部有幻灯片的 div。 这个想法是为用户提供一个隐藏或显示 div 的链接。

目前,我使用

用户单击链接以隐藏/显示 div 后,我想调用第二个文件( ) 其中包含不同的 div。

据我发现,没有办法通过会话来实现这一点,或者至少我没有找到解决这个问题的方法。

我的猜测是这需要用cookie来完成,但我不明白如何做。

I need to hide or show a div that have a slideshow inside.
The idea is to give to the users a link for them to hide or show the div.

At the moment I call the slideshow on the body of the page with <?php include('slideshow.php'); ?>

After the user clicks on the link to hide/show the div I will like to call a second file (
<?php include('no-slideshow.php'); ?> ) which contain a diferent div.

As far as I had found there is no way to achieve this with sessions, or at least I did't find a solution to this problem.

My guess is that this need to be done with cookies, but I don't understend how.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

下壹個目標 2024-11-06 05:29:21

如果您不想使用 JavaScript,实现这一点的唯一方法是使用链接(需要重新加载页面)。

这只是基本逻辑,不是完整的解决方案,但我想您会明白的。

在网页上创建到服务器端脚本的链接。

<a href="toggle_visibility.php">hide/show</a>

创建脚本togle_visibility.php来处理用户的请求。

<?php

  $hidediv = isset($_COOKIE['hide_div']) && ($_COOKIE['hide_div'] == 'hide');
  $cookie_value = !$hidediv ? 'hide' : 'show';
  setcookie('hide_div', $cookie_value, time()+32000000); // cookie expires after year
  header('location: http://www.mysite.com/index.php');

?>

您现在所需要做的就是(返回原始页面后)检查 cookie 中存储的值并决定是否不向用户显示该 div。

<?php

  ... more code

  $hidediv = isset($_COOKIE['hide_div']) && ($_COOKIE['hide_div'] == 'hide');
  if ($hidediv) {
    include('no-slideshow.php');
    }
  else {
    include('slideshow.php');
    }

  ... more code 

?>

编辑:$hidediv 条件。

如果用户禁用了 JavaScript,则该方法有效,但如果在浏览器设置中禁用了 cookie,则该方法无效。

我没有检查此代码,因此可能存在相同的拼写错误。

If you don't want to use JavaScript only way to achieve that is to use link (requires page to be reloaded).

This is basic logic only, not a complete solution, but think you'll get the point.

Create a link on web page to the server-side script.

<a href="toggle_visibility.php">hide/show</a>

Create script togle_visibility.php to process user's request.

<?php

  $hidediv = isset($_COOKIE['hide_div']) && ($_COOKIE['hide_div'] == 'hide');
  $cookie_value = !$hidediv ? 'hide' : 'show';
  setcookie('hide_div', $cookie_value, time()+32000000); // cookie expires after year
  header('location: http://www.mysite.com/index.php');

?>

All you need now (after return to original page) is to check value stored in cookie and decide do you want od not to show that div to do user.

<?php

  ... more code

  $hidediv = isset($_COOKIE['hide_div']) && ($_COOKIE['hide_div'] == 'hide');
  if ($hidediv) {
    include('no-slideshow.php');
    }
  else {
    include('slideshow.php');
    }

  ... more code 

?>

EDIT: $hidediv condition.

It works if user has JavaScript disabled but doesn't work if cookies has been disabled in browser settings.

I did not check this code, so same typos are possible.

赠佳期 2024-11-06 05:29:21

我不确定我是否理解了。您想在用户点击链接后隐藏 div 吗?

你为什么不使用 javascript 来做到这一点? (隐藏/显示 div?)

I am not sure I understood. You want to hide div once user cliks on a link?

Why are you not doing this using javascript? (hide/show the divs?)

じее 2024-11-06 05:29:21

此命令将设置一个名为include的cookie具有值要包含的内容

setcookie('include', 'what to include', time()+86400);

您可以在包含之前检查此 cookie,如下所示:

if (isset($_COOKIE['include'])) {
    include($_COOKIE['include'] . '.php');
} else {
    include('slideshow.php');
}

注意:由于 cookie 很容易被伪造,因此您需要检查两次要包含的内容。

This command will set a cookie named include with value what to include.

setcookie('include', 'what to include', time()+86400);

You can check this cookie before include like this:

if (isset($_COOKIE['include'])) {
    include($_COOKIE['include'] . '.php');
} else {
    include('slideshow.php');
}

Note: because cookies can be easily faked you'll need to check twice what to include.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文