如何在Wordpress中使用我自己的自定义会话值?
-
-
您能否说明问题的** WordPress特定**性质?Can you please clarify the **WordPress-specific** nature of your question?
- 0
- 2011-11-01
- Chip Bennett
-
WordPress的特定性质是什么?What is WordPress-specific nature ?
- 0
- 2011-11-01
- 夏期劇場
-
" *什么是WordPress特有的性质?*"-表示您的问题与** WordPress **有什么关系?"*What is WordPress-specific nature ?*" - that means, in what way is your question related to **WordPress**?
- 1
- 2011-11-01
- Chip Bennett
-
我的问题与WordPress有什么关系?因此,请阅读我的问题,您将了解与Wordpress有关的内容.In what way is my question related to WordPress?? So, please read my question and you will understand what is relating to Wordpress.
- 0
- 2011-11-01
- 夏期劇場
-
我看到一个有关"会话Cookie"的问题.我没有看到任何特定于** WordPress **的内容,这就是为什么我要求澄清的原因.I see a question about **session cookies**. I don't see anything specific to **WordPress**, which is why I asked for clarification.
- 1
- 2011-11-01
- Chip Bennett
-
与Wordpress有关的** session **或** cookies **`NOT`是否与之相关? (或)WordPress是否不使用其中任何一个?Does **session** or **cookies** `NOT` relating to Wordpress? (or) Does WordPress not using any of them ?
- 0
- 2011-11-01
- 夏期劇場
-
不.*会话*和* cookies *是一般的Internet/Web浏览器主题,WordPress完全与之无关.No. *Sessions* and *cookies* are general internet/web-browser topics toward which WordPress is entirely agnostic.
- 2
- 2011-11-01
- Chip Bennett
-
@Chip Bennett,所以.. WordPress的Sessions/Cookies没有任何问题吗?对于Wordpress,没有人应该问这些问题.......?因此,我认为您对这些问题没有足够的WordPress使用经验.@Chip Bennett, So.. does WordPress is not having any problems (relating) with Sessions/Cookies ??? No one should ask about these problem for Wordpress ?????? Huh...? So I think you don't have enough experience with WordPress for these kinds of problems.
- 0
- 2011-11-01
- 夏期劇場
-
我会建议[阅读有关问题范围的WordPress StackExchange常见问题解答](http://wordpress.stackexchange.com/faq#questions).I would recommend [reading the WordPress StackExchange FAQ regarding scope of questions asked](http://wordpress.stackexchange.com/faq#questions).
- 0
- 2011-11-01
- Chip Bennett
-
是的,会话和cookie是通用主题,但这更多是使会话与WordPress配合使用的问题……而且由于WP本身不使用会话,因此此处很重要.Yes, sessions and cookies are general topics, but this is more a question of making sessions work with WordPress ... and since WP itself doesn't use sessions, it's relevant here.
- 5
- 2011-11-01
- EAMann
-
3 个回答
- 投票数
-
- 2012-11-15
编辑:"以下任何插件均不可用,因此请使用插件代替: WordPress会话插件"
有一个很好的WordPress插件,改编自CodeIgniter会话类: WP会话插件.
激活插件时,您可以从主题中的任何位置开始使用
$session
对象($session
对象只要是全局对象).例如,要将$ session对象用于header.php
文件,只需添加以下代码:global $session;
如果您是插件开发人员,并且希望将其与您的插件配合使用,则还可以在软件包中找到独立版本.插件文档为插件开发人员提供了有关如何适应项目的更多信息.
这对于主题和插件开发人员都是有用的功能.
您可以添加会话数据,如下所示:
// One value $session->set_userdata( 'username', 'john' ); // Passing array $array = array( 'username' => 'john', 'email' => '[email protected]' ); $session->set_userdata( $array );
要检索会话数据:
$session->userdata( 'username' );
要获取所有会话数据:
$session->all_userdata(); // returns array
要从会话中删除一项:
$session->unset_userdata( 'username' );
要
删除会话中的更多项目: $array = array( 'username' => '', 'email' => '' ); $session->unset_userdata( $array );
您还可以使用 Flashdata ,这是仅可用于下一个服务器请求的会话数据,然后会自动清除.当您将它们用于信息或状态消息(例如"产品已删除")时,这些功能非常有用.
// Add Flashdata $session->set_flashdata( 'item', 'value' ); // Retrieve Flashdata $session->flashdata( 'item' ); // Preserving flashdata // (if you need to preserve flashdata through an additional request, // you can use this function): $session->keep_flashdata( 'item' );
要销毁会话:
$session->sess_destroy();
该插件还支持简码.您可以在帖子或页面上打印任何会话数据:
[session key="username"]
达到第二个键:
[session key="user_data" sec_key="display_name"]
我希望这对某人有帮助.
EDIT: "THE PLUGIN BELOW ISN'T AVAILABLE ANYMORE, SO PLEASE USE THAT PLUGIN INSTEAD: WordPress Session Plugin"
There is a good WordPress Plugin adapted from CodeIgniter Session class: WP Sessions Plugin.
When you activate the plugin, you can start to use
$session
object from anywhere in your theme ($session
object as long as global). For instance, to use $session object intoheader.php
file, simply add this code:global $session;
If you are a plugin developer and you want to adapt this plugin with yours, you can find standalone version in the package as well. Documentation of the plugin gives more information for plugin developers about how to adapt to your project.
Here is some useful functions for both theme and plugin developers.
You can add session data like this:
// One value $session->set_userdata( 'username', 'john' ); // Passing array $array = array( 'username' => 'john', 'email' => '[email protected]' ); $session->set_userdata( $array );
To retrieve session data:
$session->userdata( 'username' );
To get all session data:
$session->all_userdata(); // returns array
To remove one item from session:
$session->unset_userdata( 'username' );
To remove more items from session:
$array = array( 'username' => '', 'email' => '' ); $session->unset_userdata( $array );
You can also use Flashdata which is session data that will only be available for the next server request, are then automatically cleared. These can be very useful when you use them for informational or status messages (e.g. “Product has been deleted”).
// Add Flashdata $session->set_flashdata( 'item', 'value' ); // Retrieve Flashdata $session->flashdata( 'item' ); // Preserving flashdata // (if you need to preserve flashdata through an additional request, // you can use this function): $session->keep_flashdata( 'item' );
To destroy session:
$session->sess_destroy();
The plugin also supports shortcodes. You can print any session data on your posts or pages:
[session key="username"]
To reach second key:
[session key="user_data" sec_key="display_name"]
I hope this helps for someone.
-
WP Sessions插件不存在!WP Sessions Plugin is not there!??
- 1
- 2013-12-26
- Kiren Siva
-
是的,您将要使用其中的一个:http://wordpress.org/plugins/wp-session-manager/(这更好且更稳定).Yes, you'll want to use that one: http://wordpress.org/plugins/wp-session-manager/ (This is much better and stabile).
- 1
- 2013-12-28
- beytarovski
-
另一个插件https://wordpress.org/plugins/wp-native-php-sessions/Another plugin https://wordpress.org/plugins/wp-native-php-sessions/
- 0
- 2016-12-05
- nu everest
-
为什么我们不能在WordPress中使用PHP默认会话功能?该解决方案创建了对插件的依赖.Why can't we use PHP default session functionality in WordPress? This solution creates dependency on a plugin.
- 0
- 2017-10-16
- Amrit
-
@Amritpal,因为并非所有PHP/Apache服务器都支持会话.如果要构建像WP这样的公共软件/插件,则必须考虑一下.如果您的个人项目可以在其中编辑服务器,那不是问题.@Amritpal because not all PHP/Apache servers support sessions. If you want to build a public software/plugin like WP, you have to think about it. If its personal project where you are able to edit server, that's not a problem.
- 0
- 2017-10-16
- beytarovski
-
- 2011-11-01
WordPress不使用会话,这就是您的会话变量不起作用的原因.
事实上,如果定义了某些变量, WordPress实际上会破坏
$_SESSION
使其保持无状态.但是,如果您真的想使用会话,请尝试在
session_start()
文件的开头添加wp-config.php
.每当WP启动时,这将(有希望地)启动会话,因此您将能够在系统中的其他位置设置和读取您的$_SESSION
变量.WordPress doesn't use sessions, that's why your session variables aren't working.
As a matter of fact, if certain variables are defined, WordPress will actually destroy
$_SESSION
to keep itself stateless.But if you really want to use sessions, try adding
session_start()
at the beginning of yourwp-config.php
file. This will (hopefully) start sessions whenever WP starts up, so you'll then be able to set and read your$_SESSION
variables elsewhere in the system.-
我看到Wordpress使用Cookies来存储一些登录数据.当我打印$ _COOKIE数组时,我可以看到一些数据.我想手动设置该数据.此处提供更多信息:http://stackoverflow.com/questions/21595900/how-to-bypass-wordpress-loginI saw that Wordpress uses Cookies to store some login data. When i printed $_COOKIE array, i could see some data. I would like to set that data manually. More info here: http://stackoverflow.com/questions/21595900/how-to-bypass-wordpress-login
- 0
- 2014-02-06
- shasi kanth
-
另外,是否建议修改wp-config.php文件以启动会话?如果我们稍后更新Wordpress,wp-config.php文件也会更新吗?Also, is it recommended to modify the wp-config.php file, to start session ? If we update Wordpress later, does the wp-config.php file get updated too?
- 1
- 2014-05-29
- shasi kanth
-
讨论此内容的教程更多http://silvermapleweb.com/using-the-php-session-in-wordpress/Tutorial that discusses this more http://silvermapleweb.com/using-the-php-session-in-wordpress/
- 0
- 2016-12-05
- nu everest
-
@shasikanth不,`wp-config.php`在更新中没有涉及.@shasikanth no, `wp-cofnig.php` is not touched on updates.
- 1
- 2018-03-08
- T.Todua
-
@shasikanth`wp-config.php`将不会更新,否则您可能会失去数据库连接和您手动设置的其他参数.@shasikanth `wp-config.php` will not get updated, otherwise you could lose you DB connection and other parameters you have manually set.
- 1
- 2019-08-14
- Erenor Paz
-
-
您为什么要两次启动会话?Why would you start the session twice?
- 11
- 2012-11-16
- kaiser
-
如何在Wordpress中使用自己的(自定义)会话值?
例如:
$_SESSION['myname']="4lvin"
我已经在我需要的所有页面上插入了
session_start()
,如下所示.但无法在全球范围内运作.
只是在自我页面上工作.
不能从其他页面全局调用(使用相同的逻辑).