JQuery calling a Custom PHP function (Works in Dev but not in WordPress)

Question

My code is working fine on my localhost in my development environment which is outside of the WordPress press environment. I know the PHP function is working. I am able to send test votes to my server from my localhost on my PC.

Problem: I cannot get this to work in WordPress.

My Thoughts
I think it’s a path issue, but I’ve tried putting the PHP script in the root and using a full path.
I am not getting any errors in the web browser console (f12).

WordPress Version: 5.4.1
I put my custom php code into “/wp-contents/custom-php/votifier.php”
My JQuery script is in the header. (yes, I know I should put it in the footer.)

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>

WordPress Version

<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 = "VOTEn%sn%sn%sn%dn";
const PUBLIC_KEY_FORMAT = "-----BEGIN PUBLIC KEY-----n%sn-----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;
}
?>
0
, ScottUSA 4 years 2020-05-22T20:10:35-05:00 0 Answers 94 views 0

Leave an answer

Browse
Browse