我正在跟随这里的教程:
https://www.dropbox.com/s/lta1r9xhk2u2ef0/3592029.doc
我基本上使用教程中的所有代码。我的问题是,我登录后没有重定向 - 它只是停留在登录页面上。我使用用户名“admin”和密码在 mysql 数据库中创建了一个用户。我相信我输入正确 - 这是我的代码:
登录.inc.php
<?php
// Include required MySQL configuration file and functions
require_once('config.inc.php');
require_once('functions.inc.php');
// Start session
session_start();
// Check if user is already logged in
if ($_SESSION['logged_in'] == true) {
// If user is already logged in, redirect to main page
redirect('../index.php');
}
else {
// Make sure that user submitted a username/password and username only consists of alphanumeric chars
if ( (!isset($_POST['username'])) || (!isset($_POST['password'])) OR (!ctype_alnum($_POST['username'])) ) {
redirect('../login.php');
}
// Connect to database
$mysqli = @new mysqli(DB_HOSTNAME, DB_USERNAME,
DB_PASSWORD, DB_DATABASE);
// Check connection
if (mysqli_connect_errno()) {
printf("Unable to connect to database: %s",
mysqli_connect_error());
exit();
}
// Escape any unsafe characters before querying database
>real_escape_string($_POST['username']);
>real_escape_string($_POST['password']);
$sql = "SELECT * FROM users WHERE username = '" . $username . "' AND password = '" . md5($password) . "'";
$result = $mysqli->query($sql);
// If one row is returned, username and password are valid
if (is_object($result) && $result->num_rows == 1) {
// Set session variable for login status to true
$_SESSION['logged_in'] = true;
redirect('../index.php');
}
else {
// If number of rows returned is not one, redirect back to login screen
redirect('../login.php');
}
}
?>
登录.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-type" content="text/html;charset=utf-8" />
<title>Creating a Simple PHP and MySQL-Based Login System</title>
</head>
<body>
<form id="login-form" method="post" action="includes/login.inc.php">
Username: <input type="text" name="username" id="username"><br>
Password: <input type="password" name="password" id="password"><br>
<input type="submit" id = "submit" name="Submit" value="Login">
</form>
</body>
</html>
所需的帮助文件是:
函数.inc.php:
<?php
/**
* Crucial Functions for Application
*
* @package tpc_tutorials
* @file /includes/functions.inc.php
*
* Redirects to specified page
*
* @param string $page Page to redirect user to
* @return void
*/
function redirect($page) {
header('Location: ' . $page);
exit();
}
/**
* Check login status
*
* @return boolean Login status
*/
function check_login_status() {
// If $_SESSION['logged_in'] is set, return the status
if (isset($_SESSION['logged_in'])) {
return $_SESSION['logged_in'];
}
return false;
}
?>
配置文件
<?php
/**
* MySQL Database Configuration
*
* @file /includes/config.inc.php
* @note Replace the settings below with those for your MySQL database.
*/
define('DB_HOSTNAME', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'xxx');
define('DB_DATABASE', 'itit');
?>
我尝试用“header(...)”函数切换“redirect()”函数,但没有成功。
更新
我收到此错误:
解析错误:语法错误,第 13 行 /Users/Eamon/Sites/includes/login.inc.php 中的意外 T_STRING
有没有人看到任何语法错误?(登录.inc.php)
<?php
error_reporting(E_ALL); ini_set('display_errors', '1');
// Include required MySQL configuration file and functions
require_once('config.inc.php');
require_once('functions.inc.php');
// Start session
session_start();
// Check if user is already logged in
if ($_SESSION['logged_in'] == true) {
// If user is already logged in, redirect to main page
redirect('../index.php');
}
else {
// Make sure that user submitted a username/password and username only consists of alphanumeric chars
if ((!isset($_POST['username'])) || (!isset($_POST['password'])) OR (!ctype_alnum($_POST['username']))) {
redirect('../login.php');
}
// Connect to database
$mysqli = new mysqli(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
// Check connection
if (mysqli_connect_errno()) {
printf("Unable to connect to database: %s",
mysqli_connect_error());
exit();
}
// Escape any unsafe characters before querying database
real_escape_string($_POST['username']);
real_escape_string($_POST['password']);
$sql = "SELECT * FROM users WHERE username = '" . $username . "' AND password = '" . md5($password) . "'";
$result = $mysqli->query($sql);
// If one row is returned, username and password are valid
if (is_object($result) && $result->num_rows == 1) {
// Set session variable for login status to true
$_SESSION['logged_in'] = true;
redirect('http://localhost/~Eamon/index.php');
}
else {
// If number of rows returned is not one, redirect back to login screen
redirect('../login.php');
}
}
?>