Advertisement
HoltOnFoTheRide

Unfinished login manager

Jul 22nd, 2013
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.21 KB | None | 0 0
  1. <?php
  2.     class loginManager
  3.     {
  4.        
  5.         /*
  6.          * Class loginManager functions:
  7.          * __construct(): Constructor. Pass the parameters of the MySQL connection variables and it will create the object and then calls mysqlConnect().
  8.          * mysqlConnect(): Creates an OO connection to MySQL using the parameters passed.
  9.          * mysqlQuery(): Sanitizes the SQL query by mysqlSanitize() and then runs it from the connection created using mysqlConnect().
  10.          *      * Returns an 3D array where the first bracket is the row number. The second bracket is the column name.
  11.          * mysqlSanitize(): Sanitizes the SQL Query passed as a parameter by using mysqli_real_escape_string().
  12.          * returnMax(): Returns the greatest value of the parameters passed into the functions.
  13.          */
  14.        
  15.         /*
  16.          * Variables beginning with $database involve the database username, password, or host.
  17.          */
  18.            
  19.         private $database_host = "";
  20.         private $database_user = "";
  21.         private $databate_pass = "";
  22.         private $database_name = "";
  23.        
  24.         /*
  25.          * Variables beginning with $sql involve SQL queries or connections.
  26.          */
  27.        
  28.         private $sql_conn;
  29.        
  30.         function __construct()
  31.         {
  32.             //This constructor needs to be passed four parameters, the host, user, pass, and name of the db (in that order).
  33.            
  34.             switch (func_num_args())
  35.             {
  36.                 case 4:
  37.                     //If the number of parameters passed is correct, put them into an array.
  38.                    
  39.                     $argarray = func_get_args();
  40.                    
  41.                     /*
  42.                      * (The above array)
  43.                      * $argarray[0] = database host
  44.                      * $argarray[1] = database username
  45.                      * $argarray[2] = database password
  46.                      * $argarray[3] = database name
  47.                      */
  48.                    
  49.                     //Next, set the variables that were passed as parameters.
  50.                
  51.                     $this->setHost($argarray[0]);
  52.                     $this->setUser($argarray[1]);
  53.                     $this->setPass($argarray[2]);
  54.                     $this->setName($argarray[3]);
  55.                
  56.                     //Next, connect to the MySQL database with the previous information.
  57.                
  58.                     $this->mysqlConnect($this->database_host, $this->database_user, $this->database_pass, $this->database_name);
  59.                     break;
  60.                 default:
  61.                     //If there were not four parameters passed into the constructor, end the creation of it.
  62.                    
  63.                     die("There were not four parameters passed into the constructor of the loginManager class. There were only " . func_num_args() . " passed.");
  64.                     break;
  65.             }
  66.         }
  67.        
  68.         private function mysqlConnect($database_host, $database_user, $database_pass, $database_name)
  69.         {
  70.             //This function connects to a MySQL database.
  71.            
  72.             $this->sql_conn = new mysqli($database_host, $database_user, $database_pass, $database_name);
  73.            
  74.             if (mysqli_connect_error())
  75.             {
  76.                 //If there was an error connecting, call the die function and close the connection.
  77.                
  78.                 $this->sql_conn->close();
  79.                 die("mysqlConnect could not be completed: " . mysqli_connect_error());
  80.             }
  81.         }
  82.        
  83.         public function mysqlSanitize($string)
  84.         {
  85.             //This function will sanitize a string that was passed as a parameter and return it.
  86.            
  87.             return mysqli_real_escape_string($string);
  88.         }
  89.        
  90.         public function mysqlQuery($query)
  91.         {
  92.             //Check and make sure the the $sql_conn variable is set.
  93.            
  94.             if (!isset($this->sql_conn))
  95.             {
  96.                 //If the $sql_conn variable is not set, call the die function.
  97.                
  98.                 die("You have not created a connection to a MySQL database.");
  99.             }
  100.            
  101.             if (!is_string($query))
  102.             {
  103.                 //If a string was not passed as a parameter, call the die function.
  104.                
  105.                 die("An SQL query must be passed as a paramter int he mysqlQuery function.");
  106.             }
  107.             else
  108.             {
  109.                 //If a string was passed as a parameter, then run the query.
  110.                
  111.                 $resultarray = array();
  112.                 $result = $this->sql_conn->query($query);
  113.                 while ($row = $result->fetch_array())
  114.                 {
  115.                     //As long as there are rows that matched the query, add to the array.
  116.                    
  117.                     $resultarray[] = $row;
  118.                 }
  119.                
  120.                 //Now, close the results and return the values.
  121.                 $result->close();
  122.                 return $resultarray;
  123.                
  124.                 /*
  125.                  * $resultarray[x][y]:
  126.                  * [x] = the row number.
  127.                  * [y] = the column name.
  128.                  */
  129.             }
  130.         }
  131.  
  132.         public function returnMax()
  133.         {
  134.             //Create an array of the parameters passed.
  135.            
  136.             $argarray = func_get_args();
  137.            
  138.             //Create a blank variable that will later contain the largest variable.
  139.            
  140.             $largest = 0;
  141.            
  142.             //Loop through all of the parameters passed.
  143.            
  144.             foreach ($argarray as $value) {
  145.                
  146.                 //Check if the value is an integer.
  147.                
  148.                 if (!is_numeric($value))
  149.                 {
  150.                     //If the current value is not a number, skip the value.
  151.                    
  152.                     continue;
  153.                 }
  154.                
  155.                 if (intval($value) > intval($largest))
  156.                 {
  157.                     //If the new value is larger than the previous largest one, update it!
  158.                    
  159.                     $largest = $value;
  160.                 }
  161.             }
  162.         }
  163.        
  164.         function setHost($var)
  165.         {
  166.             $this->database_host = $var;   
  167.         }
  168.        
  169.         function setUser($var)
  170.         {
  171.             $this->database_user = $var;
  172.         }
  173.        
  174.         function setPass($var)
  175.         {
  176.             $this->database_pass = $var;
  177.         }
  178.        
  179.         function setName($var)
  180.         {
  181.             $this->database_name = $var;
  182.         }
  183.     }
  184.    
  185.     $loginManager = new loginManager("127.0.0.1","root","", "logintest");
  186.     $resultarray = $loginManager->mysqlQuery("SELECT * FROM users");
  187.     echo $loginManager->returnMax(4,6,7,2);
  188. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement