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.

5
  • codexworld.com/… Sep 23, 2020 at 14:05
  • please explain the complete scenario ? Is 10 min considered from first hit ? Sep 23, 2020 at 14:05
  • Hello @AbdulPathan i have update my question. Please check Sep 23, 2020 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 ? Sep 23, 2020 at 17:05
  • i have email send using controller.so not use database @AbdulPathan Sep 23, 2020 at 17:07

5 Answers 5

2

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

2
  • my already URl look like email link :- xyz.com/router/controller/action/?id=QWEQ12321 @Abdul Pathan Sep 23, 2020 at 17:51
  • Could you explain what is the id attribute ? and how you have used it in your code? what exactly it does ? Sep 24, 2020 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

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.

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>
9
  • Hello @Roman my already URL this :- xyz.com/router/controller/action/?id=QWEQ12321 Sep 23, 2020 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') Sep 23, 2020 at 18:14
  • Hello @Roman explain more i don't understand .Is that possible you update answer More explanation? Sep 23, 2020 at 18:18
  • Hi @MohitPatel, I updated the answer to include controller example. random_bytes is available for PHP 7 Sep 23, 2020 at 18:26
  • Okay i will try and update you . Thanks you. Sep 23, 2020 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.

1
  • It allows customers to modify the exp date.
    – HoangHieu
    Sep 27, 2020 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.