为什么此代码在回显之前返回换行符?

时间:2012-07-02 17:32:33

标签: php line-breaks

所以我有一个我正在使用的登录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";   
    }
}
?>

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:2)

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

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

答案 2 :(得分: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;
?>