3

I am building an intranet-like website for my high school students to use and I would like them to login using google sign-in because they all have google accounts. I have successfully integrated google sign-in using Google's instructions.

When a new user visits the first page (the login page) of my site and logs in, I would like them to be automatically redirected to the second page. I have researched this on SO and elsewhere but not found anything yet. How could I do this?

Also, if a user tries to access any page other than the first page without having first logged in, how can I redirect them to the first page?

My website address and code is below. Let me know if you need any more information. Thanks!

First page: http://davidstarshaw.atwebpages.com/test/index.php

<!DOCTYPE html>
<head>
    <title>Test login page</title>
    <meta name="google-signin-client_id" content="795531022003-rdb02epf7o0qpr5p83326icrseh82gqa.apps.googleusercontent.com"> <!--My google sign-in client ID-->
    <script src="https://apis.google.com/js/platform.js" async defer</script> <!--Google platform library. Needed for sign-in-->
    <script src="script.js" async defer></script>
</head>

<body>
    <p>This is the first page.</p>
    <div class="g-signin2" data-onsuccess="onSignIn"></div> <!--This code is straight from google-->
    <a href="#" onclick="signOut();">Sign out</a><br>
    <a href=home.php>Manually go to the second page</a>
</body>

Second page: http://davidstarshaw.atwebpages.com/test/home.php

<!DOCTYPE html>
<head>
    <title>Test home page</title>
    <meta name="google-signin-client_id" content="795531022003-rdb02epf7o0qpr5p83326icrseh82gqa.apps.googleusercontent.com"> <!--My google sign-in client ID-->
    <script src="https://apis.google.com/js/platform.js" async defer></script> <!--Google platform library. Needed for sign-in-->
    <script src="script.js" async defer></script>
</head>

<body>
    <p>This is the second page.</p>
    <div class="g-signin2" data-onsuccess="onSignIn"></div> <!--This code is straight from google-->
    <a href="#" onclick="signOut();">Sign out</a><br>
    <a href=index.php>Manually go back to the first page</a>

  • How do you store a successful login? Using sessions? If not, set a session called "loggedIn" and just redirect to login page if it's not set, and on the first page, redirect to second page if it is set. – M. Eriksson Oct 04 '16 at 04:58
  • use header fuction to redirect – Sanooj T Oct 04 '16 at 05:05

1 Answers1

1

there are lots of ways you can do this. but

the simplest solution is to put this code at top of each php page so that if user visit any page and session is not found then user will be redirected to login page.

<?php 
// put at the top of each php page.
session_start();
if (!isset($_SESSION["Authenticated"]))
{
  header("location: login.php");
}
?>

and replace login.php with your login php file. what it does just check session variable, if not found then redirect to login page. and on login page you can set session variable.

On login php page (where you ask user to enter his email password) put the following code, you can do this:

get user name and password from html form then pass to database in order to find that register user. if found that user from mysql results row then put these values in session variable.

<?php
  // put in only login page
  $email = $_REQUEST['email'];  // get email from html form
  $password = $_REQUEST['password'];  // get password from html form

  // search for this user in database.
  $query = mysqli_query("select * from MyRegisteredUser where email='$email' and password='$password'");
  // if any record is founf then get that records fields
  if(mysqli_num_rows($query)){
    $result_row = mysqli_fetch_fields($query);
    $_SESSION["Authenticated"] = true;
    $_SESSION['id'] = $result_row->id;
    $_SESSION['Name'] = $result_row->name;
    $_SESSION['Email'] = $result_row->email;
  }else{
      // session can't be set due to no user found. so redirect back with some error.
    unset($_SESSION);
    header("location: login.php");
  }
  ?>

your html form will look like this.

  <!DOCTYPE html>
  <head>
    <title>Login</title>
  </head>
  <body>
    <form method="post" action="login.php">
      <div class="container">
        <label><b>Email</b></label>
        <input type="text" placeholder="Enter Email" name="email"></input>
        <label><b>Password</b></label>
        <input type="password" placeholder="Password" name="password"></input>
        <input type="submit" value="Login" name="login_btn"></input>
      </div>
    </form>
  </body>
</html>
Kaleem Ullah
  • 6,799
  • 3
  • 42
  • 47
  • Thanks for the session and header function ideas. They work exactly as I need them to. But I don't collect my user's login data; Google does that with OAuth. So there's no HTML form nor a SQL database to query. There's a JS function that triggers when they sign in. I looked into whether I could use this to set the PHP variable to true but it looks to be a [bad idea](http://stackoverflow.com/questions/3590293/set-session-variable-using-javascript-in-php). Any thoughts on how I could set the session variable? – David Starshaw Oct 07 '16 at 09:43
  • what I understand you want authenticate and set session without javascript. please look [here](http://phppot.com/php/php-google-oauth-login/) and [here](https://github.com/search?utf8=%E2%9C%93&q=php+google+oauth) – Kaleem Ullah Oct 07 '16 at 12:05