我有一个登录脚本,应该分别返回"成功"或"失败",但它在结果之前添加了很多空格,在控制台中它将值显示为"<吨空间>成功"。这是登录脚本的 PHP:吨空间>
public function login() {
global $dbc, $layout;
if(!isset($_SESSION['uid'])){
if(isset($_POST['submit'])){
$username = mysqli_real_escape_string($dbc, trim($_POST['email']));
$password = mysqli_real_escape_string($dbc, trim($_POST['password']));
if(!empty($username) && !empty($password)){
$query = "SELECT uid, email, username, password, hash FROM users WHERE email = '$username' AND password = SHA('$password') AND activated = '1'";
$data = mysqli_query($dbc, $query);
if((mysqli_num_rows($data) === 1)){
$row = mysqli_fetch_array($data);
$_SESSION['uid'] = $row['uid'];
$_SESSION['username'] = $row['username'];
$_SERVER['REMOTE_ADDR'] = isset($_SERVER["HTTP_CF_CONNECTING_IP"]) ? $_SERVER["HTTP_CF_CONNECTING_IP"] : $_SERVER["REMOTE_ADDR"];
$ip = $_SERVER['REMOTE_ADDR'];
$user = $row['uid'];
$query = "UPDATE users SET ip = '$ip' WHERE uid = '$user' ";
mysqli_query($dbc, $query);
setcookie("ID", $row['uid'], time()+3600*24);
setcookie("IP", $ip, time()+3600*24);
setcookie("HASH", $row['hash'], time()+3600*24);
echo 'success';
exit();
} else {
//$error = '<div class="shadowbar">It seems we have run into a problem... Either your username or password are incorrect or you haven''t activated your account yet.</div>' ;
//return $error;
$err = 'failure';
echo($err);
exit();
}
} else {
//$error = '<div class="shadowbar">You must enter both your username AND password.</div>';
//return $error;
$err = "{'"result'":'"failure'"}";
echo json_encode($err);
exit();
}
}
} else {
echo '{"result":"success"}';
exit();
}
return $error;
}
以及表单和 JS
<div class="shadowbar"><form id="login" method="post" action="/doLogin">
<div id="alert"></div>
<fieldset>
<legend>Log In</legend>
<div class="input-group">
<span class="input-group-addon">E-Mail</span>
<input type="email" class="form-control" name="email" value="" /><br />
</div>
<div class="input-group">
<span class="input-group-addon">Password</span>
<input type="password" class="form-control" name="password" />
</div>
</fieldset>
<input type="submit" class="btn btn-primary" value="Log In" name="submit" />
</form></div>
$(function login() {
$("#login").validate({ // initialize the plugin
// any other options,
onkeyup: false,
rules: {
email: {
required: true,
email: true
},
password: {
required: true
}
}
});
$('form').ajaxForm({
beforeSend: function() {
return $("#login").valid();
},
success : function(result) {
console.log(result);
if(result == " success"){
window.location = "/index.php";
}else if(result == " failure"){
$("#alert").html("<div class='alert alert-warning'>Either you're username or password are incorrect, or you've not activated your account.</div>");
//$("#alert").show();
}
}
});
});
但是由于某种原因,结果总是有很多空间。我是 JS 的新手,所以如果这很常见,我还不知道。
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
define("CCore", true);
session_start();
//Load files...
require_once('include/scripts/settings.php');
require_once('include/scripts/version.php');
require('include/scripts/core.class.php');
require('include/scripts/nbbc_main.php');
$parser = new BBCode;
$core = new core;
$admin = new admin;
require_once('include/scripts/layout.php');
require_once('include/scripts/page.php');
//Set Variables...
global $dbc, $parser, $layout, $main, $settings, $core;
$page = new pageGeneration;
$page->Generate();
?>
这是我的索引,在生成页面并调用 login() 之前的任何内容都在那里。
我想
你正在使用Ajax调用。我遇到了同样的问题,但我的情况是结果不包含空格,它以新行返回。问题是我的脚本是Ajax请求的,在PHP脚本之前包含"换行符"。在脚本文件中搜索以 <?php //code...
开头的 PHP 脚本之前的空格 如果您在返回成功说明的脚本中包含了一些脚本,请同时搜索它们。
我不知道
这是否重要,但你的
if(result == " success"){ // <<<<<< Here is a Problem maybe
window.location = "/index.php";
}else if(result == " failure"){ // <<<<<< Here is a Problem maybe
$("#alert").html("<div class='alert alert-warning'>Either you're username or password are incorrect, or you've not activated your account.</div>");
//$("#alert").show();
}
比较来自服务器的结果,即"成功"与"成功"。空间太大了。
编辑::我不明白为什么你在响应格式之间跳跃。有时你会回显"成功",这在你的 if 条件中是简单而好的,但有时你会返回 json 编码字符串。这些响应不能只与纯文本进行比较。您必须将这些响应解析为 JSON 对象。然后你可以与:
if (parsedJSONobject.result == "success"){}
关于这个问题的评论很可能是正确的:空格正在被 PHP 回应(同样,可能,如果不阅读整个源代码,没有人可以确定)包含在此之前。例如,如果您这样做:
<?php
// there's a space before the previous line
你会在输出中获得那个空间。
你可以做的是有点黑客,你包含一个标题,例如:
header('Content-Type: text/html');
在成功输出之前,这将(可能再次)输出如下内容:
Warning: Cannot modify header information - headers already sent by (output started at /some/file.php:12) in /some/file.php on line 23
(注意"输出开始"部分),现在您知道从哪里开始寻找了。
呵呵。