Question

I'm working on an authentication form using google authenticator. When a user logs in for the first time, the QR code is generated. The QR code should be visible on my page. The problem is QR codes can only be generated when I browse the link from the url bar, redirecting from a url is not generating the code. Is there any workaround for this problem?

Here is the code that can be found in the google authenticator to generate the QR code:

public function getUrl($user, $hostname, $secret) {
    $url =  sprintf("otpauth://totp/%s@%s?secret=%s", $user, $hostname, $secret);
    $encoder = "https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=";
    $encoderURL = sprintf( "%sotpauth://totp/%s@%s%%3Fsecret=%s",$encoder, $user, $hostname, $secret);
    return $encoderURL;     
    }

And this is where i request the url:

<?php
    $linkQR = $g->getUrl($user,$location,$secret);
?>
<a  href="<?php echo $linkQR;?>"><img style="border: 0; padding:10px" src="<?php echo $linkQR;?>"/></a>
Was it helpful?

Solution

I found a solution for my problem. using https://chart.googleapis.com/ instead of https://www.google.com/ in the URL to generate solved it. Now my codes are generated on the first page load.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top