How to implement Google reCaptcha V3 in PHP

Google recently introduced reCAPTCHA V3, which is a pure Javascript API that requires no user interaction at all. Official documentation can be found here.

One major difference is that reCAPTCHA’s Site Verify Response now returns more information:

{
"success": true|false,      // whether this request was a valid reCAPTCHA token for your site
"score": number             // the score for this request (0.0 - 1.0)
"action": string            // the action name for this request (important to verify)
"challenge_ts": timestamp,  // timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
"hostname": string,         // the hostname of the site where the reCAPTCHA was solved
"error-codes": [...]        // optional
}

So in your verification code you can check for parameters like “success”, “score”, “action”, etc. The below is a very basic one-page demo on how to use reCaptcha V3 in a form.
Continue reading “How to implement Google reCaptcha V3 in PHP”

How to make custom admin “Save and Continue Edit” button work in Magento 2

I recently created a custom module that has a “Save and Continue Edit” button on the admin edit form. The tricky part is to make the page stay on the edit screen after the button is pressed and the form is saved.

When Save and Continue button is pressed, a back/edit string is appended to the submit url by default, so in the save controller we can check for it and set redirect url accordingly.

The return statement of the save controller should look something like this:

    ...
    //check for 'back' parameter
    if ($this->getRequest()->getParam('back')) {
        //redirect to edit page
        return $resultRedirect->setPath('*/*/edit', ['id' => $model->getId(), '_current' => true]);
    }
 
    //redirect to index page
    return $resultRedirect->setPath('*/*/');
    ...

Use AWS CloudFront with EC2 instance

Hi guys, I haven’t written anything for quite some time due to lack of time (and laziness). In this article I’m going to share my experience on how to put a whole website, hosted on a EC2 instance, behind CloudFront.

If you want your website to have a global presence and to be accessed quickly anywhere around the world, even in China probably, AWS CloudFront is a great tool to do just that. CloudFront supports the distribution of both static and dynamic content, so you can pretty much set it up with any type of website.
Continue reading “Use AWS CloudFront with EC2 instance”

Show address fields on Magento customer registration page

There’s no setting in Magento’s backend to switch on/off address fields display on customer registration page. The easiest way to show address fields is to add a ‘show_address_fields’ argument to the layout file customer_account_create.xml

File: app/design/frontend//Magento_Customer/layout/customer_account_create.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="customer_form_register">
            <arguments>
                <argument name="show_address_fields" xsi:type="number">1</argument>
            </arguments>
        </referenceBlock>
    </body>
</page>

Clear cache afterwards and the address fields should appear on the customer account creation page.

How to implement Google Invisible reCaptcha in PHP

Google recently introduced the Invisible reCAPTCHA, which hides Captcha challenge from view and makes it even simpler for website’s users to fill in a form.

Official documentation can be found here. The below is an example of how to implement it on a PHP website.
Continue reading “How to implement Google Invisible reCaptcha in PHP”