Home >Backend Development >PHP Tutorial >Basics of ecshop from scratch (19)_PHP tutorial

Basics of ecshop from scratch (19)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:17:011003browse

(to reduce the burden on the entry file and move the functions implemented by the entry file into the basic class of the framework)

Add Framework.class.php in the framework directory

Plan: Divide the public functions that need to be initialized into modules and form each method of the framework base class, and call and execute them separately to complete the initialization function of the project.

Use static method classes to complete: (see it as a collection of functions, not a drawing design object; practice static use)

Note, the handling of magic constants

The dirname() function can obtain the path part of an address

2
3
2
3
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

2
3
4
56
7
8
9
10
11
12
13
14
span>

Auto-loading function at this time:

There is a special project for the implementation of functions. The implementation of class static methods

However, PHP only knows one function called __autoload(), which means PHP cannot find this function.

Processing method:

Inform PHP that when you need to find an automatic loading function, just look for the automatic loading method we defined.

Register an ordinary function (or method) as an automatic loading function

Use a php function: spl_autoload_register(); to register ordinary functions (methods) as automatic loading

Parameters, functions or methods that need to be registered

Parameter function: function name is enough, just use a string

Parameter method: class (object) and method name. Use an array, the first element class name, the second element method name 2
3
4
5
6
7
                    );            } 
                }   
                }
  }
     FRAME_PATH .  );                                                                                                                         ,-5) ==                                                                                                                     }                                                                               ) {
                                                                                                             
span> span> span>

In the entry file, load the Framework.class.php framework base class and run the run() program run

2
3
4
5
6
                            }                     }
    }
    }
                                                                                                                                                                          =                                                                                                                                                $this->jump( } }
}
}

setcookie(name, value, validity period, valid directory, valid domain name)

setcookie second, only string

setcookie('name',array('itcast','php'))

2
3
4
setcookie(setcookie(
2
3
4
span>
setcookie(setcookie(

At this point, when getting the variable:

$_COOKIE['info'] becomes an array with 2 elements

$_POST['info'] = array()

By default, the browser is closed, also known as temporary cookies

Using the third parameter of setcookie, it can be configured: use a timestamp to indicate the validity period

time() gets the current timestamp. Do additions and subtractions to get timestamps for other times.

span> 2
3
4
5
6
Typical setting method

2
3
4
5
6

2
3
4
5
//但是,浏览器端的还是itcast
2
3
4
5
//However, the browser side is still itcast

The cookie is saved on the browser side, php Data can be saved on the browser by issuing a command.

Cookies on your browser

Use firebug to go to the network panel to monitor the requests sent by the browser and the responses received:

In the response data, the browser that sends the data should tell the browser to save the cookie variable:

The server uses part of the response data (response header) to send the set cookie information to the browser:

Viewing The request data sent by the browser carries the cookie that the browser considers valid to the server.

At this time, the server received the request and found that cookie data existed. This data was used to form the $_COOKIE array for use by user scripts.

Session and cookie solve the same problem.

Get a method that can pass data in multiple requests from the same browser.

Cookie Disadvantages:

Data is stored directly on the browser side. There are two obvious problems:

Plan:

On the server, save the data.

How to ensure that data can be passed between multiple requests of the browser and distinguish between browsers.

On the server side, create an independent data space for each visiting browser. Assign a unique identifier to each data space and let the browser save this unique identifier. Every time the browser makes a request, it carries the identifier and uses the identifier to determine the unique data space.

Summary:

Session technology: The session data is saved on the server side, and the browser side saves the storage data space identifier. The browser carries the identifier when requesting, and the server is responsible for using the identifier to perform data processing in the corresponding storage space.

session_start() can be turned on

2
3
4
5
6
7
8
Use the predefined variable $_SESSION variable to operate. Adding, deleting, modifying, and checking are all done on $_SESSION. 2
3
4
5
6
7
8
table>

Tips; session technology is based on cookie technology and needs to save the identifier in the cookie

When the session is opened, the server will assign a cookie variable to the browser to save the sessionID (identification of the session), and save it to the browser device end.

Tips: It is a cookie variable that is valid for the entire site

In the next request of the browser, the cookie variable and sessionID are carried to the server:

Default: PHP saves each independent data space in the form of a file. It is saved in the temporary directory of the server system.

The file name is named after the current session ID to ensure spatial uniqueness.

It can be seen that when saving, it is the result of the deterioration of academic qualifications.

Two basic steps:

Only the elements of $_SESSION in the following table can be numeric.

2
2

unset($_SESSION['key']); delete an element in $_SESSION

What if all session data is deleted?

$_SESSION = array();

Incorrect: unset($_SESSION); will not cause session data to be lost. PHP’s internal session mechanism can also find existing processed session data and write it to the session data space.

Delete session-related storage files

2
session_destroy() function can be completed 2

However, if the file is deleted, the data in the $_SESSION array is still there:

However, after executing session_destroy(), the write operation after the script cycle will not be executed.

How to completely delete all data of a session?

File, $_SESSION, sessionID variable in cookie

session_destroy();

$_SESSION = array();

setcookie('PHPSESSID','',time() - 1);

Typically complete by executing session_start()

Supported, automatically enabled in configuration file

However, if the session is opened repeatedly, an error will be reported:

Typically just mask the errors

can be configured:

The session data processing method used by session.save_handler php

can be changed to user, indicating user customization

session.save_path

Different storage locations

Low sensitivity, data that needs to be saved permanently is stored in cookies (user experience)

High security, the data existing during the session period is saved in the session (data legality, rationality, integrity)

session can save various data types

Typically save the login verification logo into the session:

application/controller/back/AdminController.class.php

2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
                }
}  } span>

2
3
4
5
6
7
8
                    
    }   
}
application/controller/back/IndexController.class.php

2
3
4
5
6
7
8
                                                                                                     >

Record login status

After successful login, determine whether you have chosen to save the login information:

2
3
4
    
application/controller/back/AdminController.class.php signinAction() 2
3
4
 

What format to record:

Confidential and can be verified

Cannot save, background login information

Must be at least a pair and can be verified

Designed to:

admin_id

Processed password (encrypted on the basis of md5)

2
3
4
5
2
3
4
5
        setcookie(}

setcookie(}

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
        $result = $model_admin->checkByCookie();
                    $session_start();
            }             }
}

2
3
4
5
6
7
8
9
10
                }
        }

application/controller/back/IndexController.class.php
indexAction()

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$result = $model_admin->checkByCookie();
                                                                                                                           span [ }   }
}
Model Add a checkByCookie() method in AdminModel: application/model/AdminModel.class.php 2
3
4
5
6
7
8
9
10
 }
http://www.bkjia.com/PHPjc/626631.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/626631.htmlTechArticle (to reduce the burden on the entry file and move the functions implemented by the entry file into the basic class of the framework) In the framework directory Add Framework.class.php plan: public functions that need to be initialized, split the model...

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn