最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How to check if a meta value has already been assigned to any user?

programmeradmin0浏览0评论

On my website I automatically generate an 8-digits random number $rndnr via wp_rand() during the registration process and save it as user meta.

How can I check if this number has already been assigned to any user?

I actually handle it with this, while $result reads the value from the database:

$result = $wpdb->get_var("SELECT meta_value from $wpdb->usermeta where meta_key='user_reference_id' AND meta_value = '$rndnr'");
if(empty($result))
 return true;

But I don't know for sure if this code actually would recognize a duplicate?

How would you go about it?

Thank you.

On my website I automatically generate an 8-digits random number $rndnr via wp_rand() during the registration process and save it as user meta.

How can I check if this number has already been assigned to any user?

I actually handle it with this, while $result reads the value from the database:

$result = $wpdb->get_var("SELECT meta_value from $wpdb->usermeta where meta_key='user_reference_id' AND meta_value = '$rndnr'");
if(empty($result))
 return true;

But I don't know for sure if this code actually would recognize a duplicate?

How would you go about it?

Thank you.

Share Improve this question edited Jan 22, 2021 at 16:11 RealJohnDoe asked Jan 22, 2021 at 2:36 RealJohnDoeRealJohnDoe 135 bronze badges 6
  • 1 What is $result? Please edit your question to show us where it's being set. – Pat J Commented Jan 22, 2021 at 4:26
  • Hi Pat. I use an 8 digit number for this via wp_rand(). After that I retrieved the meta_key and the meta_value using $wpdb->get_var(). The result as $result then goes into the if(empty($result) statement. – RealJohnDoe Commented Jan 22, 2021 at 12:02
  • Note that your code can be used for SQL injection attacks, you need to use $wpdb->prepare, don't just put variables straight into SQL queries, it's dangerous – Tom J Nowell Commented Jan 22, 2021 at 17:20
  • Also you shouldn't be using wp_rand to generate unique IDs, PHP already has functions and libraries for this that are more secure – Tom J Nowell Commented Jan 22, 2021 at 17:23
  • @TomJNowell Thank you so much for your advice. I am still a rookie when it comes to WordPress and programming but I am improving. Would you kindly show me a working alternative? – RealJohnDoe Commented Jan 22, 2021 at 17:32
 |  Show 1 more comment

1 Answer 1

Reset to default 0

Here is an approach you could take to do this. It's problematic if you have a large number of users, but it could be adapted in-time.


do {
    // 1. generate your unique random number
    $result = rand();

    // 2. check it is really unique
    $is_unique = count( get_users( array(
            'meta_key'   => 'user_reference_id',
            'meta_value' => $result,
        ) ) ) === 0;

} while ( !$is_unique );

// 3. Store user meta $result
update_user_meta( $user_id, 'user_reference_id', $result );

Assumptions:

  • I've used rand() but frankly you should be using something like UUIDs and this way you wouldn't need a loop like this to check for whether your number is unique. But I've provided this approach as it's likely going to solve for your problem in the way you've designed it currently.
  • there is no handling here for WPMS or other such mods.

Edit:

Your original question keeps changing, so I'll leave this edit as the last change.

Following your comments, I'm providing code so that you can create a Unique ID every time using UUIDs. Instead of the loop to ensure your custom random codes are unique, UUIDs ensure codes are unique by design.

So simply:

// 1. Generate the random ID
$rndnr = wp_generate_uuid4();

// 2. Store the ID
update_user_meta( $user_id, 'user_reference_id', $rndnr );

// 3. Get the user meta key in the future, simply:
get_user_meta( $user_id, 'user_reference_id' );

Assumptions:

  • WordPress is at least version 4.7

In your original question you didn't state that you were using 8-digit codes. Not sure why you picked 8, but if you update your system to use UUIDs you solve your issues of uniqueness.

I'm also using update_user_meta and get_user_meta - it's unclear why you're using MySQL select statements when these helper functions are available.

发布评论

评论列表(0)

  1. 暂无评论