成功登录后(即用户在输入用户名和密码后单击登录按钮),根据我要将用户发送到特定页面的用户类型。以下是我的代码。 (这里,如果usertype是'employee',他应该在点击'login'按钮后进入“data-update.php”页面。)
这是index.php页面中的代码:
// This file is the home page.
// Require the configuration before any PHP code as the configuration controls error reporting:
require ('config.inc.php');
// The config file also starts the session.
// Require the database connection:
require (MYSQL);
// If it's a POST request, handle the login attempt:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
include ('login.inc.php');
}
/* PAGE CONTENT STARTS HERE! */
?>
<!DOCTYPE html>
<html lang="en">
<head>
...
这是login.inc.php页面中的代码:
<?php
// This is the login page for the site.
// It's included by index.php, which receives the login form data.
// Array for recording errors:
$login_errors = array();
// Validate the username:
// Validate the password:
if (empty($login_errors)) { // OK to proceed!
// Query the database:
$q = "SELECT id, type FROM users WHERE (username='$u' AND pass='" . get_password_hash($p) . "')";
$r = mysqli_query ($dbc, $q);
if (mysqli_num_rows($r) == 1) { // A match was made.
// Get the data:
$row = mysqli_fetch_array ($r, MYSQLI_NUM);
// Store the data in a session:
$_SESSION['user_id'] = $row[0];
$_SESSION['username'] = $u;
if ($row[1] == 'employee') {
reset_sms_update_session();
header("Location: http://".BASE_URL."data-update.php");
exit;
}
elseif ($row[1] == 'client'){
header("Location: http://".BASE_URL."About.html");
exit;
}
...
这是“data-update.php”页面中的部分代码:
// Start the session:
session_start();
function redirect_invalid_user($check = 'user_id', $destination = 'index.php', $protocol = 'http://') {
// Check for the session item:
if (!isset($_SESSION[$check])) {
$url = $protocol . BASE_URL . $destination; // Define the URL.
header("Location: $url");
exit(); // Quit the script.
}
} // End of redirect_invalid_user() function.
//Redirect to index.php if user not logged in
redirect_invalid_user( );
/* PAGE CONTENT STARTS HERE! */
?>
<!DOCTYPE html>
...
问题是,在谷歌浏览器中,当用户点击登录按钮时,他首先被带到index.php页面(重定向发生的地方),然后他必须再次点击登录按钮以获取“数据 - update.php“页面。 为什么会在Google Chrome中发生这种情况? (仅供参考:在本地PC的XAMP测试环境中不会发生这种情况。)
在Firefox或IE8中不会发生这种情况(即用户在第一次点击登录按钮时会被带到“data-update.php”)。
请帮忙。
答案 0 :(得分:0)
我通过在{@ 1}}到http://example.com
的网络服务器中设置CPanel的永久重定向来解决此问题。