Ronnie
Ronnie

Reputation: 11198

Why is this code returning a line break before the echo?

So I have this login php script that I am using and it works fine on one server (returns "success" || "invalid login") and then this other server it breaks because it returns a line break and then "success" or "invalid login"

My guess is a php.ini setting. I am just not sure which one.

<?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";   
    }
}
?>

Upvotes: 0

Views: 173

Answers (3)

Bob Davies
Bob Davies

Reputation: 2282

I'd bet you a coke that connect.php or config.php contain a \n (or \r\n) before or after their <?php ?> parts.

Upvotes: 3

Robert
Robert

Reputation: 8767

The code displayed does not indicate the occurrence of a line-break.

On a side note since you are only outputting one value from your booleans then you could initialize a variable to hold the response and then only echo the response once:

<?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;
?>

Upvotes: 1

JClaspill
JClaspill

Reputation: 1745

This is most likely due to your includes. The code you posted has no reason to have one, and there is no php.ini setting that I'm aware of to add such.

Post your config and connect (with username/pw hidden) for us to help further.

Upvotes: 2

Related Questions