Php code 100 calculates that a php registration and login page can be submitted to mysql. The port is 3307, the database name is bbs, and the table name is user-info, register. php & nbsp; login is logged on. php code, for reference to learning ------ solution ---------------------- & lt ;? Phpif (! In_array ($ _ POST [type php code 100 points
Find a php registration page that can be submitted to mysql. The port is 3307, and the database name is bbs,
The table name is user-info, and the registration is register. php. the login is login. php.
Code for reference
------ Solution ----------------------
If (! In_array ($ _ POST ['type'], array ('login', 'Reg '))){
Echo-1;
Exit ();
}
If ($ _ POST ['type'] = 'login '){
$ Username = addslashes ($ _ POST ['username']);
$ Pwd = $ _ POST ['pwd'];
$ SQL = "SELECT * FROM test WHERE name = '$ username'"; // change test to user-info.
$ Db = new DB ();
If ($ user_exists = $ db-> execute_dql ($ SQL )){
If (md5 ($ pwd) ==$ user_exists [0] ['pwd']) {
Session_start ();
$ _ SESSION ['username'] = $ user_exists [0] ['name'];
Echo 1;
Exit ();
}
} Else {
Echo-1;
Exit ();
}
} Elseif ($ _ POST ['type'] = 'Reg '){
$ Username = addslashes ($ _ POST ['username']);
$ Pwd = md5 ($ _ POST ['pwd']);
$ SQL _exists = "SELECT * FROM test WHERE name = '$ username'"; // change test to user-info.
$ Db = new DB ();
If ($ db-> execute_dql ($ SQL _exists) {// this user already exists
Echo-2;
Exit ();
}
$ SQL = "INSERT INTO test (name, pwd) VALUES ('$ username',' $ pwd')"; // change test to user-info.
If ($ code = $ db-> execute_dml ($ SQL )){
Session_start ();
$ _ SESSION ['username'] = stripslashes ($ username );
Echo 1;
Exit ();
} Else {
Echo-1;
Exit ();
}
}
Class DB {
Private $ conn;
Private $ host = "localhost"; // localhost: 3307
Private $ user = "root ";
Private $ password = "123456 ";
Private $ db = "test"; // bbs
Private $ res;
Function _ construct (){
$ This-> conn = mysql_connect ($ this-> host, $ this-> user, $ this-> password );
If (! $ This-> conn ){
Die ("failed to connect to database". mysql_error ());
}
Mysql_select_db ($ this-> db, $ this-> conn );
Mysql_query ("set names utf8 ");
}
Function execute_dql ($ SQL ){
$ This-> res = mysql_query ($ SQL, $ this-> conn) or die (mysql_error ());
$ R = array ();
While ($ row = mysql_fetch_assoc ($ this-> res )){
$ R [] = $ row;
}
Return $ r;
}
Function execute_dml ($ SQL ){
$ B = mysql_query ($ SQL, $ this-> conn) or die (mysql_error ());
If (! $ B ){
Return 0; // failed
} Else {
If (mysql_affected_rows ($ this-> conn)> 0 ){
Return 1; // success
} Else {
Return 2; // The number of rows is not affected.
}
}
}
Function _ destruct (){
If (! Empty ($ this-> res )){
Mysql_free_result ($ this-> res );
}
Mysql_close ($ this-> conn );
}
}
?>
Exe. php
If (! In_array ($ _ POST ['type'], array ('login', 'Reg '))){
Echo-1;
Exit ();
}
If ($ _ POST ['type'] = 'login '){
$ Username = addslashes ($ _ POST ['username']);
$ Pwd = $ _ POST ['pwd'];
$ SQL = "SELECT * FROM test WHERE name = '$ username'"; // change test to user-info.
$ Db = new DB ();
If ($ user_exists = $ db-> execute_dql ($ SQL )){
If (md5 ($ pwd) ==$ user_exists [0] ['pwd']) {
Session_start ();
$ _ SESSION ['username'] = $ user_exists [0] ['name'];
Echo 1;
Exit ();
}
} Else {
Echo-1;
Exit ();
}
} Elseif ($ _ POST ['type'] = 'Reg '){
$ Username = addslashes ($ _ POST ['username']);
$ Pwd = md5 ($ _ POST ['pwd']);
$ SQL _exists = "SELECT * FROM test WHERE name = '$ username'"; // change test to user-info.
$ Db = new DB ();
If ($ db-> execute_dql ($ SQL _exists) {// this user already exists
Echo-2;
Exit ();
}
$ SQL = "INSERT INTO test (name, pwd) VALUES ('$ username',' $ pwd')"; // change test to user-info.
If ($ code = $ db-> execute_dml ($ SQL )){
Session_start ();
$ _ SESSION ['username'] = stripslashes ($ username );
Echo 1;
Exit ();
} Else {
Echo-1;
Exit ();
}
}
Class DB {
Private $ conn;
Private $ host = "localhost"; // localhost: 3307
Private $ user = "root ";
Private $ password = "123456 ";
Private $ db = "test"; // bbs
Private $ res;
Function _ construct (){
$ This-> conn = mysql_connect ($ this-> host, $ this-> user, $ this-> password );
If (! $ This-> conn ){
Die ("failed to connect to database". mysql_error ());
}
Mysql_select_db ($ this-> db, $ this-> conn );
Mysql_query ("set names utf8 ");
}
Function execute_dql ($ SQL ){
$ This-> res = mysql_query ($ SQL, $ this-> conn) or die (mysql_error ());
$ R = array ();
While ($ row = mysql_fetch_assoc ($ this-> res )){
$ R [] = $ row;
}
Return $ r;
}
Function execute_dml ($ SQL ){
$ B = mysql_query ($ SQL, $ this-> conn) or die (mysql_error ());
If (! $ B ){
Return 0; // failed
} Else {
If (mysql_affected_rows ($ this-> conn)> 0 ){
Return 1; // success
} Else {
Return 2; // The number of rows is not affected.
}
}
}
Function _ destruct (){
If (! Empty ($ this-> res )){
Mysql_free_result ($ this-> res );
}
Mysql_close ($ this-> conn );
}
}
?>
------ Solution ----------------------
Let's just look at it. come on!