简体   繁体   中英

How to style for <a href> for php

<?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>";
?>

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

<a href="<?=createAuthUrl()?>" class="your-class-name">Login with Google"</a>

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>';

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');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM