0

I'm trying to set up a Google logon authentication system using PHP that sends its data to into a MySQL database. I've gotten as far as getting the files done and putting them all into my C:\xampp\htdocs directory but this is what happens when I open http://localhost/login.php

Warning: require_once(googleapi/vendor/autoload.php): failed to open stream: No such file or directory in C:\xampp\htdocs\config.php on line 3

Fatal error: require_once(): Failed opening required 'googleapi/vendor/autoload.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\config.php on line 3

Here are all my codes

//login.php file
<?php
    require_once "config.php";
    if (isset($_SESSION['access_token'])) {
        header('Location: index.php');
        exit();
    }
    $loginURL = $gClient->createAuthUrl();
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0,">
          <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Login</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">
</head>
<body>
<div class="container" style="margin-top: 100px">
    <div class="row justify-content-center">
        <div class="col-md-6 col-offset-2" align="center">
        <form>
        <input type="text" placeholder="Email" name="email" class="form-control"><br>
        <input type="password" placeholder="Password" name="password" class="form-control"><br>
        <input type="button" onclick="window.location = '<?php echo $loginURL ?>';"  name="google" value="Login with Google" class="btn btn-primary">
        <input type="reset" name="submit" value="Clear" class="btn btn-primary">
        </form> 
        </div>  
    </div>
</div>
</body>
</html>

//config.php file
<?php
    session_start();
    require_once "googleapi/vendor/autoload.php";
    $gClient = new Google_Client();
    $gClient->setClientId("<removed>");
    $gClient->setClientSecret("<removed>");
    $gClient->setApplicationName("capstoneproject");
    $gClient->setRedirectUri("http://localhost/login/g-callback.php");
    $gClient->addScope("https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email");    
    $con = new mysqli('localhost', 'root','' ,'table');
    if ($con->connect_error) {
    die("Connection failed: " . $con->connect_error);
}   
?>

//index.php file
<?php
    session_start();
    if (!isset($_SESSION['access_token'])) {
        header('Location: login.php');
        exit();
    }
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Login With Google</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
</head>
<body>
<div class="container" style="margin-top: 100px">
    <div class="row">
        <div class="col-md-3">
            <img style="width: 80%;" src="<?php echo $_SESSION['picture'] ?>">
        </div>
        <div class="col-md-9">
            <table class="table table-hover table-bordered">
                <tbody>
                    <tr>
                        <td>ID</td>
                        <td><?php echo $_SESSION['id'] ?></td>
                    </tr>
                    <tr>
                        <td>First Name</td>
                        <td><?php echo $_SESSION['givenName'] ?></td>
                    </tr>
                    <tr>
                        <td>Last Name</td>
                        <td><?php echo $_SESSION['familyName'] ?></td>
                    </tr>
                    <tr>
                        <td>Email</td>
                        <td><?php echo $_SESSION['email'] ?></td>
                    </tr>
                    <tr>
                        <td>Gender</td>
                        <td><?php echo $_SESSION['gender'] ?></td>
                    </tr>
                    <tr>
                        <td>Logout</td>
                        <td><a href="logout.php">Logout</a></td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>
</body>
</html>
//g-callback.php
<?php
    require_once "config.php";
    if (isset($_SESSION['access_token']))
        $gClient->setAccessToken($_SESSION['access_token']);
    else if (isset($_GET['code'])) {
        $token = $gClient->fetchAccessTokenWithAuthCode($_GET['code']);
        $_SESSION['access_token'] = $token;
    } else {
        header('Location: login.php');
        exit();
    }
    $oAuth = new Google_Service_Oauth2($gClient);
    $userData = $oAuth->userinfo_v2_me->get();
    $_SESSION['id'] = $userData['id'];
    $_SESSION['email'] = $userData['email'];
    $_SESSION['gender'] = $userData['gender'];
    $_SESSION['picture'] = $userData['picture'];
    $_SESSION['familyName'] = $userData['familyName'];
    $_SESSION['givenName'] = $userData['givenName'];
 $sql="insert into google_users (clint_id,name,last_name,google_email,gender,picture_link) values
 ('".$userData['id']."','".$userData['givenName']."','".$userData['familyName']."','".$userData['email']."',
 '".$userData['gender']."','".$userData['picture']."')";
    mysqli_query($con,$sql);
    header('Location: index.php');
    exit();
?>
Mike
  • 1
  • 2
  • 1
    Are you sure the `login.php` is at the same folder level as the folder `googleapi`? –  Mar 29 '19 at 02:54
  • Your `include_path` does not include `.` (the current working directory). Prefix your paths with `__DIR__`, eg `require_once __DIR__ . '/googleapi/vendor/autoload.php';`. That being said, `require_once "config.php"` should also fail but it does not appear to be the case – Phil Mar 29 '19 at 02:56
  • I did that on all my files, and I get this error now: Warning: require_once(C:\xampp\htdocsconfig.php): failed to open stream: No such file or directory in C:\xampp\htdocs\login.php on line 2 Fatal error: require_once(): Failed opening required 'C:\xampp\htdocsconfig.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\login.php on line 2 – Mike Mar 29 '19 at 13:03

0 Answers0