Guest User

Untitled

a guest
Oct 8th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.39 KB | None | 0 0
  1. <?php
  2. //Datos BD
  3. $user = "id6152109_admin";
  4. $pass = "admin";
  5. $bd = "id6152109_kiriki";
  6. $server = "localhost";
  7.  
  8. if(isset($_POST['password'])){
  9. $E = $_POST["correo"];
  10. $P = $_POST["password"];
  11.  
  12.  
  13. //Evitar inyección de código
  14. $correo = htmlspecialchars($E);
  15. $password = htmlspecialchars($P);
  16.  
  17. //Encriptamos password
  18. $encrip = password_hash($password, PASSWORD_DEFAULT);
  19.  
  20. //Conexión BD
  21. $mysqli = mysqli_connect($server,$user,$pass,$bd);
  22.  
  23. //Consulta SQL (SELECT). Comprobamos si el email ya existe
  24. $sqlSelect = "SELECT * FROM Usuario WHERE Correo = '".$correo."'" ;
  25. $result = mysqli_query($mysqli, $sqlSelect);
  26.  
  27. //Comprobar resultado
  28. if (mysqli_num_rows($result) == 1) {
  29. $sqlUpdate = "UPDATE Usuario SET Password = '".$encrip."' WHERE Correo = '".$correo."'";
  30.  
  31. $result2 = mysqli_query($mysqli,$sqlUpdate);
  32.  
  33. if(mysqli_connect_errno()==0){
  34. //Consulta correcta
  35. $response["success"] = 1;
  36. $response["message"] = "Update realizado con exito";
  37.  
  38.  
  39. }else{
  40. $response["success"] = 0;
  41. $response["message"] = "Fallo en el update";
  42.  
  43. //Mostramos respuesta JSON
  44. echo json_encode($response);
  45. }
  46.  
  47.  
  48.  
  49. }else if(mysqli_num_rows($result) == 0){
  50. //Consulta incorrecta
  51. $response["success"] = 0;
  52. $response["message"] = "No existe un usuario con ese correo";
  53.  
  54. //Mostramos respuesta JSON
  55. echo json_encode($response);
  56. }else{
  57. //Consulta incorrecta
  58. $response["success"] = 0;
  59. $response["message"] = "Hay mas de un usuario con ese correo.";
  60.  
  61. //Mostramos respuesta JSON
  62. echo json_encode($response);
  63. }
  64. }else{
  65. //Consulta incorrecta
  66. $response["success"] = 0;
  67. $response["message"] = "Error.Campo vacio";
  68.  
  69. //Mostramos respuesta JSON
  70. echo json_encode($response);
  71. }
  72.  
  73.  
  74.  
  75. ?>
  76.  
  77. package com.example.alex.proyecto;
  78.  
  79. public class UpdatePass {
  80.  
  81. /**
  82. * success : 0
  83. * message : Error.Campo vacio
  84. */
  85.  
  86. private int success;
  87. private String message;
  88.  
  89. public int getSuccess() {
  90. return success;
  91. }
  92.  
  93. public void setSuccess(int success) {
  94. this.success = success;
  95. }
  96.  
  97. public String getMessage() {
  98. return message;
  99. }
  100.  
  101. public void setMessage(String message) {
  102. this.message = message;
  103. }
  104.  
  105. package com.example.alex.proyecto;
  106.  
  107. import android.app.AlertDialog;
  108. import android.content.Context;
  109. import android.content.DialogInterface;
  110. import android.content.Intent;
  111. import android.support.v7.app.AppCompatActivity;
  112. import android.os.Bundle;
  113. import android.util.Log;
  114. import android.view.LayoutInflater;
  115. import android.view.View;
  116. import android.widget.Button;
  117. import android.widget.EditText;
  118. import android.widget.Toast;
  119.  
  120. import com.google.gson.Gson;
  121. import com.loopj.android.http.AsyncHttpClient;
  122. import com.loopj.android.http.JsonHttpResponseHandler;
  123. import com.loopj.android.http.RequestParams;
  124.  
  125. import org.json.JSONObject;
  126.  
  127. import cz.msebera.android.httpclient.Header;
  128.  
  129. public class CambioPassActivity extends AppCompatActivity {
  130. EditText pass,pass2;
  131. Button bCambiar;
  132. String correoN;
  133. String url = "ftp://proyectokiriki@files.000webhost.com/public_html/cambiarPass.php";
  134.  
  135. @Override
  136. protected void onCreate(Bundle savedInstanceState) {
  137. super.onCreate(savedInstanceState);
  138. setContentView(R.layout.pantalla_cambiopass);
  139.  
  140.  
  141. pass = (EditText) findViewById(R.id.textoPass);
  142. pass2 = (EditText) findViewById(R.id.textoPass2);
  143. bCambiar = (Button) findViewById(R.id.botonCambioPass);
  144. correoN = getIntent().getStringExtra("correo");
  145.  
  146. bCambiar.setOnClickListener(new View.OnClickListener() {
  147. @Override
  148. public void onClick(View view) {
  149. Toast.makeText(getApplicationContext(), correoN, Toast.LENGTH_LONG).show();
  150.  
  151. if(pass.getText().toString().trim().equalsIgnoreCase("")||pass2.getText().toString().trim().equalsIgnoreCase("")){
  152. LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  153. final View formElementsView = inflater.inflate(R.layout.pantalla_jugadores,
  154. null, false);
  155.  
  156. AlertDialog.Builder builder = new AlertDialog.Builder(CambioPassActivity.this);
  157. builder.setTitle("Deben estar todos los campos rellenos.");
  158. builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  159.  
  160. public void onClick(DialogInterface dialog, int id) {
  161. dialog.dismiss();
  162. }
  163. });
  164. builder.setView(formElementsView);
  165. final AlertDialog dialog = builder.create();
  166. dialog.show();
  167. }else{
  168.  
  169. if(pass.getText().toString().trim().equalsIgnoreCase(pass2.getText().toString().trim())==true) {
  170. AsyncHttpClient cliente = new AsyncHttpClient();
  171. RequestParams rp = new RequestParams();
  172. rp.put("correo", correoN);
  173. rp.put("password", pass.getText().toString().trim());
  174.  
  175. cliente.post(url, rp, new JsonHttpResponseHandler() {
  176. @Override
  177. public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
  178.  
  179. if (statusCode == 200) {
  180.  
  181. String respuesta = response.toString();
  182. Gson gson = new Gson();
  183.  
  184. UpdatePass update = gson.fromJson(respuesta, UpdatePass.class);
  185.  
  186. String mensaje = update.getMessage();
  187. Toast.makeText(getApplicationContext(), mensaje, Toast.LENGTH_LONG).show();
  188. int res = update.getSuccess();
  189.  
  190. //Si es correcta
  191. if (res == 1) {
  192. Toast.makeText(getApplicationContext(), "Contraseña cambiada", Toast.LENGTH_LONG).show();
  193. //Una vez iniciada la sesión, nos vamos a la pantalla principal
  194.  
  195. Intent Principal = new Intent(getApplicationContext(), MainActivity.class);
  196. finish();
  197. startActivity(Principal);
  198.  
  199. }
  200. }
  201. }
  202. });
  203. }
  204. }
  205. }
  206. });
  207.  
  208. }
  209. }
Add Comment
Please, Sign In to add comment