ここのチュートリアルに従っています:
https://www.dropbox.com/s/lta1r9xhk2u2ef0/3592029.doc
私は基本的にチュートリアルのすべてのコードを使用しています。私の問題は、ログイン後にリダイレクトがなく、ログイン ページにとどまることです。ユーザー名「admin」とパスワードを使用して、mysql データベースにユーザーを作成しました。私はそれらを正しく入力していると信じています-これが私のコードです:
login.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');
}
}
?>
login.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>
必要なヘルパー ファイルは次のとおりです。
functions.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;
}
?>
config.inc.php
<?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');
?>
「redirect()」関数を「header(...)」関数で切り替えようとしましたが、うまくいきませんでした。
アップデート
次のエラーが表示されます。
解析エラー: 構文エラー、13 行目の /Users/Eamon/Sites/includes/login.inc.php の予期しない T_STRING
構文エラーが表示される人はいますか? (login.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');
}
}
?>