当我用httpPost从android设备调用php代码时出错


Error in my php code when i call it from android device with httpPost

我为android设备编写了一个程序,可以将调用日志发送到主机中的数据库。我使用httpPost方法来做这件事,另一边我有一个php代码。

当我尝试在请求中运行此代码时,我得到了一个页面,主要是这样的:

500 Internal Server Error

在CCD_ 2中,我给出了这个错误:

PHP Startup: Suhosin Extension does not officially support PHP 5.2 and below anymore, because it is discontinued. Use it at your own risk. in Unknown on line 0

我的主机提供商不允许我直接更改php.ini文件。

我的一些php代码端:

function callLogSyncToCount()
{
global $serverAddress, $DBusername, $DBpassword, $DBname;
$userID = $_POST['userID'];
$data   = $_POST['data'];
$logs = explode('`second`', $data);
foreach($logs as $item)
{
    //list($number, $duration, $date, $type) = explode("`first`", $item);
    $counter = 0;
    $con = mysql_connect($serverAddress, $DBusername, $DBpassword) or die (mysql_error());
    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
        return false;
    }
    mysql_select_db($DBname, $con);mysql_query("SET NAMES 'utf8'");
    $result = mysql_query("SELECT * FROM `call_log` WHERE `user_id` = '".$userID."' AND `number` = '".$number."' AND `date` = '".$date."' AND `type` = '".$type."'") or die (mysql_error());
    while($row = mysql_fetch_array($result))
    {
        $counter++;
    }
    if ($counter < 1)
    {
        mysql_query("INSERT INTO `call_log`(`user_id`, `date`, `duration`, `number`, `type`) VALUES ('".$userID."', '".$date."', '".$duration."', '".$number."', '".$type."')") or die (mysql_error());
    }
    mysql_close($con) or die (mysql_error());
}
return true;

}

我的一些安卓侧代码是:

private class SyncToCount extends AsyncTask<String, Void, String> {
    @SuppressLint("SimpleDateFormat")
    @Override
    protected String doInBackground(String... param) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(param[0]);
        SimpleDateFormat mySQLFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        mySQLFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
        long ID = Long.parseLong(param[1]);
        List<callLogType> calls = new ArrayList<CallLogUtility.callLogType>();
        calls = getCallList();
        String sendStr = "";
        globalVars.syncLogSize = calls.size();
        try {
            int j = 0;
            for(int i = 0; i < calls.size() && j < 20; i++) 
            {           
                if(calls.get(i).ID <= ID)
                    continue;
                j++;
                sendStr += calls.get(i).PhoneNumber + "`first`";
                sendStr +=Integer.toString(calls.get(i).Duration)+ "`first`";
                Date newDate = new SimpleDateFormat("d MMM yyyy hh:mm:ss z").parse(calls.get(i).Date);
                sendStr +=mySQLFormat.format((newDate))+ "`first`";
                sendStr +=Integer.toString(calls.get(i).Type)+ "`second`";
                globalVars.syncLogUntil+=1;
            }
        } catch (Exception e) {
        }
        try
        {
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("syncType", "calllogtocount"));
            nameValuePairs.add(new BasicNameValuePair("userID", param[1]));
            nameValuePairs.add(new BasicNameValuePair("data", sendStr));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            @SuppressWarnings("unused")
            HttpResponse response = httpclient.execute(httppost);
            BufferedReader in = new BufferedReader
            (new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while ((line = in.readLine()) != null) {
                sb.append(line + NL);
            }
            in.close();
        }
        catch(Exception ex)
        {
        }
        return null;
    }       
}

这段代码不起作用,但我的登录代码在这样的语法下,以及在这个android代码、那个服务器、php和数据库中,都能正常工作。

帮我,我是模棱两可的whit-php和android。。。

我的直觉是,您没有初始化正在使用的变量,这会让事情变得不安。我不是在关注命名变量的来源——我假设这里没有Register_globals(希望没有)。如果你是,这将解释为什么系统在使用中的PHP版本上检查错误,尽管我认为这只是注释行的副作用。

我的直觉是担心未扫描的数据被放入SQL语句中,但这可能会分散注意力。如果可以使用mysql_real_eescape_string(),那么它应该防止(故意的)坏用户数据,因为事实并非如此。我想知道你是否发送了格式错误的数据(比如表单),是否会触发错误(或导致其他错误,如SQL注入)?

您是否使用一个基本表单测试了PHP,该表单可以完全按照您的期望提交数据?这将允许您将输出数据调试到文件或屏幕中,以查看发生了什么,但从这里看,PHP代码需要使用$_POST中的值显式设置这些变量。

在我看来,变量需要填充,但由于列表行已被注释掉,这可能不会发生,这可能会给您留下未初始化的变量,这些变量可能会也可能不会扰乱事情。

除此之外,如果您将foreach中的代码屏蔽掉,它会返回true吗?如果是的话,那么我可能是对的,问题就在里面,所以一点一点地把它拿回来,直到错误出现。如果没有,那么它在外面,你已经从你的查询中删除了一半的代码。

我希望这能给你一些工作机会。最好能找到你不幸的根源。