setcookie由于标题而无法工作在wordpress上 [英] setcookie not working wordpress because of header

查看:81
本文介绍了setcookie由于标题而无法工作在wordpress上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个想要创建Cookie的WordPress插件,但是它不起作用.我真的很感谢您的帮助.

I have a WordPress plugin that wants to create cookies but it doesn't work. I would really appreciate a help.

add_shortcode( 'watchlist', 'cwatchlist_short' );
  
function cwatchlist_short() {
	echo '<div class="wrap"><center>';
echo "<form method='POST' action=''";
echo '<p>Please enter the initial of the cryptocurrency or the symbol you want to add to the watchlist!</p> <p>For example: BTC</p>';
	echo "<p><input type='text' name='symbol' placeholder='Initials or symbol'>";
	
	
			echo "<p><input type='submit' value='Submit'>";
			echo "</form></center>";
	echo '</div>';
	if (isset($_POST["symbol"])){
  $x = $_COOKIE['xvalue'] + 1;
setcookie( 'xvalue', $x, time() + 108000, COOKIEPATH, COOKIE_DOMAIN   );

调试显示:Cannot modify header information - headers already sent通过 我真的需要帮助 谢谢 最好的问候

debugging shows: Cannot modify header information - headers already sent by I really need help. Thank you best regards

推荐答案

setcookie函数无法在直接页面运行过程中使用,它的始终抛出标头已设置错误消息.
要解决此问题,必须对setcookie&使用新功能;在初始化操作中添加此功能.

setcookie function can not work on direct page run process, its always throw header already set error message.
To solve this issue must use new function for setcookie & add this function on init action.

例如.

add_action( 'init', 'my_setcookie_example' );
function my_setcookie_example() {
	setcookie( "name", "value", time(), "/", COOKIE_DOMAIN );
}

在您的情况下,需要删除"setcookie('xvalue',$ x,time()+ 108000,COOKIEPATH,COOKIE_DOMAIN);"&放到函数中,然后在它的位置上添加"add_action"钩子.

In Your case,need to remove "setcookie( 'xvalue', $x, time() + 108000, COOKIEPATH, COOKIE_DOMAIN );" & place into function, After that add "add_action" hook on place of it.

这篇关于setcookie由于标题而无法工作在wordpress上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆