Advertisement
Guest User

Untitled

a guest
Apr 16th, 2017
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.56 KB | None | 0 0
  1. <?php
  2.  
  3.     /*
  4.       This login.php script was inspired by https://www.w3schools.com/php/php_mysql_connect.asp
  5.     */
  6.  
  7.     //Define and initialize variables to connect to database server
  8.     $servername = "localhost";
  9.     $username = "id1148117_xirokx";
  10.     $password = "project";
  11.     $dbName = "id1148117_login";
  12.    
  13.     //Create database connection
  14.     $con = mysqli_connect($servername, $username, $password, $dbName);
  15.    
  16.     //check connection - If database connection unsuccessful display error = connection failed
  17.     if ($con->connect_error) {
  18.         die("Connection failed: " . $con->connect_error);
  19.     }
  20.        
  21.     //check username and password form values are not empty and send (POST) them to database server to verify they exist.
  22.     if(isset($_POST["username"]) && isset($_POST["password"])) {
  23.         $username = $_POST["username"];
  24.         $password = $_POST["password"];
  25.    
  26.         //using a prepared sql statement to minimise the risk of SQL injection attack
  27.         //a prepared SQL "SELECT" query template is used to be executed on my database table
  28.         //this query is executed on the "user" table and requests all the users details which match the
  29.         //the username and password provided by the user in the android application.
  30.         $statement = mysqli_prepare($con, "SELECT user_id,name,username,age,password FROM user WHERE username = ? AND password = ?");
  31.    
  32.         //attaches ("bind's") my form variables to my SQL query
  33.         //"ss" defines the data types for both my variables - username and password are both Strings hence "ss"
  34.         mysqli_stmt_bind_param($statement, "ss", $username, $password);
  35.  
  36.         //runs the SQL query on my database table
  37.         mysqli_stmt_execute($statement);
  38.    
  39.         //stores the result of my prepared SQL query to a variable
  40.         mysqli_stmt_store_result($statement);
  41.        
  42.         //defines each variable to attach the returned result of my prepared SQL SELECT query
  43.         mysqli_stmt_bind_result($statement, $user_id, $name, $username, $age, $password);
  44.    
  45.         //creates an array to store results of the prepared SQL query
  46.         $response = array();
  47.        
  48.         //initialises the value of "success" variable to false
  49.         $response["success"] = false;  
  50.    
  51.         //the SELECT SQL query above returns the values from the database table for that user and stores them in pre-defined variables
  52.         //a while loop is used here to iterate through the returned result set of my above prepared SELECT SQL query
  53.         //these returned values are stored in my array, called "response" and then sent back to my android application to use as required.
  54.         while(mysqli_stmt_fetch($statement)){
  55.             //return true because the SQL query successfully returned data
  56.             $response["success"] = true;  
  57.             //the user id returned from the SQL query
  58.             $response["user_id"] = $user_id;
  59.             //the name returned from the SQL query
  60.             $response["name"] = $name;
  61.             //the username returned from the SQL query
  62.             $response["username"] = $username;
  63.             //the age returned from the SQL query
  64.             $response["age"] = $age;
  65.             //the password returned from the SQL query
  66.             $response["password"] = $password;
  67.         }
  68.    
  69.         //translates Php response into JSON Object so my mobile application can work
  70.         //with the returned String and continue with processing.
  71.         echo json_encode($response);
  72.        
  73.     }
  74.     //if user did not provide values on my login form then return this error
  75.     else {
  76.         echo json_encode ("user input error: the user failed to provide a username and password,
  77.                            please provide these details and retry logging into account");
  78.     }
  79.    
  80.     //Close database connection to service further db requests and avoid application from slowing down
  81.     $conn->close();
  82. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement