![](/img/trans.png)
[英]I am developing website using php and mysql.my each page file extension end with .php extension how do i avoid that?
[英]I am developing an android app for job search .here i am using PHP as back end
在php代碼中,當我們注冊時會自動創建一個隨機代碼作為用戶ID。 在android應用中,當我們保存個人資料信息時,我無法確保用戶ID會有所不同。 因此如何在所有活動中管理用戶ID,並在退出前幫助片段保持相同的用戶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);
$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代碼:
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);
}**
您的服務器響應除了響應文本外還需要包含ID。 通常,這是通過將響應格式設置為JSON來完成的。
不僅僅是“用戶注冊成功”,PHP腳本的響應可能是
{
"message":"User Registration Successfully",
"userId":<your_user_id_goes_here>
}
您可以在PHP中使用json_encode從數組中為您創建此格式。 該代碼看起來像這樣
echo json_encode([
"message" => "User Registration Successfully",
"userId" => $rndm_code
]);
exit();
最好在發送響應后退出,這樣您的JSON deos不會因為以后意外在其他地方回顯某些內容而無效。
在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.