5

I want any other server request goes through my server just like link in Gmail and i checked it there data-saferedirecturl="".

So how to use data-saferedirecturl in PHP automatcally in our website?

Exapmple:

<a href="xxxxx" target="_blank" data-saferedirecturl="https://www.mywebsite.com/url?hl=en&amp;q=xxxx">Link</a>

2 Answers 2

10

All links in Gmail are interpreted in the browser.

The data-saferedirecturl tag is added automatically.

So the href shows the link that you will be clicking in the bottom of your browser but sends you to a google-originated URL like https://www.google.com/url?hl=en-GB&site.com/324dd3.

This way the third party don't have access to sensitive data.

3

There are different ways to achieve this.

You can point all the redirects to the same page with the safe URL as a GET parameter (remember to use urlencode):

<a href="/handler.php?safeurl=www.google.it%2Ftest"  target="_blank">Link</a>

Then in handler.php something like:

$desturl = isset($_GET["safeurl"]) ? $_GET["safeurl"] : false;
if($desturl != false){
    //do something
    header("location: ".$desturl);
}

If you want to use data-saferedirecturl you have to use some JavaScript, and intercat somehow with your server (ex. ajax calls).

Example (using jQuery):

HTML:

<a href="xxxxx" class="test" target="_blank" data-saferedirecturl="https://www.mywebsite.com/url?hl=en&amp;q=xxxx">Link</a>

JavaScript:

$(".test").click(function(e){
  e.preventDefault();
  let url = $(this).data("saferedirecturl");
  // do ajax or check somehow the URL
  location.href = url;
})

Your Answer

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

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