在PHP脚本奇怪的行为 [英] Strange Behaviour in PHP script
问题描述
我在一个问题,任何一个可以帮我请。
我有我从我的Java类文件的调用AsyncTask的PHP文件。
在AsyncTask的我发送三个变量电子邮件,密码和PIN码。
这是怎么回事,就是当我跑我硬codeD PHP的值,它给了我正确的结果。
结果是:
里面第一中频
内部如果第二
验证
如果主
但闻一试图通过$ C $运行我的PHP c那么它给了我错误的结果。
结果是:
里面第一中频
无效
如果主
我'无法理解为什么会出现这种情况,请指引我。
我的PHP文件
< PHP需要'DbConnect.php'; $ I = 1;
$密码= $ _ POST [密码];
$电子邮件= $ _ POST [电子邮件];
$针= $ _ POST [针];
// $关键code = $ _REQUEST [钥匙];
如果((使用isset($ _ POST [密码]))及及(使用isset($ _ POST [电子邮件]))及及(使用isset($ _ POST [针)))
{
$ query4 =(选择seller_id,姓名,电子邮件,密码,verification_Pin,verification_ code,CREATED_DATE FROM`seller` WHERE电子邮件= $电子邮件');
$ query_run =的mysql_query($ query4);
$行= mysql_fetch_row($ query_run); $ INT = $行[0];
$则strName = $行[1];
$ strEmail = $行[2];
$ strPwd = $行[3];
$ strPin = $行[4]; 回声$密码;
回声$密码;
回声$电子邮件;
回声$ INT;
回声$ strEmail;
回声$ strPwd;
回声$ strPin;
如果(($电子邮件== $ strEmail)及及($密码== $ strPwd)及及($针== $ strPin))
{
全球$我;
$ I = 2;
$ ID = updateValidation($ INT);
回声$ ID;
如果($ ID == 1)
{
回声已验证;
}
其他
{
回声未验证;
}
}
其他
{
回声Invaild
}
}
其他
{
回声值未设置;
}功能updateValidation($ SID)
{
全球$我;
如果($ I == 2)
{
回声里更新验证;
$ queryUpdate =(更新`seller` SET verification_Pin = 0,verification_ code ='验证',CREATED_DATE = CURDATE(),其中seller_id = $ SID'); 如果(的mysql_query($ queryUpdate))
{
返回1;
}
其他
{
返回2;
}
}
其他
{
回声我不是2;
}
}?>
我的.class文件:
按钮OK =(按钮)myDialog
.findViewById(R.id.button1);
et_pin =(EditText上)myDialog
.findViewById(R.id.editText1);
ok.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
Toast.makeText(getApplicationContext(),
单击确定,Toast.LENGTH_LONG).show();
引脚= et_pin.getText()的toString()。
Toast.makeText(
getApplicationContext(),
电子邮件,传递,引脚=+ STR1 +,+ STR2
+,+引脚,Toast.LENGTH_LONG)
。显示();
新App_pin_Task()执行(FILENAME_pin);
//意向意图=新
//意图(Dealer_details.this,
// Login.class);
// startActivity(意向);
}
});公共类App_pin_Task扩展的AsyncTask<字符串,整数,字符串> { @覆盖
在preExecute保护无效(){
super.on preExecute(); } @燮pressLint(NewApi)
@覆盖
保护无效onPostExecute(字符串结果){
super.onPostExecute(结果); Toast.makeText(getApplicationContext(),
内部App_pin_Task后执行(结果)=+结果,
Toast.LENGTH_LONG).show(); 如果(result.contains(无效)){
et_pin.setText();
}其他{
意图myIntent =新意图(Login.this,UserActivity.class);
startActivity(myIntent);
} } @覆盖
保护无效onProgressUpdate(整数...值){
// TODO自动生成方法存根
super.onProgressUpdate(值);
} @覆盖
保护字符串doInBackground(字符串... PARAMS){
// String为= NULL; HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httppost =新HttpPost(
http://animsinc.com/verifyEmail.php);
尝试{
清单<&的NameValuePair GT; namevaluepairs中=新的ArrayList<&的NameValuePair GT;(
4);
nameValuePairs.add(新BasicNameValuePair(电子邮件,STR1));
nameValuePairs.add(新BasicNameValuePair(密码,STR2));
nameValuePairs.add(新BasicNameValuePair(针,针));
httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
httpclient.execute(httppost); HTT presponse响应= httpclient.execute(httppost);
HttpEntity实体= response.getEntity();
是= EntityUtils.toString(实体); }赶上(ClientProtocolException E){
// TODO自动生成catch块
}赶上(UnsupportedEncodingException五){
e.printStackTrace();
}赶上(IOException异常五){
// TODO自动生成catch块
} 回报;
}}
补充从fayeq阿里汗的答案我也做了以下内容:
首先后期使用和安全添加HTML特殊字符。
用htmlspecialchars =>转换特殊字符为HTML实体
块引用>$密码=用htmlspecialchars($ _ POST ['密码'],ENT_QUOTES);
$电子邮件=用htmlspecialchars($ _ POST [电子邮件],ENT_QUOTES);
$ PIN =用htmlspecialchars($ _ POST ['脚'],ENT_QUOTES);
$关键code =用htmlspecialchars($ _ POST ['关键'],ENT_QUOTES);也在你的android活动,并在发送字符串之前,这将是一个好主意,修剪值,以确保您不会发射空字符也可以搞砸与PHP
nameValuePairs.add(新BasicNameValuePair(电子邮件,str1.trim()));
nameValuePairs.add(新BasicNameValuePair(密码,str2.trim()));
nameValuePairs.add(新BasicNameValuePair(针,pin.trim()));我希望它帮助你的东西。
I am in a problem can any one help me please.
I have a php file which i am calling from my java class file's Asynctask. In the Asynctask i am sending three variables email,password and pin.
What is happening, is that when i run my php with hardcoded values it gives me proper result.
RESULT IS:
Inside 1st if Inside 2nd if Verified main if
But wen i try running my php through the code it gives me wrong result
RESULT IS:
Inside 1st if Invalid main if
I'am not able to understand why is this happening please Guide me.
My PHP File
<?php require 'DbConnect.php'; $i=1; $Password = $_POST["password"]; $Email = $_POST["email"]; $Pin = $_POST["pin"]; //$KeyCode = $_REQUEST["key"]; if((isset($_POST["password"])) && (isset($_POST["email"])) && (isset($_POST["pin"]))) { $query4 = ("SELECT seller_id, name, email, password, verification_Pin, verification_Code, created_Date FROM `seller` WHERE email = '$Email'"); $query_run = mysql_query($query4); $row=mysql_fetch_row($query_run); $int=$row[0]; $strName=$row[1]; $strEmail=$row[2]; $strPwd=$row[3]; $strPin=$row[4]; echo $Pin; echo $Password; echo $Email; echo $int; echo $strEmail; echo $strPwd; echo $strPin; if(($Email==$strEmail) && ($Password==$strPwd) && ($Pin==$strPin)) { global $i; $i=2; $id=updateValidation($int); echo $id; if($id==1) { echo "Verified"; } else { echo "Not Verified"; } } else { echo "Invaild"; } } else { echo "Values not set"; } function updateValidation($sid) { global $i; if($i==2) { echo "Inside Update vAlidation"; $queryUpdate = ("UPDATE `seller` SET verification_Pin = 0, verification_Code = 'Verified', created_Date = CURDATE() where seller_id='$sid'"); if(mysql_query($queryUpdate)) { return 1; } else { return 2; } } else { echo "i not 2"; } } ?>
My Class file:
Button ok = (Button) myDialog .findViewById(R.id.button1); et_pin = (EditText) myDialog .findViewById(R.id.editText1); ok.setOnClickListener(new OnClickListener() { public void onClick(View v) { Toast.makeText(getApplicationContext(), "CLICKED OK", Toast.LENGTH_LONG).show(); pin = et_pin.getText().toString(); Toast.makeText( getApplicationContext(), "email,pass,pin= " + str1 + "," + str2 + "," + pin, Toast.LENGTH_LONG) .show(); new App_pin_Task().execute(FILENAME_pin); // Intent intent = new // Intent(Dealer_details.this, // Login.class); // startActivity(intent); } }); public class App_pin_Task extends AsyncTask<String, Integer, String> { @Override protected void onPreExecute() { super.onPreExecute(); } @SuppressLint("NewApi") @Override protected void onPostExecute(String result) { super.onPostExecute(result); Toast.makeText(getApplicationContext(), "Inside App_pin_Task post Execute(Result)=" + result, Toast.LENGTH_LONG).show(); if (result.contains("Invalid")) { et_pin.setText(""); } else { Intent myIntent = new Intent(Login.this, UserActivity.class); startActivity(myIntent); } } @Override protected void onProgressUpdate(Integer... values) { // TODO Auto-generated method stub super.onProgressUpdate(values); } @Override protected String doInBackground(String... params) { // String is = null; HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost( "http://animsinc.com/verifyEmail.php"); try { List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>( 4); nameValuePairs.add(new BasicNameValuePair("email", str1)); nameValuePairs.add(new BasicNameValuePair("password", str2)); nameValuePairs.add(new BasicNameValuePair("pin", pin)); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); httpclient.execute(httppost); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = EntityUtils.toString(entity); } catch (ClientProtocolException e) { // TODO Auto-generated catch block } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block } return is; } }
解决方案Complementing the answer from fayeq-ali-khan I would also do the following;
First use post and add html special character for security.
htmlspecialchars => Convert special characters to HTML entities
$Password = htmlspecialchars($_POST['password'],ENT_QUOTES); $Email = htmlspecialchars($_POST['email'],ENT_QUOTES); $Pin = htmlspecialchars($_POST['pin'],ENT_QUOTES); $KeyCode = htmlspecialchars($_POST['key'],ENT_QUOTES);
Also on your android activity and before you send the string it would be a good idea to trim the value to make sure that you are not transmitting empty character that can also mess up with the PHP
nameValuePairs.add(new BasicNameValuePair("email", str1.trim())); nameValuePairs.add(new BasicNameValuePair("password", str2.trim())); nameValuePairs.add(new BasicNameValuePair("pin", pin.trim()));
I hope it help you with something
这篇关于在PHP脚本奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!