2012-07-02 101 views
0

所以我有這個登錄PHP腳本,我正在使用,它在一臺服務器上正常工作(返回「成功」||「無效登錄」),然後這個其他服務器它打破,因爲它返回一個換行符,然後「成功」或「無效登錄」這段代碼爲什麼在echo之前返回換行符?

我的猜測是一個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"; 
    } 
} 
?> 
+2

檢查裏面'config.php'和'connect.php',可能會發生在那裏 –

+0

哦right..duh..be回到一秒 – Ronnie

+0

抱歉..等待客戶給我發電子郵件的文件... – Ronnie

回答

3

我敢打賭你connect.php或config.php包含一個\ n(或\ r \ n)在他們的<?php ?>零件之前或之後的可樂。

+1

這的確是原因。而不是可樂,我會給你幾點作爲接受的答案:) – Ronnie

2

這很可能是由於您的包含。你發佈的代碼沒有理由擁有一個,並且沒有我知道要添加的php.ini設置。

發佈您的配置並連接(用戶名/密碼隱藏)爲我們提供幫助。

+0

它看起來像他需要connect.php,否則他的mysql_query函數調用將失敗... –

+0

我指的是'沒有理由'他發佈的代碼將無法正常工作。不是他不需要包括;) – JClaspill

+0

啊,這使得更多的意義:) –

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

這也是一個很好的做法 – Ronnie

相關問題