0

I'm trying to customize a plugin to check the current user's IP against previous IP values collected and stored by the plugin.

The plugin collects user votes for a custom post type called "competitors". The plugin also collects the IP of the user for each vote to prevent multiple votes by the user on a given competitor.

This is the code the plugin uses to store:

  1. the most recent voter IP into a $key called "lastIP" in the wp_postmeta database table
  2. all IPs that voted on the competitor into the next $key called "history" in the wp_postmeta database table (so there could be dozens of IPs stored in the "history" key array, along with a timestamp for each, and one of two other values depending on the vote type)

Existing code to store the values - works great:

function wps_update_history ( $thiscompetitor, $field, $value, $lastIP ) {

  update_post_meta($thiscompetitor, 'lastIP', $lastIP);

  $current_history = get_post_meta($thiscompetitor, 'history', true);
  $current_history_array = unserialize($current_history); 

  //push new values on
  $current_history_array[] = array ( current_time( "timestamp" ), $lastIP, $field, $value           );

  update_post_meta($thiscompetitor, 'history', serialize( $current_history_array ));
}

I've tried this to get the IP history list (to check if current user is in the list) but it is not working (within the post loop, with wp_query as orderby=>random) - from this post here

$hasIP = false;
    $histMeta = get_post_meta($post->ID, 'history', false);
        foreach($histMeta as $array) {
            if(isset($array['lastIP'])) {
                $hasIP = $array['lastIP'];
                break;
            }
        }
    echo $hasIP;

I've even tried to just echo the entire history array (for debugging) but can't even get this working.

    echo $histMeta;

Ultimately what I'd like to do is this logic:

if (current_user has their IP in this $post=>ID wp_postmeta 'history' key array) {
       // don't show or let them vote on this $post=>ID
    } else {
       // try another $post=>ID
        }

Any suggestions how I can achieve this logic above?

  • FYI, this is a typical array within the 'history' key - you can see the first value in each row (4 rows/entries) is a timestamp, then the user IP, then a string (won, shown, points), and if there's points it adds an integer value: a:6:{i:0;a:4:{i:0;i:1424637516;i:1;s:13:"65.92.235.251";i:2;s:3:"won";i:3;s:0:"";}i:1;a:4: {i:0;i:1424637581;i:1;s:13:"24.114.65.160";i:2;s:6:"points";i:3;i:9;}i:4;a:4:{i:0;i:1424638574;i:1;s:13:"24.114.65.160";i:2;s:3:"won";i:3;s:0:"";}i:5;a:4:{i:0;i:1424640181;i:1;s:12:"24.138.123.7";i:2;s:3:"won";i:3;s:0:"";}} – themav Feb 23 '15 at 8:51
1

First, let met say that instead of echo, you should use var_dump or print_r for debugging. At first view, I see you are building a sequential array $current_history_array but you are trying to use it as associative array.

$current_history_array[] = array ( current_time( "timestamp" ), $lastIP, $field, $value           );

The above code generates a sequential array, for example:

array(
    '1424680884',
    '77.77.777.777',
    $field,
    $value
);

After, you are trying to access to lastIP key using $array['lastIP'] but such key doesn't exists. Instead, the last IP is stored in the index 2: $array[2]. If you want ot use associative arrays you have to build it as follow:

$current_history_array[] = array (
                               //The structure is key => value
                               'time'   => current_time( "timestamp" ),
                               'lastIP' => $lastIP,
                               'field'  => $field,
                               'value'  => $value          
);

The above code generates an array like this:

array(
    //The structure is key => value
    'timestamp' => '1424680884',
    'lastIP'    => '77.77.777.777',
    'field'     => $field,
    'value'     => $value
);

Now you can access to 'lastIP' key as $array['lastIP'].

Also, note that you store serialized data, so you have to unserialize it before use it as array:

$hasIP = false;
$histMeta = get_post_meta($post->ID, 'history', false);
    foreach($histMeta as $array) {

        $array = unserialize( $array );

        if(isset($array['lastIP'])) {
            $hasIP = $array['lastIP'];
            break;
        }

    }
echo $hasIP;

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.