2016-10-04 160 views
3

我正在建立一个类似Intranet的网站供我的高中生使用,我希望他们使用谷歌登录登录,因为他们都有谷歌帐户。我已经使用Google的说明成功整合了Google登录功能。从登录页面重定向

当新用户访问我网站的第一页(登录页面)并登录时,我希望它们自动重定向到第二页。我在SO和其他地方研究过这个,但还没有发现任何东西。我怎么能这样做?

此外,如果用户尝试访问除第一页以外的任何页面而未先登录,那么如何将它们重定向到第一页?

我的网站地址和代码如下。让我知道你是否需要更多信息。谢谢!

第一页: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> 

第二页: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> 

+0

如何保存成功登录?使用会话?如果没有,请设置一个名为“loggedIn”的会话,如果没有设置,就重定向到登录页面,如果设置了第一页,则重定向到第二页面。 –

+0

使用标题功能重定向 –

回答

1

有很多方法可以做到这一点。但

最简单解决方案是将此代码放在每个php页面的顶部,以便如果用户访问任何页面和会话未找到,那么用户将被重定向到登录页面。

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

并用您的登录php文件替换login.php。它只是检查会话变量,如果没有找到,然后重定向到登录页面。并在登录页面上设置会话变量。

在登录PHP页面(即要求用户输入自己的邮箱密码),把下面的代码,你可以这样做:

获取用户名和HTML表单密码,然后传递到数据库,以便发现注册用户。如果从mysql结果行中找到该用户,则将这些值放入会话变量中。

<?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"); 
    } 
    ?> 

您的html格式将如下所示。

<!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> 
+0

感谢会议和标题功能的想法。他们完全按照我需要的方式工作。但我不收集我的用户的登录数据; Google通过OAuth来做到这一点。所以没有HTML表单和SQL数据库来查询。有一个JS函数会在他们登录时触发。我研究了是否可以使用它将PHP变量设置为true,但它看起来是一个[坏主意](http://stackoverflow.com/questions/3590293/set -session-使用JavaScript的功能于PHP可变)。关于如何设置会话变量的任何想法? –

+0

我明白你想验证和设置没有JavaScript的会话。请看[这里](http://phppot.com/php/php-google-oauth-login/)和[这里](https://github.com/search?utf8=%E2%9C%93&q=php +谷歌+的OAuth) –