2

这个 php 代码是通过 ajax 从 javascript 调用的,应该返回一个数字代码和一条消息。我可以从 PHP 脚本返回一个数组,但是当我读取 ajax 响应时,响应包含的不仅仅是返回的数组,它还有其他回显语句。有没有办法从仅返回数组的 Php 发送信息?我只想要 json 对象{"return_code":0,"return_msg":"Login successful."}

这是通过 ajax 返回到 javascript 的信息alert(login_info);

Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the

date_default_timezone_set() 函数。如果您使用了这些方法中的任何一种,但仍然收到此警告,您很可能拼错了时区标识符。我们在第 60 行的 /Library/WebServer/Documents/Pagelinks_Dev/login_user.php 中为 'EDT/-4.0/DST' 选择了 'America/New_York' {"return_code":0,"return_msg":"登录成功。"}

这是ajax调用。

var message = $.ajax({
    url: "login_user.php",
    type: "POST",
    data: { username_email: username_email, upass: user_passwd },
    cache: false,
    async: false,
    success: function (login_info) {
        if (login_info != '') 
        {
          alert(login_info);
        }
    },
    error: function (request, status, error) {
        alert ("status "+status+" error "+error+"responseText "+request.responseText);
    },
}).responseText;

PHP 脚本

<?php include("dbconnect.php"); ?>

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
session_start();

function login_user( $username_email, $password_text ) {
    global $dbco;

    $password = md5($password_text);
    $privPost = 1; // 3: max.
    $privSedit = 0;

    $messages = array( 'log_no_un_em' => 'Enter username or email.',
            'log_no_pw' => 'Enter password.',
            'log_un_em_pw_incorrect' => 'Incorrect login info.',
            'log_success' => 'Login successful.'
    );

    $return = array();

    $l_un_em = isset($username_email) ? $username_email : '';
    $l_pword = isset($password_text) ? $password_text : '';

    if (!$l_un_em) {
        /* echo $messages['log_no_un_em'];
        return; */
        $return['return_code'] = -1;
        $return['return_msg'] = $messages['log_no_un_em'];
        echo json_encode($return);
    }
    if (!$l_pword) {
        /* echo $messages['log_no_pw'];
        return; */
        $return['return_code'] = -1;
        $return['return_msg'] = $messages['log_no_pw'];
        echo json_encode($return);
    }

    mysql_select_db("luxcal", $dbco);

    while (true) {
        $md5_pw = md5($password_text);
        $r_getuser = mysql_query("SELECT * FROM users WHERE (user_name = BINARY '".mysql_real_escape_string($l_un_em)."' OR email = '".mysql_real_escape_string($l_un_em)."') AND (password = '$md5_pw' OR temp_password = '$md5_pw') AND status >= 0");
        if (mysql_num_rows($r_getuser) == 0) {
            /* echo $messages['log_un_em_pw_incorrect'];
            return; */
            $return['return_code'] = -1;
            $return['return_msg'] = $messages['log_un_em_pw_incorrect'];
            echo json_encode($return);
        }
        $row = mysql_fetch_assoc($r_getuser);
        /* if ($row['privs'] < 1) { echo $messages['log_no_privs']; break; } */
        if ($row['temp_password'] == $md5_pw) { //new password
            mysql_query("UPDATE users SET password = '".$md5_pw."', temp_password = NULL WHERE user_id = '{$row['user_id']}'");
        }
        $today = date('Y-m-d');
        if ($row['login_0'][0] == '9') { //first login
            mysql_query("UPDATE users SET login_0 = '".$today."', login_1 = '".$today."', login_cnt = 1 WHERE user_id = '{$row['user_id']}'");
        } else {
            mysql_query("UPDATE users SET login_1 = '".$today."', login_cnt = login_cnt+1 WHERE user_id = '{$row['user_id']}'");
        }
        $_SESSION['uid'] = $row['user_id'];
        $_SESSION['unm'] = stripslashes($row['user_name']);
        $_SESSION['uml'] = stripslashes($row['email']);
        $_SESSION['cL'] = $row['language'];
        /* echo '<meta http-equiv="refresh" content="0;url=livemass_CENTER34.php">'; */ //default page
        break;
    }

    $return['return_code'] = 0;
    $return['return_msg'] = $messages['log_success'];

    echo json_encode($return);
    /* return; */
}

echo login_user(trim($_REQUEST['username_email']), trim($_REQUEST['upass']));

?>
4

2 回答 2

2

在你的 php 文件中使用

 date_default_timezone_set('your time zone');

告诉 php 你的时区是什么。有关更多详细信息,请参阅date_default_timezone_set()

于 2012-04-21T19:32:55.940 回答
0

如果要返回 json,则不能使用 json 字符串输出任何其他文本。更改ini_set('display_errors', 1);ini_set('display_errors', 0);

于 2012-04-21T19:21:04.827 回答