0

所以我有这个我正在使用的登录 php 脚本,它在一个服务器上运行良好(返回“成功”||“无效登录”),然后另一个服务器中断,因为它返回一个换行符,然后是“成功”或“登录无效”

我的猜测是 php.ini 设置。我只是不确定是哪一个。

<?php
include("../config.php");
include("../connect.php");

$adminCheck = mysql_query("SELECT * FROM admins WHERE username = '" . mysql_real_escape_string($_POST['username']) . "' AND password = '" .  mysql_real_escape_string($_POST['password']) . "'");
if (mysql_num_rows($adminCheck) == 1)
{
    $result = mysql_fetch_array($adminCheck);
    $_SESSION['user']['level']    = "admin";
    $_SESSION['user']['userid']   = $result['id'];
    $_SESSION['user']['username'] = $result['username'];
    echo "success"; 
}
else
{
    $clientCheck = mysql_query("SELECT * FROM clients WHERE username = '" . mysql_real_escape_string($_POST['username']) . "' AND password = '" .  mysql_real_escape_string($_POST['password']) . "'");
    if (mysql_num_rows($clientCheck) == 1)
    {
        $result = mysql_fetch_array($clientCheck);
        $_SESSION['user']['level']      = "client";
        $_SESSION['user']['userid']     = $result['id'];
        $_SESSION['user']['username']   = $result['username'];
        $_SESSION['user']['client']     = $result['client'];
        echo "success";
    }
    else
    {
        echo "invalid login";   
    }
}
?>
4

3 回答 3

3

我敢打赌,connect.php 或 config.php 在它们的<?php ?>部分之前或之后包含一个 \n(或 \r\n)。

于 2012-07-02T17:37:58.837 回答
2

这很可能是由于您的包含。您发布的代码没有理由拥有一个,并且我知道没有 php.ini 设置可以添加这样的设置。

发布您的配置并连接(隐藏用户名/密码)以供我们进一步帮助。

于 2012-07-02T17:35:50.413 回答
1

显示的代码不表示出现换行符。

附带说明一下,由于您只从布尔值中输出一个值,因此您可以初始化一个变量来保存响应,然后只回显一次响应:

<?php
include("../config.php");
include("../connect.php");

$response = 'success';

$adminCheck = mysql_query("SELECT * FROM admins WHERE username = '" . mysql_real_escape_string($_POST['username']) . "' AND password = '" .  mysql_real_escape_string($_POST['password']) . "'");
if (mysql_num_rows($adminCheck) == 1)
{
    $result = mysql_fetch_array($adminCheck);
    $_SESSION['user']['level']    = "admin";
    $_SESSION['user']['userid']   = $result['id'];
    $_SESSION['user']['username'] = $result['username'];
}
else
{
    $clientCheck = mysql_query("SELECT * FROM clients WHERE username = '" . mysql_real_escape_string($_POST['username']) . "' AND password = '" .  mysql_real_escape_string($_POST['password']) . "'");
    if (mysql_num_rows($clientCheck) == 1)
    {
        $result = mysql_fetch_array($clientCheck);
        $_SESSION['user']['level']      = "client";
        $_SESSION['user']['userid']     = $result['id'];
        $_SESSION['user']['username']   = $result['username'];
        $_SESSION['user']['client']     = $result['client'];
    }
    else
    {
        $response = "invalid login";   
    }
}

echo $response;
?>
于 2012-07-02T17:39:30.173 回答