php - 创建一个简单的基于 PHP 和 MYSQL 的登录系统

标签 php mysql redirect login header

这个问题不太可能帮助任何 future 的访客;它仅与一个小地理区域、一个特定时刻或一个非常狭窄的情况相关,而这些情况通常不适用于互联网的全局受众。如需帮助使这个问题更广泛地适用,visit the help center .




8年前关闭。




我正在跟随这里的教程:

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()”函数,但没有成功。

更新

我收到此错误:

Parse error: syntax error, unexpected T_STRING in /Users/Eamon/Sites/includes/login.inc.php on line 13



有没有人看到任何语法错误? (登录.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');
    }
}

?>

最佳答案

您的 redirect函数正在设置 Location标题。这不适用于相对路径。

redirect('../index.php');

你不能那样做。您需要使用页面的绝对路径,例如:
redirect('http://example.com/index.php');

关于php - 创建一个简单的基于 PHP 和 MYSQL 的登录系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17067751/

相关文章:

Spring Post Controller重定向仅打印url而不是重定向

.htaccess - Cloudflare 灵活的 SSL 和重定向

php - 使用 PHP 在 Windows 中获取文件所有者

php - 在 PHP 中组合多个循环 MySQL 查询

mysql - 当我选择一个元时,如何获取记录中的所有元

mysql - 如何在mysql查询中应用GROUP_CONCAT

Django - 基于类的通用 View - "No URL to redirect to"

php - 从 print_r 或 var_dump 隐藏特定类字段

javascript - 使用框输入重定向页面

Python MySQL 连接器返回 bytearray 而不是常规字符串值