PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
<?php // Configuration $client_id = 'YOUR_CLIENT_ID'; $client_secret = 'YOUR_CLIENT_SECRET'; $redirect_uri = 'YOUR_REDIRECT_URI'; $db_host = 'YOUR_DB_HOST'; $db_username = 'YOUR_DB_USERNAME'; $db_password = 'YOUR_DB_PASSWORD'; $db_name = 'YOUR_DB_NAME'; // Connect to database $conn = new mysqli($db_host, $db_username, $db_password, $db_name); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // Google API setup require_once 'vendor/autoload.php'; $google_client = new Google_Client(); $google_client->setClientId($client_id); $google_client->setClientSecret($client_secret); $google_client->setRedirectUri($redirect_uri); $google_client->addScope(Google_Service_Oauth2::USERINFO_EMAIL); $google_client->addScope(Google_Service_Oauth2::USERINFO_PROFILE); // Login with Google if (isset($_GET['code'])) { $token = $google_client->authenticate($_GET['code']); $google_client->setAccessToken($token); $oauth2 = new Google_Service_Oauth2($google_client); $userinfo = $oauth2->userinfo->get(); $username = $userinfo->getName(); $email = $userinfo->getEmail(); // Check if user exists in database $query = "SELECT * FROM users WHERE email = '$email'"; $result = $conn->query($query); if ($result->num_rows > 0) { // User exists, log them in $user = $result->fetch_assoc(); $_SESSION['user_id'] = $user['id']; header('Location: dashboard.php'); exit; } else { // User does not exist, sign them up $query = "INSERT INTO users (username, email, password) VALUES ('$username', '$email', '')"; $conn->query($query); $_SESSION['user_id'] = $conn->insert_id; header('Location: dashboard.php'); exit; } } // Login form if (isset($_POST['username']) && isset($_POST['password'])) { $username = $_POST['username']; $password = $_POST['password']; $query = "SELECT * FROM users WHERE username = '$username' AND password = '$password'"; $result = $conn->query($query); if ($result->num_rows > 0) { $user = $result->fetch_assoc(); $_SESSION['user_id'] = $user['id']; header('Location: dashboard.php'); exit; } else { echo 'Invalid username or password'; } } // Sign up form if (isset($_POST['username']) && isset($_POST['email']) && isset($_POST['password'])) { $username = $_POST['username']; $email = $_POST['email']; $password = $_POST['password']; $query = "INSERT INTO users (username, email, password) VALUES ('$username', '$email', '$password')"; $conn->query($query); $_SESSION['user_id'] = $conn->insert_id; header('Location: dashboard.php'); exit; } // Google Sign-In button echo '<a href="' . $google_client->createAuthUrl() . '">Sign in with Google</a>'; // Login form echo '<form action="" method="post">'; echo 'Username: <input type="text" name="username"><br>'; echo 'Password: <input type="password" name="password"><br>'; echo '<input type="submit" value="Login">'; echo '</form>'; // Sign up form echo '<form action="" method="post">'; echo 'Username: <input type="text" name="username"><br>'; echo 'Email: <input type="email" name="email"><br>'; echo 'Password: <input type="password" name="password"><br>'; echo '<input type="submit" value="Sign up">'; echo '</form>'; ?>
Copy Clear
Copy Format Clear
<?php // Configuration $client_id = 'YOUR_CLIENT_ID'; $client_secret = 'YOUR_CLIENT_SECRET'; $redirect_uri = 'YOUR_REDIRECT_URI'; // Google API setup require_once 'vendor/autoload.php'; $google_client = new Google_Client(); $google_client->setClientId($client_id); $google_client->setClientSecret($client_secret); $google_client->setRedirectUri($redirect_uri); $google_client->addScope(Google_Service_Oauth2::USERINFO_EMAIL); $google_client->addScope(Google_Service_Oauth2::USERINFO_PROFILE); // Login with Google if (isset($_GET['code'])) { $token = $google_client->authenticate($_GET['code']); $google_client->setAccessToken($token); $oauth2 = new Google_Service_Oauth2($google_client); $userinfo = $oauth2->userinfo->get(); $username = $userinfo->getName(); $email = $userinfo->getEmail(); // Check if user exists in database if ($mysqli) { $query = "SELECT * FROM users WHERE email = '$email'"; $result = $mysqli->query($query); if ($result->num_rows > 0) { // User exists, log them in $user = $result->fetch_assoc(); $_SESSION['user_id'] = $user['id']; header('Location: dashboard.php'); exit; } else { // User does not exist, sign them up $query = "INSERT INTO users (username, email, password) VALUES ('$username', '$email', '')"; $mysqli->query($query); $_SESSION['user_id'] = $mysqli->insert_id; header('Location: dashboard.php'); exit; } } elseif ($pdo) { $stmt = $pdo->prepare("SELECT * FROM users WHERE email = :email"); $stmt->bindParam(':email', $email); $stmt->execute(); $user = $stmt->fetch(); if ($user) { // User exists, log them in $_SESSION['user_id'] = $user['id']; header('Location: dashboard.php'); exit; } else { // User does not exist, sign them up $stmt = $pdo->prepare("INSERT INTO users (username, email, password) VALUES (:username, :email, '')"); $stmt->bindParam(':username', $username); $stmt->bindParam(':email', $email); $stmt->execute(); $_SESSION['user_id'] = $pdo->lastInsertId(); header('Location: dashboard.php'); exit; } } } // Login form if (isset($_POST['username']) && isset($_POST['password'])) { $username = $_POST['username']; $password = $_POST['password']; if ($mysqli) { $query = "SELECT * FROM users WHERE username = '$username' AND password = '$password'"; $result = $mysqli->query($query); if ($result->num_rows > 0) { $user = $result->fetch_assoc(); $_SESSION['user_id'] = $user['id']; header('Location: dashboard.php'); exit; } else { echo 'Invalid username or password'; } } elseif ($pdo) { $stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username AND password = :password"); $stmt->bindParam(':username', $username); $stmt->bindParam(':password', $password); $stmt->execute(); $user = $stmt->fetch(); if ($user) { $_SESSION['user_id'] = $user['id']; header('Location: dashboard.php'); exit; } else { echo 'Invalid username or password'; } } } // Sign up form if (isset($_POST['username']) && isset($_POST['email']) && isset($_POST['password'])) { $username = $_POST['username']; $email = $_POST['email']; $password = $_POST['password']; if ($mysqli) { $query = "INSERT INTO users (username, email, password) VALUES ('$username', '$email', '$password')"; $mysqli->query($query); $_SESSION['user_id'] = $mysqli->insert_id; header('Location: dashboard.php'); exit; } elseif ($pdo) { $stmt = $pdo->prepare("INSERT INTO users (username, email, password) VALUES (:username, :email, :password)"); $stmt->bindParam(':username', $username); $stmt->bindParam(':email', $email); $stmt->bindParam(':password', $password);}
Show:  
Copy Clear