我能够成功修改“使用Google + API登录”的代码&在localhost上运行良好。
我正在尝试将$authUrl
(gplus-login.php)值分配给另一个PHP文件“trackorder.php”图像(在div标签内)。
Trackorder.php 文件我要放置 $ authUrl 值
<div style="margin-left:50px;">
<a href="">
<img src="images/Google-button.png" style="width:200px; height:35px;"></a>
</div>
gplus-login.php中
<?php
require_once 'src/apiClient.php';
require_once 'src/contrib/apiPlusService.php';
session_start();
$client = new apiClient();
$client->setApplicationName("My Project");
$client->setScopes(array('https://www.googleapis.com/auth/plus.me'));
$plus = new apiPlusService($client);
if (isset($_REQUEST['logout']))
{
unset($_SESSION['access_token']);
}
if (isset($_GET['code']))
{
$client->authenticate();
$_SESSION['access_token'] = $client->getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
if (isset($_SESSION['access_token']))
{
$client->setAccessToken($_SESSION['access_token']);
}
if ($client->getAccessToken())
{
$me = $plus->people->get('me');
$optParams = array('maxResults' => 100);
$activities = $plus->activities->listActivities('me', 'public',$optParams);
// The access token may have been updated lazily.
$_SESSION['access_token'] = $client->getAccessToken();
}
else
{
$authUrl = $client->createAuthUrl();
}
?>
<div>
<?php if(isset($me))
{
$_SESSION['gplusdata']=$me;
header("location: home.php");
}
?>
<?php
if(isset($authUrl))
{
print "<a class='login' href="$authUrl">Google Login</a>";
}
else
{
print "<a class='logout' href='trackorder.php?logout'>Logout</a>";
}
?><br/>
</div>
答案 0 :(得分:0)
您可以包含Trackorder.php并在include之前设置动态变量。
<?php
if(!isset($authUrl)) {
$authUrl = 'trackorder.php?logout';
$authLabel = 'Logout';
$authClass = 'logout';
} else {
$authLabel = 'Google Login';
$authClass = 'login';
}
include('Trackorder.php');
?>
和Trackorder.php
<div style="margin-left:50px;">
<a href="<?= $authUrl; ?>" class="<?= $authClass; ?>"><?= $authLabel; ?>
<img src="images/Google-button.png" style="width:200px; height:35px;"></a>
</div>
不确定您要对链接中的图片做什么。
如果您只想存在Trackorder.php文件以进行登录,只需将include移动到else内部并恢复初始if子句以吐出注销链接。