【问题标题】:PHP page shows up blankPHP页面显示为空白
【发布时间】:2014-07-29 03:17:12
【问题描述】:

我是 PHP 新手,我正在关注 this tutorial 创建登录页面。当我完成教程时,页面完全是白色的,查看源代码也显示一个空白页面。这是我收到的错误“发生解析错误 消息:语法错误,文件意外结束”这是代码:

    <?php
include_once 'psl-config.php';

function sec_session_start() {
    $session_name = 'sec_session_id';   // Set a custom session name
    $secure = SECURE;
    // This stops JavaScript being able to access the session id.
    $httponly = true;
    // Forces sessions to only use cookies.
    if (ini_set('session.use_only_cookies', 1) === FALSE) {
        header("Location: ../error.php?err=Could not initiate a safe session (ini_set)");
        exit();
    }
    // Gets current cookies params.
    $cookieParams = session_get_cookie_params();
    session_set_cookie_params($cookieParams["lifetime"],
        $cookieParams["path"], 
        $cookieParams["domain"], 
        $secure,
        $httponly);
    // Sets the session name to the one set above.
    session_name($session_name);
    session_start();            // Start the PHP session 
    session_regenerate_id();    // regenerated the session, delete the old one. 

function login($email, $password, $mysqli) {
    // Using prepared statements means that SQL injection is not possible. 
    if ($stmt = $mysqli->prepare("SELECT id, username, password, salt 
        FROM members
       WHERE email = ?
        LIMIT 1")) {
        $stmt->bind_param('s', $email);  // Bind "$email" to parameter.
        $stmt->execute();    // Execute the prepared query.
        $stmt->store_result();

        // get variables from result.
        $stmt->bind_result($user_id, $username, $db_password, $salt);
        $stmt->fetch();

        // hash the password with the unique salt.
        $password = hash('sha512', $password . $salt);
        if ($stmt->num_rows == 1) {
            // If the user exists we check if the account is locked
            // from too many login attempts 

            if (checkbrute($user_id, $mysqli) == true) {
                // Account is locked 
                // Send an email to user saying their account is locked
                return false;
            } else {
                // Check if the password in the database matches
                // the password the user submitted.
                if ($db_password == $password) {
                    // Password is correct!
                    // Get the user-agent string of the user.
                    $user_browser = $_SERVER['HTTP_USER_AGENT'];
                    // XSS protection as we might print this value
                    $user_id = preg_replace("/[^0-9]+/", "", $user_id);
                    $_SESSION['user_id'] = $user_id;
                    // XSS protection as we might print this value
                    $username = preg_replace("/[^a-zA-Z0-9_\-]+/", 
                                                                "", 
                                                                $username);
                    $_SESSION['username'] = $username;
                    $_SESSION['login_string'] = hash('sha512', 
                              $password . $user_browser);
                    // Login successful.
                    return true;
                } else {
                    // Password is not correct
                    // We record this attempt in the database
                    $now = time();
                    $mysqli->query("INSERT INTO login_attempts(user_id, time)
                                    VALUES ('$user_id', '$now')");
                    return false;
                }
            }
        } else {
            // No user exists.
            return false;
        }
    }
}

function checkbrute($user_id, $mysqli) {
    // Get timestamp of current time 
    $now = time();

    // All login attempts are counted from the past 2 hours. 
    $valid_attempts = $now - (2 * 60 * 60);

    if ($stmt = $mysqli->prepare("SELECT time 
                             FROM login_attempts 
                             WHERE user_id = ? 
                            AND time > '$valid_attempts'")) {
        $stmt->bind_param('i', $user_id);

        // Execute the prepared query. 
        $stmt->execute();
        $stmt->store_result();

        // If there have been more than 5 failed logins 
        if ($stmt->num_rows > 5) {
            return true;
        } else {
            return false;
        }
    }
}

function esc_url($url) {

    if ('' == $url) {
        return $url;
    }

    $url = preg_replace('|[^a-z0-9-~+_.?#=!&;,/:%@$\|*\'()\\x80-\\xff]|i', '', $url);

    $strip = array('%0d', '%0a', '%0D', '%0A');
    $url = (string) $url;

    $count = 1;
    while ($count) {
        $url = str_replace($strip, '', $url, $count);
    }

    $url = str_replace(';//', '://', $url);

    $url = htmlentities($url);

    $url = str_replace('&amp;', '&#038;', $url);
    $url = str_replace("'", '&#039;', $url);

    if ($url[0] !== '/') {
        // We're only interested in relative links from $_SERVER['PHP_SELF']
        return '';
    } else {
        return $url;
    }
}
?>

【问题讨论】:

  • 通常意味着致命错误。
  • 死机白页,报错\显示关闭,开启error_reporting(E_ALL); ini_set('display_errors', 1);
  • 检查服务器上的PHP错误日志。
  • ini_set('display_errors', 1); error_reporting(E_ALL); 放在脚本的顶部。
  • sec_session_start() 不是原生 PHP 函数。该函数是否已在您调用的某个包含文件中定义?

标签: php html mysql login


【解决方案1】:

我猜你的问题在于这些脚本......

include_once 'includes/db_connect.php';
include_once 'includes/functions.php';

正如其他人所提到的,您可以在 php.ini 中启用错误报告,但我相信它是默认启用的。您需要做的是确保对这些脚本中的所有命令进行错误检查。

编辑: 删除自 sec_session_start(); 以来的最后一个建议是您教程中的一个功能。它必须在那里。

【讨论】:

  • 在函数页面的第 141 行显示错误,这是代码“?>”的结尾,这让我感到困惑
  • 确保您的 php 文件末尾没有空行,?> 应该是最后一行。
  • “出现解析错误消息:语法错误,文件意外结束”这没有意义,后面什么都没有?>
  • 听起来你缺少右括号 } 或 ;在一行的末尾。
  • 你在最后缺少一个右括号 }。
【解决方案2】:

(1) 将以下行放在脚本顶部 &lt;?php 正下方

ini_set('display_errors', 1);
error_reporting(E_ALL);

(2) 将sec_session_start(); 替换为session_start();

它应该可以解决问题!

【讨论】:

  • 我非常怀疑这一点。 sec_session_start() 是人们经常使用的(自定义)函数,其中包含 session_start() 但是,嘿,你说对了 50%。编辑:根据the tutorial OP 如下:function sec_session_start()...
  • 这里不是无限递归吗?我看到session_start 在里面打电话。
【解决方案3】:

这是我在查看您的代码时看到的:

<?php
include_once 'psl-config.php';

function sec_session_start() {
    // lot of code here
    // but closing tag of the function is missing

    // therefore following functions are actually define inside sec_session_start()
    // this should produce a parser error.
    function login($email, $password, $mysqli) {
        // lot of code here
    }

    function esc_url($url) {
        // code
    }
?>

首先,您应该通过以下方式显示解析器错误:

ini_set('display_errors', 1);
error_reporting(E_ALL);

另外,我没有看到您实际上在任何地方调用这些函数。定义函数不会向输出中添加任何内容(即使函数本身有 echo/print 语句)。

【讨论】:

    猜你喜欢
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 2018-03-05
    • 1970-01-01
    • 2022-11-26
    • 2015-11-16
    相关资源
    最近更新 更多