skip to Main Content
<?php
echo "<a href='".$client->createAuthUrl()."'><button>Login with Google</button></a>";
?>

Im trying to add class for <a href> inside php echo , but i cant do any style what i want to do

<?php
echo "<a href='".$client->createAuthUrl()."'><button>Login with Google</button></a>";
?>

3

Answers


  1. You can try something like this, I hope it will help you.

    <a href="<?=createAuthUrl()?>" class="your-class-name">Login with Google"</a>
    
    Login or Signup to reply.
  2. Your use of single quotes and double quotes is a little mixed up. I should also add that buttons inside a tags aren’t really correct. I’d suggest removing the button and styling the a tag instead. With that in mind, here are the options:

    Regular CSS method

    echo '<a class="mylink" href="'.$client->createAuthUrl().'">Login with Google</a>';
    

    then in the stylesheet:

    a.mylink{
    styles here
    }
    

    Inline styling Method

    echo '<a style="styles here" href="'.$client->createAuthUrl().'">Login with Google</a>';
    
    Login or Signup to reply.
  3. From a google development stand point im going to ask why are you doing it like that? Just redirect the user using the location header if you can detect they aren’t logged in.

    index.php

    <a href="Auth/login.php">login</a>
    

    login.php

    $Auth = new Oauth2Authentication();
    $client = $Auth->GetClient();
    $client = $Auth->initializeClient($client);
    

    relevant code from my Oauth2Authentication class.

    function initializeClient($client)
        {
            try {
    
                // Set the refresh token on the client.
                if (isset($_SESSION['refresh_token']) && $_SESSION['refresh_token']) {
                    $client->refreshToken($_SESSION['refresh_token']);
                }
    
                // If the user has already authorized this app then get an access token
                // else redirect to ask the user to authorize access to Google Analytics.
                if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
    
                    // Set the access token on the client.
                    $client->setAccessToken($_SESSION['access_token']);
    
                    // Refresh the access token if it's expired.
                    if ($client->isAccessTokenExpired()) {
                        $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
                        $client->setAccessToken($client->getAccessToken());
                        $_SESSION['access_token'] = $client->getAccessToken();
                    }
                    return $client;
                } else {
                    // We do not have access request access.
                    header('Location: ' . filter_var( $client->getRedirectUri(), FILTER_SANITIZE_URL));
                }
    
            } catch (Exception $e) {
                print "An error occurred: " . $e->getMessage();
            }
    
        }
    

    oauthcallback.php

    <?php
    
    require_once __DIR__. '/../vendor/autoload.php';
    require_once __DIR__ . '/Oauth2Authentication.php';
    
    // Start a session to persist credentials.
    session_start();
    
    // Handle authorization flow from the server.
    
    if (!isset($_GET['code'])) {
    
        // Request authorization of the user by displaying the consent screen
        $Auth = new Oauth2Authentication();
        $client = $Auth->GetClient();
        $auth_url = $client->createAuthUrl();
        header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
    
    } else {
    
        // Exchange the Authorization code for an access token and refresh token.
        $Auth = new Oauth2Authentication();
        $client = $Auth->GetClient();
    
        $client->fetchAccessTokenWithAuthCode($_GET['code']); // Exchange the authentication code for a refresh token and access token.
        // Add access token and refresh token to session.
        $_SESSION['access_token'] = $client->getAccessToken();
        $_SESSION['refresh_token'] = $client->getRefreshToken();
    
        //Redirect back to main script
        $redirect_uri = str_replace("oauth2callback.php","login.php", $client->getRedirectUri());
    
        //header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
        header('Location: http://localhost');
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search