1

I want to generate a unique hash/number that I can use for status tracking for an contest. I'm going to use this hash/number in a url created with add_query_args.

The url creation will look something like this:

$url = add_query_arg( array( 'id' => 'tracking_number_comes_here', 'email' => '$contest_object->email' ), $redirect_url );

That will output something like this:

www.example.dev/share/?id=tracking_number_comes_here&email=email@example.com

This is working fine, but I'm having a hard way to generate a unique hash/number to use as the tracking number.

A row is added to contest table when a participant submits his participation. I use wpdb to insert to my table:

    // Add user to db
    $addRow = $wpdb->insert(
        $wpdb->contest,
            array(
                'name' => $data['name'],    
                'email' => $data['email'],
                'shared_facebook' => 0,
                'shared_mail' => 0,
                'tracking' => $unique_hash_number
            )
    );

I think generating an 8 character hash from an integer with md5 were the integer is the participant ID will work.

$hash = substr(md5($id), 0, 8);

Is it possible to grab the participant id on insertion? or do I have to update the hash after the participant is added to the table?

3

The userid will not exist until the user is inserted, you would need to do a second update for that. You may be better off using PHP's uniqid(), perhaps with the user's email as a salt/prefix to avoid a race condition (however unlikely).

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.