Php practice day 6

Source: Internet
Author: User

I learned how to create a background page today. If it fails, it will not be displayed.

Php: the following code can block php attention-level errors, that is, throw any non-Attention errors.


[Php] error_reporting (E_ALL &~ E_NOTICE );

Error_reporting (E_ALL &~ E_NOTICE );
You can use $ _ SESION after enabling session.

Session is something stored on the server. After the session is started, a session ID is sent to the browser, which is cookies.

[Php] session_start ();

Session_start ();
It doesn't matter if the browser disables cookies. You can write the session to the URL.

[Php] session_name. '='. session_id ()

Session_name. '='. session_id () is attached with the adminAction that I have written. Only the login function is supported.


[Php] <? Php
/**
*
**/
Class adminAction extends Action
{
Protected $ db = '';
Function _ construct ()
{
// Code...
/// $ Smarty-> force_compile = true;
/* $ This-> db = new MySQL ("127.0.0.1", "root", "1234", "myly", "data ");
$ This-> db-> table ('data ');*/
$ This-> db = MySQL: getClass ();
// Var_dump ($ this-> db );
$ This-> db-> connect ("127.0.0.1", "root", "1234", "myly ");
 
$ This-> db-> table ('user ');
 
Parent: :__ construct ();
}
 
Public function index ()
{
If ($ this-> is_login ()){
Echo "<script> window. location. href = 'admin. php? M = admin & a = admin'; </script> ";
} Else {
Echo "<script> window. location. href = 'admin. php? M = admin & a = login '; </script> ";
}
}
 
Private function is_login ()
{
Return $ _ SESSION ['login'];
 
}
 
Public function login ()
{
If (! Empty ($ _ POST ['username']) &! Empty ($ _ POST ['Password']) {
$ UserName = $ _ POST ['username'];
$ Password = md5 ($ _ POST ['Password']);
/* Var_dumP ($ _ POST );
Var_dump ($ password );
Var_dump ($ password );*/
If ($ _ SESSION ['userdata'] = $ this-> db-> where ("userName = '{$ userName}' and password = '{$ password }'") -> fine ()){
Echo "<script> window. location. href = 'admin. php? M = admin & a = admin'; </script> ";
$ _ SESSION ['login'] = true;

} Else {
 
Echo "Logon Failed ";
}
}
 
$ This-> display ();
}
 
Public function ulogin ()
{
Echo "<script> window. location. href = 'admin. php? M = admin & a = login '; </script> ";
Unset ($ _ SESSION );
}
 
Public function admin ()
{
If (! $ This-> is_login ()){
Echo "<script> window. location. href = 'admin. php? M = admin & a = login '; </script> ";
}
// Var_dump ($ _ SESSION );
$ This-> assign ('username', $ _ SESSION ['userdata'] ['username']);
$ This-> display ();
}
 
Function add (){
/* Var_dump ($ this-> db
-> Data (
Array ('username' => 'admin ',
'Password' => md5 ('admin '),
'Time' => time ()
))
-> Add ());
*/
}

}
?>

<? Php
/**
*
**/
Class adminAction extends Action
{
Protected $ db = '';
Function _ construct ()
{
// Code...
/// $ Smarty-> force_compile = true;
/* $ This-> db = new MySQL ("127.0.0.1", "root", "1234", "myly", "data ");
$ This-> db-> table ('data ');*/
$ This-> db = MySQL: getClass ();
// Var_dump ($ this-> db );
$ This-> db-> connect ("127.0.0.1", "root", "1234", "myly ");

$ This-> db-> table ('user ');

Parent: :__ construct ();
}

Public function index ()
{
If ($ this-> is_login ()){
Echo "<script> window. location. href = 'admin. php? M = admin & a = admin'; </script> ";
} Else {
Echo "<script> window. location. href = 'admin. php? M = admin & a = login '; </script> ";
}
}

Private function is_login ()
{
Return $ _ SESSION ['login'];

}

Public function login ()
{
If (! Empty ($ _ POST ['username']) &! Empty ($ _ POST ['Password']) {
$ UserName = $ _ POST ['username'];
$ Password = md5 ($ _ POST ['Password']);
/* Var_dumP ($ _ POST );
Var_dump ($ password );
Var_dump ($ password );*/
If ($ _ SESSION ['userdata'] = $ this-> db-> where ("userName = '{$ userName}' and password = '{$ password }'") -> fine ()){
Echo "<script> window. location. href = 'admin. php? M = admin & a = admin'; </script> ";
$ _ SESSION ['login'] = true;

} Else {

Echo "Logon Failed ";
}
}

$ This-> display ();
}

Public function ulogin ()
{
Echo "<script> window. location. href = 'admin. php? M = admin & a = login '; </script> ";
Unset ($ _ SESSION );
}

Public function admin ()
{
If (! $ This-> is_login ()){
Echo "<script> window. location. href = 'admin. php? M = admin & a = login '; </script> ";
}
// Var_dump ($ _ SESSION );
$ This-> assign ('username', $ _ SESSION ['userdata'] ['username']);
$ This-> display ();
}

Function add (){
/* Var_dump ($ this-> db
-> Data (
Array ('username' => 'admin ',
'Password' => md5 ('admin '),
'Time' => time ()
))
-> Add ());
*/
}

}
?>
Login.html used to submit the form
[Html] <Head>
<Title> log on to the background of the waterfall message board </title>
</Head>
<Body>
<Form action = "admin. php? M = admin & a = login "method =" post ">
<Input type = "text" name = "userName" value = "admin">
<Input type = "password" name = "password" value = "admin">
<Input type = "submit" value = "login">
</Form>
</Body>
</Html>

<Html>
<Head>
<Title> log on to the background of the waterfall message board </title>
</Head>
<Body>
<Form action = "admin. php? M = admin & a = login "method =" post ">
<Input type = "text" name = "userName" value = "admin">
<Input type = "password" name = "password" value = "admin">
<Input type = "submit" value = "login">
</Form>
</Body>
</Html> the admin.html


[Html] <Head>
<Title> welcome <{$ userName}> </title>
</Head>
<Body>
 
</Body>
</Html>

<Html>
<Head>
<Title> welcome <{$ userName}> </title>
</Head>
<Body>

</Body>
</Html>
This is my login function.

 


Modified mysql. class. php and added a function fine, so that the returned array is one-dimensional, which is more convenient.


[Php]/**
+ ----------------------------------------------------------
* Obtain a single record of a data table and return a one-dimensional array
+ ----------------------------------------------------------
* @ Access public
+ ----------------------------------------------------------
* @ Param
+ ----------------------------------------------------------
*/
Public function fine (){
$ Select_ SQL = 'select ';
$ Fields = isset ($ this-> query_list ['fields'])? $ This-> query_list ['fields']: '*';
$ Select_ SQL. = $ fields;
$ Select_ SQL. = 'from'. $ this-> query_list ['table']. ''';

Isset ($ this-> query_list ['join'])? ($ Select_ SQL. = $ this-> query_list ['join']): '';
Isset ($ this-> query_list ['where'])? ($ Select_ SQL. = 'where'. $ this-> query_list ['where']): '';
Isset ($ this-> query_list ['group'])? ($ Select_ SQL. = 'group by'. $ this-> query_list ['group']): '';
Isset ($ this-> query_list ['having '])? ($ Select_ SQL. = 'mysql having '. $ this-> query_list ['having']): '';
Isset ($ this-> query_list ['order'])? ($ Select_ SQL. = 'ORDER BY'. $ this-> query_list ['order']): '';
Isset ($ this-> query_list ['limit'])? ($ Select_ SQL. = '1,1 '):'';
// Echo '<br> ----->'. $ select_ SQL. '<--------- <br> ';
Return $ this-> query ($ select_ SQL) [0];
}

/**
+ ----------------------------------------------------------
* Obtain a single record of a data table and return a one-dimensional array
+ ----------------------------------------------------------
* @ Access public
+ ----------------------------------------------------------
* @ Param
+ ----------------------------------------------------------
*/
Public function fine (){
$ Select_ SQL = 'select ';
$ Fields = isset ($ this-> query_list ['fields'])? $ This-> query_list ['fields']: '*';
$ Select_ SQL. = $ fields;
$ Select_ SQL. = 'from'. $ this-> query_list ['table']. ''';

Isset ($ this-> query_list ['join'])? ($ Select_ SQL. = $ this-> query_list ['join']): '';
Isset ($ this-> query_list ['where'])? ($ Select_ SQL. = 'where'. $ this-> query_list ['where']): '';
Isset ($ this-> query_list ['group'])? ($ Select_ SQL. = 'group by'. $ this-> query_list ['group']): '';
Isset ($ this-> query_list ['having '])? ($ Select_ SQL. = 'mysql having '. $ this-> query_list ['having']): '';
Isset ($ this-> query_list ['order'])? ($ Select_ SQL. = 'ORDER BY'. $ this-> query_list ['order']): '';
Isset ($ this-> query_list ['limit'])? ($ Select_ SQL. = '1,1 '):'';
// Echo '<br> ----->'. $ select_ SQL. '<--------- <br> ';
Return $ this-> query ($ select_ SQL) [0];
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.