PHP函数带有返回码和返回消息以供参考

时间:2012-04-21 19:17:24

标签: php javascript ajax json

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

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

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()函数。如果您使用其中任何一个   方法,你最有可能仍然得到这个警告   拼写错误的时区标识符。我们选择了“America / New_York”   'EDT / -4.0 / DST'代替   第60行/Library/WebServer/Documents/Pagelinks_Dev/login_user.php       {“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']));

?>

2 个答案:

答案 0 :(得分:2)

在你的php文件中使用

 date_default_timezone_set('your time zone');

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

答案 1 :(得分:0)

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