如何使用 MyCred 和 WordPress 读取二维码(URL)后为登录用户添加积分

问题描述 投票:0回答:1

我正在尝试为一家当地商店创建一个 WordPress 项目,该项目希望在商品到达商店并读取与商店相关的特定二维码时向客户提供积分。

我正在使用下面的项目来使用移动摄像头读取二维码。 https://github.com/cozmo/jsQR

我发现了类似的东西,但这个项目适用于分配给用户的任何二维码。 https://stackoverflow.com/questions/44809685/how-to-interact-with-a-wordpress-plugin-based-on-a-url-coded-into-a-qr-code

我需要的是给读取二维码的登录用户添加一定数量的积分。

另外,如果我可以设置二维码本身的使用时间限制,我将不胜感激。

例如,使用如下链接

http://www.anysite.com/shop-page?action=setpoints&points=10&shop=1&date=20231030

积分:要添加到登录用户的积分数

店铺:表示该二维码属于哪个店铺

日期:二维码的有效期。该代码的有效期至 2023 年 10 月 30 日。

我将非常感谢任何关于如何实现这一目标的指导。请注意,我并不是寻求替代品或产品的建议。只是,就编程而言,我将如何进行设置。

php wordpress qr-code
1个回答
0
投票

一定喜欢二维码集成!

到目前为止,您提到了

MyCred
cozmo/jsQR
,但也没有过多展示这些内容。

就二维码扫描而言,这与所涉及的二维码网址没有任何关系。

您已声明您的二维码网址是

http://www.anysite.com/shop-page?action=setpoints&points=10&shop=1&date=20231030
...

因此,我们需要做的就是在扫描并重定向到您的 WordPress 网站时处理此 QR 码的 URL 参数。

请注意,我的解决方案如下,这并不能解决通过作弊递归点更新而多次重新扫描二维码的问题。但这可能会让您走上正确的道路,使用额外的参数来处理单个扫描码或其他限制方法。

正如 @CBroe 指出的第一个问题,任何人都可以操纵通过 url 代码传递的 url 中的点...

超级安全的方法是使用 php openssl_encrypt() 函数来加密您的 url 参数,但加密字符串的最小长度为 128 个字符。对于 url 参数来说有点庞大,但并不非法。

另一种更轻、更小的编码 url 参数字符串的方法是使用 base64_encode()。通过一些额外的厚颜无耻的添加,使其成为半加密的,同时保持编码的 url 参数尽可能短。请参阅下面的工作和测试示例...

好的,这是您想要的 URL 参数...

http://www.anysite.com/shop-page?action=setpoints&points=10&shop=1&date=20231030

让我们将这些参数作为一个普通的 php 数组......

$params = [
    'action' => 'setpoints',
    'points' => 10,
    'shop'   => 1,
    'date'   => 20230130
];

现在让我们将此函数添加到您的

functions.php
,请参阅下面 php 中的注释,以便您知道发生了什么...

/**
 * @param $array
 * @return bool|string
 */
function make_url_code($array) {

    // if param is an array
    if(is_array($array)) {

        // json encode our php array
        $data = json_encode($array,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);

        // base64 encode json string
        $data = base64_encode($data);

        // reverse our base64 encoded string
        // a little hack to throw off any sneaky pete's trying to decode our string
        $data = strrev($data);

        // return url encoded string of our reversed base64 encoded string
        return urlencode($data);

    }

    // return false
    return false;

}

现在让我们通过

make_url_code()
函数传递 params 数组...

echo get_bloginfo('url') . '?code=' . make_url_code($params);

然后输出我们稍微加密的编码,但短网址

code
参数用于您的二维码...

http://www.anysite.com/?code=%3D0HMzATMzIDMyojIlRXYkJCLxojIw9GazJCLwEjOiMHdul2bwJCLiMHdul2bwRXZzJiOi42bpR3YhJye

现在,当您使用上面输出的网址生成二维码后,我们需要在扫描时处理该网址。为此,我们需要在您的

functions.php
中添加自定义操作函数。

查看 php 中的注释,这样你就知道发生了什么......

// add action to handle our url code param when used anywhere on our site
add_action('init', 'action_handle_url_code');

/**
 * @return void
 */
function action_handle_url_code() {

    // check if the 'code' parameter is set in the URL
    if(isset($_GET['code'])) {

        // url decode our url code param
        // reverse our url decoded string
        // base64 decode our reversed string
        // json decode our decoded base64 string into php array
        $params = json_decode(
            base64_decode(
                strrev(
                    urldecode($_GET['code'])
                )
            ),true);

        // if params code decoded output is php array
        if(is_array($params)) {

            // if action key exists in code params
            if(array_key_exists('action', $params)) {

                // if params array action key is 'setpoints' 
                if($params['action'] === 'setpoints') {

                    // convert the 'date' key value to a date object
                    $date = DateTime::createFromFormat('Ymd', $params['date']);

                    // if data is false because object could not be created
                    if($date === false) {

                        // handle invalid date format here
                        var_dump('Invalid QR code date');

                    } else {

                        // get the current date
                        $now = new DateTime();

                        // compare the 'date' value with the current date
                        if($date < $now) {

                            // invalid code because date has elapsed current date
                            var_dump('QR code has expired!');

                        } else {

                            // valid code because date has not expired yet
                            var_dump($params['points'], $params['shop']);

                            // do your set points stuff here...

                        }
                    }

                }

            }

        } else {

            // do or don't do invalid code stuff here
            var_dump('Invalid QR code');

        }

    }

}

正如您将在上面的代码中看到的...

  1. 首先我们检查 url code 参数解码后是否是一个有效的数组。
  2. 然后我们检查
    action
    键是否已在数组中设置。
  3. 然后我们检查
    action
    键值是否为
    setpoints
  4. 如果
    setpoints
    为 true,我们将检查日期是否有效或日期是否已过期。

您会看到我使用

var_dump()
来指示您在哪里实施自己的操作来处理点添加等。

就像我之前在这个答案中提到的,这并不能解决问题 任何人重新扫描二维码以递归地获得积分。你会 需要向您的

$params
数组添加一些内容以供单次扫描使用或 处理使用后停用二维码的问题。

无论如何,希望这能让您走上正轨!

© www.soinside.com 2019 - 2024. All rights reserved.