[英]I am developing an android app for job search .here i am using PHP as back end
in php code when we register automatically create a random code as user id. 在php代码中,当我们注册时会自动创建一个随机代码作为用户ID。 in android app when we save profile information i cant the user id will different.
在android应用中,当我们保存个人资料信息时,我无法确保用户ID会有所不同。 so how to manage user id in all activities and fragment keep same user id until logout help me............................................... **
因此如何在所有活动中管理用户ID,并在退出前帮助片段保持相同的用户ID。 ................ **
Sign up code: 注册代码:
<?php
session_start();
if($_SERVER['REQUEST_METHOD']=='POST'){
include_once("db_connect.php");
$username = $_POST['user_name'];
$useremail = $_POST['user_email'];
$usermobile = $_POST['user_mobile'];
$password = $_POST['password'];
function randomstring($len) {
$string = "";
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for($i=0;$i<$len;$i++)
$string.=substr($chars,rand(0,strlen($chars)),1);
return $string;
}
$rndm_code=randomstring(5);
$CheckSQL = "SELECT * FROM user WHERE user_email='$useremail'";
$check = mysqli_fetch_array(mysqli_query($con,$CheckSQL));
if(isset($check)){
echo 'Email Already Exist';
}
else{
$Sql_Query = "insert into user (user_id,user_name,user_email,user_mobile,password) values ('$rndm_code','$username','$useremail','$usermobile','$password')";
if (mysqli_query($con,$Sql_Query)){
echo 'User Registration Successfully';
}
else
{
echo 'Try Again';
}
}
}
mysqli_close($con);
?>
Android code: Android代码:
private void registerUser() {
isConnectingToInternet();
final String username = signupname.getText().toString().trim();
final String email = signupemail.getText().toString().trim();
final String phone = signupphone.getText().toString().trim();
final String password = signuppassword.getText().toString().trim();
pDialog = new ProgressDialog(SignupActivity.this);
pDialog.setMessage("Signing Up.. Please wait...");
pDialog.setCancelable(false);
pDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.URL_REGISTER,
new Response.Listener<String>() {
@Override
public void onResponse(String ServerResponse) {
// Hiding the progress dialog after all task complete.
pDialog.dismiss();
// Showing response message coming from server.
Toast.makeText(SignupActivity.this, ServerResponse, Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
// Hiding the progress dialog after all task complete.
pDialog.dismiss();
// Showing error message if something goes wrong.
Toast.makeText(SignupActivity.this,"Check Internet connection", Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
// Creating Map String Params.
Map<String, String> params = new HashMap<String, String>();
// Adding All values to Params.
params.put("user_name",username);
params.put("user_email",email);
params.put("user_mobile", phone);
params.put("password",password);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(SignupActivity.this);
requestQueue.add(stringRequest);
}**
Your server response needs to contain the id in addition to the response text. 您的服务器响应除了响应文本外还需要包含ID。 Usually, this is done by formatting the response as JSON.
通常,这是通过将响应格式设置为JSON来完成的。
Instead of just "User Registration Successfully", the response from the PHP script could be 不仅仅是“用户注册成功”,PHP脚本的响应可能是
{
"message":"User Registration Successfully",
"userId":<your_user_id_goes_here>
}
You can use json_encode in PHP to create this format for you from an array. 您可以在PHP中使用json_encode从数组中为您创建此格式。 The code for that would look something like this
该代码看起来像这样
echo json_encode([
"message" => "User Registration Successfully",
"userId" => $rndm_code
]);
exit();
It would be good to exit after sending the response so that your JSON deos not becoem invalid by you accidentally echoing something more somewhere later. 最好在发送响应后退出,这样您的JSON deos不会因为以后意外在其他地方回显某些内容而无效。
In the android app, you would then get the data from the JSON - I am not experienced in android, but I think you would do this by making it a JsonObjectRequest instead of a StringRequest, and then displaying serverResponse.getString("message") and saving serverResponse.getString("userId") as the id in a variable somewhere. 在android应用中,您将随后从JSON获取数据-我在android中没有经验,但是我认为您可以通过将其设置为JsonObjectRequest而不是StringRequest,然后显示serverResponse.getString(“ message”)来实现。并将serverResponse.getString(“ userId”)作为ID保存在某个地方的变量中。
<?php
session_start();
if($_SERVER['REQUEST_METHOD']=='POST'){
include_once("db_connect.php");
$username = $_POST['user_name'];
$useremail = $_POST['user_email'];
$usermobile = $_POST['user_mobile'];
$password = $_POST['password'];
function randomstring($len) {
$string = "";
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for($i=0;$i<$len;$i++)
$string.=substr($chars,rand(0,strlen($chars)),1);
return $string;
}
$rndm_code=randomstring(5);
$checkUser = "SELECT * FROM user WHERE user_email='$useremail' OR user_mobile='$usermobile'";
$userStatus = mysqli_fetch_array(mysqli_query($con,$checkUser));
if(isset($userStatus)){
die(json_encode(array("success"=>0,"message"=>"Email or Mobile no already exist!!")));
}
else
{
$sql =$con->prepare( "INSERT INTO user(user_id,user_name,user_email,user_mobile,password) VALUES ('$rndm_code','$username', '$useremail', '$usermobile','$password')");
$sql->bind_param("ssss",$rndm_code, $username, $useremail, $usermobile,$password);
if($sql->execute())
{
echo(json_encode(array("success"=>1,"message"=>"Account created successfully!! Please Login")));
}
else
{
die("Something Error".$sql."<br>".mysqli_error($con));
}
}
}else{
die(json_encode(array("success"=>0,"message"=>"Empty Request Parameters..!!")));
}
mysqli_close($con);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.