0

I'm using jQuery and AJAX to call a PHP function when a user enters certain pages. The PHP function receives a parameter that needs to be decrypted, the thing is, for the decryption I use the following function:

function casos_crypt( $string, $action ) {

        // you may change these values to your own
        $secret_key = 'my-secret-key';
        $secret_iv = 'my-secret-iv';

        $output = false;
        $encrypt_method = "AES-256-CBC";
        $key = hash( 'sha256', $secret_key );
        $iv = substr( hash( 'sha256', $secret_iv ), 0, 16 );

        if( $action == 'e' ) {
            $output = base64_encode( openssl_encrypt( $string, $encrypt_method, $key, 0, $iv ) );
        }
        else if( $action == 'd' ){
            $output = openssl_decrypt( base64_decode( $string ), $encrypt_method, $key, 0, $iv );
        }

        return $output;
    }

Which works okay unless I call it throw AJAX which returns an empty string.


In case it's needed as further info: Jquery code to execute AJAX:

jQuery(document).ready(function(){

    if (!jQuery('body').hasClass("admin-bar")) {

        var ue = jQuery.urlParam('ue');

        jQuery.ajax({
            type: 'POST',
            url: ClickAjax.ajaxurl,
            data: {"action": "nlmd_ustrack_frontend_only_function_ajax", "ue": ue}
        });

    };

});

jQuery.urlParam = function(name){
    var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
    return results[1] || 0;
}

PHP Function called by AJAX (inside the decryption function is called):

function nlmd_ustrack_frontend_only_function_ajax() {

        $user_email = sanitize_text_field($_POST['ue']);

        if ( !empty($user_email) ) {

                global $wpdb;
                $table_name = $wpdb->prefix . 'hits';

                $decrypted_email = casos_crypt( $user_email, 'd' );

                $blogusers = get_users( [ 'role__in' => [ 'subscriber' ] ] );

                foreach ( $blogusers as $user ) {
                    if ($user->user_email === $decrypted_email) {

                            $wpdb->insert(
                                $table_name,
                                array(
                                    'time' => current_time( 'mysql', false ),
                                    'tipo' => 'Access',
                                    'name' => 'Test name',
                                    'text' => 'Text test',
                                    'titulo' => get_the_title(),
                                )
                            );
                    }

                }
        }
        wp_die();

}
add_action('wp_ajax_nlmd_ustrack_frontend_only_function_ajax', 'nlmd_ustrack_frontend_only_function_ajax');
add_action('wp_ajax_nopriv_nlmd_ustrack_frontend_only_function_ajax', 'nlmd_ustrack_frontend_only_function_ajax');

(To check the returning values I use write_log, I deleted those lines to make the code shorter)

Thanks in advance!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.