首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google登录未重定向到在线服务器上的特定PHP页面

问Google登录未重定向到在线服务器上的特定PHP页面
EN

Stack Overflow用户
提问于 2020-05-18 01:45:53
回答 1查看 150关注 0票数 0

这是我的PHP Google登录脚本。它可以在本地主机上工作,但是当我把它放到在线服务器上时,它不能工作。我想重定向一个特定的页面,但不是重定向。身份验证后,它显示HTTP错误500。解决方案是什么?

在本地主机上,它的工作很顺利。但在在线服务器上,它显示HTTP错误500。为什么?

authenticate.php

代码语言:javascript
复制
<?php 
require_once 'config.php';


if(isset($_GET['code'])){
    $googleClient->authenticate($_GET['code']);
    $_SESSION['token'] = $googleClient->getAccessToken();
    header('Location: ' . filter_var($redirectURL, FILTER_SANITIZE_URL));
}
############ Set Google access token ############
if (isset($_SESSION['token'])) {
    $googleClient->setAccessToken($_SESSION['token']);
}


if ($googleClient->getAccessToken()) {
    ############ Fetch data from graph api  ############
    try {
        $gpUserProfile = $google_oauthV2->userinfo->get();
    }
    catch (\Exception $e) {
        echo 'Graph returned an error: ' . $e->getMessage();
        session_destroy();
        header("Location: ./");
        exit;
    }
    ############ Store data in database  ############
    $oauthpro = "google";
    $oauthid = $gpUserProfile['id'] ?? '';
    $first_name = $gpUserProfile['given_name'] ?? '';
    $last_name = $gpUserProfile['family_name'];
    $email_id = $gpUserProfile['email'] ?? '';
    $picture = $gpUserProfile['picture'] ?? '';
    $sql = "SELECT * FROM users WHERE oauthid='".$gpUserProfile['id']."'";
    $result = $conn->query($sql);
    if ($result->num_rows == 1) {
       $conn->query("UPDATE users SET first_name='".$first_name."', last_name='".$last_name."', email_id='".$email_id."', picture='".$picture."' where oauthid='".$oauthid."' ");
    } else {
        $conn->query("INSERT INTO users (oauth_pro, oauthid, first_name, last_name, email_id, picture) VALUES ('".$oauthpro."', '".$oauthid."', '".$first_name."', '".$last_name."', '".$email_id."', '".$picture."')");  
    }
    $res = $conn->query($sql);
    $Loggedin = $res->fetch_assoc();

    $_SESSION['Loggedin'] = $Loggedin;
    header("Location: ../../study.php");

} else {
    header("Location:/");
}


?>

config.php

代码语言:javascript
复制
<?php
session_start();
require '../../connect.php';
##### Google App Configuration #####
$googleappid = "1028475651905-m925oqjild1gcg0uqftve3t3bi7b761e.apps.googleusercontent.com"; 
$googleappsecret = "XN4AiqDE8sBOtC3Ao49YC5__"; 
$redirectURL = "http://example.com/login/google/authenticate.php"; 
//$redirectURL = "YourRedirectURL"; 

##### Required Library #####
include_once 'src/Google/Google_Client.php';
include_once 'src/Google/contrib/Google_Oauth2Service.php';

$googleClient = new Google_Client();
$googleClient->setApplicationName('Login to ইচ্ছে');
$googleClient->setClientId($googleappid);
$googleClient->setClientSecret($googleappsecret);
$googleClient->setRedirectUri($redirectURL);

$google_oauthV2 = new Google_Oauth2Service($googleClient);

?>

index.php

代码语言:javascript
复制
<?php
session_start();
require_once 'config.php';
if(isset($_SESSION['Loggedin'])){
    header('location: ../../study.php');
}
$loginURL="";
$authUrl = $googleClient->createAuthUrl();
$loginURL = filter_var($authUrl, FILTER_SANITIZE_URL);
?>
<!DOCTYPE html>
<html>
<body>
    <a href="<?= htmlspecialchars( $loginURL ); ?>">
        <div class="google-btn">
            <div class="google-icon-wrapper">
                <img class="google-icon" src="login/Google.png"/>
            </div>
            <p class="btn-text"><b>Log in with Google</b></p>
        </div>
    </a>
</body>
</html>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-26 12:46:56

是!我有答案了。

当我使用(.cf)域或任何其他免费域名或免费托管时,它不能在在线服务器上工作。但当我尝试与一个付费的顶级域名和托管其正常工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61856168

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档