2016-10-04 156 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) –