0

I want to Simply expire link after 10 min in PHP.Assuming I have create custom email send template.So in email i have added one link.So how to expire email link after 10 min.

THANKS.

  • codexworld.com/… – Mayur Sisodiya Sep 23 at 14:05
  • please explain the complete scenario ? Is 10 min considered from first hit ? – Abdul Pathan Sep 23 at 14:05
  • Hello @AbdulPathan i have update my question. Please check – Mohit Patel Sep 23 at 16:47
  • Assuming you are creating a link dynamically, you can store the link & its expiry time in database, on the page you can validate if time has expired or not to show the page. Does this works for you ? – Abdul Pathan Sep 23 at 17:05
  • i have email send using controller.so not use database @AbdulPathan – Mohit Patel Sep 23 at 17:07
1

You can generate a link with param attached to it. This param can be a timestamp or token.

1. Approach with timestamp

Your page link in email would be something like

<a href="https://your-site/your-page?expires=1600881883">Click here</a>

On your page, you can get the expire timestamp from URL, generate timestamp of current time & compare if 10 min has passed. Show the page if 10 min hasn't passed else do not show it.

Drawback : geek could manipulate the timestamp to have access of expired page as well.

2. Approach with token

  • Create a token and maintain it in a separate database table.(eg: Token, page_link, expiry_timestamp).
  • Pass this token with the email like
<a href="https://your-site/your-page?token={token_string}">Click here</a>
  • On your page, you can get the token from URL, search this token in your table, compare if 10 min has passed. Show the page if 10 min hasn't passed else do not show it.
  • This option would be more secure than first approach.

Hope it was helpful.

Thanks

| improve this answer | |
  • my already URl look like email link :- xyz.com/router/controller/action/?id=QWEQ12321 @Abdul Pathan – Mohit Patel Sep 23 at 17:51
  • Could you explain what is the id attribute ? and how you have used it in your code? what exactly it does ? – Abdul Pathan Sep 24 at 5:45
1

If you are saving data in the database than save current time and then compare that time and date.

if you are not saving it then pass date and time in parameters and compare it with current time date.

Happy coding

| improve this answer | |
1

I can suggest a solution using cache lifetime (10 minutes), save your token_key into cache service with 10 minutes is cache lifetime, if you can't get your token_key on the cache system, that means the token_key is expired.

| improve this answer | |
1

Unorthodox solution just for fun :) You mentioned that you're sending inside controller:

namespace MyCompany\MyModule\Controller\Token;

use Magento\Framework\App\Action\Context;
use Magento\Framework\Cache\FrontendInterface;
use Magento\Framework\Controller\Result\Raw;
use Magento\Framework\Controller\ResultFactory;

class Send extends \Magento\Framework\App\Action\Action
{
    /**
     * @var FrontendInterface
     */
    private $cache;

    public function __construct(
        FrontendInterface $cache,
        Context $context
    ) {
        $this->cache = $cache;
        parent::__construct($context);
    }

    public function execute()
    {
        // random_bytes is available at PHP 7
        $token = base64_encode(random_bytes(32));
        $this->cache->save(
            "this is a valid link :)", // value doesn't matter
            "my-token-$token", // key
            ['expiration-token'], // tags
            10*60 // expiration in seconds
        );

        $link = $this->_url->getUrl('myroute/token/verify', ['_query' => ['id' => $token]]);
        // send email with this link to xyz.com/router/controller/action/?id=QWEQ12321

        /** @var Raw $result */
        $result = $this->resultFactory->create(ResultFactory::TYPE_RAW);
        $result->setContents("<a href='$link' target='_blank'>$token</a>");
        return $result;
    }
}

Verify controller:

namespace MyCompany\MyModule\Controller\Token;

use Magento\Framework\App\Action\Context;
use Magento\Framework\Cache\FrontendInterface;
use Magento\Framework\Controller\Result\Raw;
use Magento\Framework\Controller\ResultFactory;

class Verify extends \Magento\Framework\App\Action\Action
{
    /**
     * @var FrontendInterface
     */
    private $cache;

    public function __construct(
        FrontendInterface $cache,
        Context $context
    ) {
        $this->cache = $cache;
        parent::__construct($context);
    }

    public function execute()
    {
        /** @var Raw $result */
        $result = $this->resultFactory->create(ResultFactory::TYPE_RAW);

        $token = $this->_request->getParam('id');
        $isValid = $this->cache->load("my-token-$token");
        if (false === $isValid) {
            $result->setContents('This link does not have power here 😕.');
            return $result;
        }

        // verified, put your code here
        $result->setContents('Valid 👍!');
        return $result;
    }
}

app/etc/frontend/di.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="MyCompany\MyModule\Controller\Token\Send">
        <arguments>
            <argument name="cache" xsi:type="object">Magento\Framework\App\Cache\Type\Config</argument>
        </arguments>
    </type>
    <type name="MyCompany\MyModule\Controller\Token\Verify">
        <arguments>
            <argument name="cache" xsi:type="object">Magento\Framework\App\Cache\Type\Config</argument>
        </arguments>
    </type>
</config>
| improve this answer | |
  • Hello @Roman my already URL this :- xyz.com/router/controller/action/?id=QWEQ12321 – Mohit Patel Sep 23 at 17:58
  • @MohitPatel, in your send controller import cache interface and use my first snippet to generate id parameter. In your verify controller, import cache again, and you already have $this->request, use my second snippet to verify $this->request->getParam('id') – Roman Kabanov Sep 23 at 18:14
  • Hello @Roman explain more i don't understand .Is that possible you update answer More explanation? – Mohit Patel Sep 23 at 18:18
  • Hi @MohitPatel, I updated the answer to include controller example. random_bytes is available for PHP 7 – Roman Kabanov Sep 23 at 18:26
  • Okay i will try and update you . Thanks you. – Mohit Patel Sep 23 at 18:29
0

I have got answer.All Suggestions.

My Controller File CustomerDelete.php. And

  1. I have get current time $current_time = time('h:i:s');

  2. In Email Template set time like :-

    ->setTemplateVars([
       "name" =>$customerName,
       "id" => $encodeId,
       "time" => $current_time
    ])
    
  3. In Email Template my account_expire.html file set time with URL :- [_query:['id':$id,'exp':$time]]

  4. Final Work expire link operation in my controller CustomerSuccess.php:-

    <?php
        $exp = $this->getRequest()->getParam('exp');
        $currenttime = time('h:i:s');
        $expiretime = $exp;
     if ($currenttime - $expiretime < 10*60) 
     {
       echo "Link Is Not Expire";
     }else{
       echo "Link is Expire";
     }
    

THANKS.

| improve this answer | |
  • It allows customers to modify the exp date. – HoangHieu Sep 27 at 10:10

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.