Quantcast
Viewing all articles
Browse latest Browse all 3

PHP Mysql Login Table

This PHP Mysql Login Table is written on Website Developer

For PHP Mysql Login Table, An application to build a blog typically consists of two parts:

  • A back-end or control panel for administering posts, comments and other features;
  • A front-end for reading the post and comments, the blog itself.

If the front-end access is generally permitted to all users, the same is not true for the administration area which, for safety reasons you must access only after a positive authentication phase.

Image may be NSFW.
Clik here to view.
The database schema is my blog for this purpose a table named login in which there are three popular fields, data entered into it will allow the user to set the necessary parameters for authentication within the admin area of the blog.

In PHP the insertion of data within a table, you can pass as a parameter to the function mysql_query() an SQL statement based on the INSERT INTO command to which they must follow in parentheses the field names involved in the insertion and their values to be inserted in them, introduced by key VALUES and always in parentheses.

Below will show a function with public modifier that will be inserted between the functions provided by the MysqlClass class and allow for INSERT query by passing three arguments:

  • $t: the name of the table in which to insert;
  • $v: values to be inserted.
  • $r: fields to populate with the value provided by the argument above.

Will proposed the code of the function, then you will go to the analysis of several steps:

    //Function for inserting data into the table
    public function insert($t, $v, $r = null) {
        if(isset($this->active)) {
            $statement = ' INSERT INTO '.$t;
            if($r != null) {
                $statement .= 's ('.$r.') ';
            }

            for($i = 0; $i < count($v); $i++) {
                if(is_string($v[$i]))
                    $v[$i] = '"'.$v[$i].'"';
            }
            $v = implode(', ', $v);
            $statement .= ' VALUES ('.$v.') ';

            $query = mysql_query($statement) or die(mysql_error());
        } else {
            return false;
        }
    }

The function Enter() operates on the basis of a simple mechanism:

  • The parameter “$t” is the name of the table in which you want to enlist, this variable is then passed as an argument to the INSERT INTO command;
  • “$r” is an array will contain the field names from popular, this array may not be empty, so the function will check the contents before executing the query.
  • “$v” is an array that represents value to populate different fields whose names are also the values of the vector “$r”;
  • Thanks to a for loop (item 12) will run as many iterations as there are values in the array “$v”, since the SQL language provides for string values must be delimited by single quotes inside of questions, with each iteration of the loop the function “is_string ()” will catch all string values in the array “$v” and replace them with the same value delimited by quotes.
  • The array “$v” changes come “imploded” (item 17) to obtain one sequence in which divides each value with a comma in it (based on the syntax INSERT INTO statement in SQL language);
  • Now the variable $istruzione will contain the value SQL statement (item 18) to pass to the function mysql_query() (item 20) which can then be executed;
  • All steps will be carried out only in case you have opened a connection to the DBMS, otherwise the function returns FALSE immediately, ignoring any other step.

Once you have created the appropriate function, entering data in the table will require only a few lines of code:

//Include the file that contains the class
include "function_mysql.php";
//instance of the class
$data = new MysqlClass();
//connecting to MySQL
$data->Connect();

//define variables to be passed to the function for data entry
$t = "login"; //table name
$v = array("admin", sha1("password")); //values to insert
$r = "username_login, password_login"; //to populate fields
//calling the function for data entry
$data->insert($t, $v, $r);
//disconnect
$data->disconnect();

The login table will therefore be involved from an INSERT INTO statement where the fields username_login and password_login will be populated with values that are admin and password (modified arbitrarily) and will these to be used for authentication.

Note that, for security reasons, the password value is not inserted into the table “in the clear”, i.e. literally, but first encrypted using sha1() that produces a 40 character string any string passed as a parameter, for this reason has been associated with a length of 40 characters in the password field of the table.


Viewing all articles
Browse latest Browse all 3

Trending Articles