在WordPress环境之外的开发环境中,我的代码在本地主机上运行良好。我知道PHP函数正在工作。我可以从PC上的本地主机向服务器发送测试投票。
Problem: 我无法在WordPress中使用此功能。
My Thoughts我认为这是一个路径问题,但我已经尝试将PHP脚本放在根目录中并使用完整路径。我在web浏览器控制台(f12)中没有收到任何错误。
WordPress Version: 5.4.1我将我的自定义php代码放入“/wp-contents/custom-php/votifier.php”我的JQuery脚本在标题中。(是的,我知道我应该把它放在页脚。)
The Button
<div id="voteButton">
<button type="button">Try it</button>
</div>
Localhost Version
<script>
$(document).ready(function(){
$("#voteButton").click(function(){
$.post("votifier/votifier.php",
{
key: $.trim($("#field_yjr62").val()),
ip: $(\'input[name="item_meta[40]"]\').val(),
port: $(\'input[name="item_meta[42]"]\').val(),
service: "Votifier",
username: $(\'input[name="item_meta[59]"]\').val()
},
function(data,status){
alert("Data: " + data + "\\nStatus: " + status);
});
});
});
</script>
一
<script>
jQuery(document).ready(function( $ ) {
jQuery("#voteButton").click(function(){
$.post("/home/xxxxxxxxxxxx/public_html/wp-content/custom-php/votifier.php",
{
key: $.trim($("#field_yjr62").val()),
ip: $(\'input[name="item_meta[40]"]\').val(),
port: $(\'input[name="item_meta[42]"]\').val(),
service: "Votifier",
username: $(\'input[name="item_meta[59]"]\').val()
},
function(data,status){
alert("Data: " + data + "\\nStatus: " + status);
});
});
});
</script>
My Custom PHP Script
<?php
const VOTE_FORMAT = "VOTE\\n%s\\n%s\\n%s\\n%d\\n";
const PUBLIC_KEY_FORMAT = "-----BEGIN PUBLIC KEY-----\\n%s\\n-----END PUBLIC KEY-----";
$public_key = formatPublicKey($_POST[\'key\']);
$server_ip = $_POST["ip"];
$port = $_POST["port"];
$service_name = $_POST["service"];
$username = $_POST["username"];
sendVote($username, $public_key, $server_ip, $port, $service_name);
function formatPublicKey($public_key) {
$public_key = wordwrap($public_key, 65, "\\n", true);
$public_key = sprintf(PUBLIC_KEY_FORMAT, $public_key);
return $public_key;
}
function sendVote($username, $public_key, $server_ip, $port, $service_name) {
if (php_sapi_name() !== \'cli\') {
//Detect proxy and use correct IP.
$address = isset($_SERVER[\'HTTP_X_FORWARDED_FOR\']) ? $_SERVER[\'HTTP_X_FORWARDED_FOR\'] : $_SERVER[\'REMOTE_ADDR\'];
} else {
//Script is run via CLI, use server name.
$address = $_SERVER[\'SERVER_NAME\'];
}
$data = sprintf(VOTE_FORMAT, $service_name, $username, $address, time());
openssl_public_encrypt($data, $crypted, $public_key);
$socket = @fsockopen($server_ip, $port);
if ($socket) {
if (fwrite($socket, $crypted)) {
fclose($socket);
return true;
}
}
return false;
}
?>
Network Info
Request URL:https://bestlist.com/home/xxxxxxxxxxxx/public_html/wp-content/custom-php/votifier.php
Request Method:POST
Remote Address:999.999.999.99:443
Status Code:
404
Version:HTTP/2
Referrer Policy:strict-origin-when-cross-origin
SO网友:ScottUSA
Function: 用户单击网页上的按钮
Action: 单击应使用以下参数连接到外部服务器:
1. 用户名2. IP地址3. 港口城市4. 公钥5. 服务
PHP代码仍然没有将投票发送到我的外部服务器。所以,我尝试了一种稍微不同的方法。也就是说,我正在尝试使用wp-ajax操作。
服务器端PHP是相同的(见上文)。
Wordpress处于调试模式,但我没有收到任何错误。
我使用的是Firefox,表单数据看起来不错:
HTML CODE
/* XXXX HTML BUTTON XXXX */
<div id="frm_field_61_container">
<button type="button">Test Vote</button>
</div>
JQuery AJAX
/* XXXXX JQuery / AJAX Call XXXX */
jQuery(document).ready( function($) {
$("#frm_field_61_container").click(function(){
nonce = \'votifier_response_key\';
jQuery.ajax({
type : "post",
dataType : "json",
url : myAjax.ajaxurl,
data : { action: "my_vote_count"
,key: $.trim($("#field_yjr62").val())
,ip: $(\'input[name="item_meta[40]"]\').val()
,port: $(\'input[name="item_meta[42]"]\').val()
,service: "Votifier"
,username: $(\'input[name="item_meta[59]"]\').val()
,nonce: nonce},
success: function(response) {
if(response.type == "success") {
alert("Your vote was counted!")
}
else {
alert("Your vote could not be added")
}
}
});
});
});
WordPress - functions dot php file
/* ===== WordPress Java Script Registration ==== */
add_action( \'wp_enqueue_scripts\', \'my_script_enqueuer\' );
function my_script_enqueuer() {
wp_register_script( "my_voter_script", get_stylesheet_directory_uri() . \'/js/sendvote.js\', array(\'jquery\') );
wp_localize_script( \'my_voter_script\', \'myAjax\', array( \'ajaxurl\' => admin_url( \'admin-ajax.php\' )));
wp_enqueue_script( \'jquery\' );
wp_enqueue_script( \'my_voter_script\' );
}
/* ===== My Votifier Code ==== */
add_action( \'wp_ajax_my_vote_count\', \'my_ajax_handler\');
add_action( \'wp_ajax_nopriv_my_vote_count\', \'my_ajax_handler\' );
function my_ajax_handler() {
check_ajax_referer( \'votifier_response_key\' );
echo "Thank You for your Vote!";
wp_die(); // All ajax handlers die when finished
}
JQuery is Registered and showing up in the footerWhen I click on the link in the footer, I see the JQuery: