WordPress的* get_current_user_id *远程服务器而不是本地服务器上返回0
问题描述:
请我与WordPress和使用Ajax的一些数据存储到数据库中,我已取回后登录的用户ID与WP功能get_current_user_id()。WordPress的* get_current_user_id *远程服务器而不是本地服务器上返回0
在我的本地服务器localhost上,但在我的远程服务器上,一切正常。get_current_user_id()总是返回0而不是登录的用户ID。
当我直接运行的submit.php文件get_current_user_id()返回用户ID精细
请注意submit.php和Ajax不是WordPress的的一部分,它是在一个单独的文件夹中WordPress的根文件夹
当运行seperately的submit.php文件并测试以查看所得到的执行查询get_current_user_id()输出在用户ID正确的记录,而不是0
submit.php
require_once("../../../wp-blog-header.php");
$connection = mysqli_connect("host", "user", "pass",
"db"); // Establishing Connection with Server..
$db = mysqli_select_db($connection, "db"); // Selecting Database
//Fetching Values from URL
@$score = $_POST['score'];
$user_id = get_current_user_id();
//Insert query
$query = "update gamestats set bestscore = {$score} WHERE game='2048' AND
playerid = {$user_id} and bestscore <= {$score}";
mysqli_query($connection, $query);
if (mysqli_affected_rows($connection) <= 0){
$ins_query = "INSERT INTO gamestats (bestscore, game, playerid)
VALUES({$score}, '2048', {$user_id})";
mysqli_query($connection, $ins_query);
}
mysqli_close($connection); // Connection Closed
Ajax调用文件是同一个文件夹内的submit.php
$.ajax({
type: "POST",
url: "submit.php",
data: dataString,
cache: false,
success: function(result){
//alert(score + " :Post Best Score");
}
});
WP模板中的启动代码
<iframe height="1057" scrolling="no" align="middle" frameborder="0" src="http://www.xxxx.com/wp-folder/custom-folder/index.html">
的iframe是什么启动我使用WP模板的整个过程。
这是Ajax的脚本被称为index.html的内
“自定义文件夹”是放在WordPress的文件夹中的我自己的自定义文件夹
注:get_current_user_id()返回在本地服务器上的正确的用户ID和一切工作完全正常,但在远程服务器上它返回0作为playerid存储0作为数据库的所有时间。可能有什么问题请帮忙?
答
通过清除Web浏览器和本地存储的缓存来修复。
为什么不使用wordpress ajax钩子? – madalinivascu
@madalin我不知道该怎么做。此外,ajax文件不是WordPress的一部分,它只是将一些数据保存到包含来自wp的用户标识的数据库中。但是,它在我的本地主机上正常工作。谢谢 –
确定你已登录? – madalinivascu