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

问题描述 投票:0回答:1

这是我的PHP Google登录脚本。它可以在localhost上运行,但是当我将其放置在在线服务器上时,它无法正常工作。我想重定向特定页面,但不重定向。身份验证后,它显示HTTP错误500。解决方案是什么?

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

authenticate.php

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

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

<?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>
php google-login
1个回答
0
投票

是的!我得到答案。

[当我使用(.cf)域或任何其他免费域或免费托管时,它在联机服务器上不起作用。但是,当我尝试使用付费顶级域名并托管其正常工作时。

© www.soinside.com 2019 - 2024. All rights reserved.