Programming specific examples using phpo

xiaoxiao2021-03-06  66

Some statements

With regard to this example, this example is originally written for the members of the Association (the GNU / Linux Association of Sun Yat-sen University), so it is written from a relatively long-term perspective, considering the code preparation specification and habits and algorithms. Considering that these factors are important to programming, I have not deleted the relevant chapters and content. If the reader feels that this part is not necessary, please email the author, thank you.

2. Code writing style

It's not how to write an comment or write copyright information, but proposes a number of representative and beautiful program habits (here, you are called doing style). In general, the Web scripts such as PHP and Perl inevitably becomes related to HTML. And quite a lot of programmers did not pay attention to their own PHP / Perl code and HTML code, causing the process of hardships, and the structure is finally lazy.

From the development of Zsu-Slash-Pro discovery, excellent procedures not only have excellent algorithms, but also beautiful and easy to understand. Especially for large programs, if the code is vague, the structure is confusing, even the program developer himself, it seems that the code will feel extraordinary, others look more worker.

It is therefore necessary to explore and develop a specific solution for proper writing code, use this set of specifications to constrain and unified program developers developed. Make it easy to read and modify, facilitate future maintenance and further development, so that the debugging program is simplified.

Here, I mainly refer to a few large programs such as PHPSLASH and PHPLIB and FREETRADE, combined with their little experience, summed up the following points:

(1). Universal content is included in the included file

This method is to make the general content that each generated page contains contains files, which reduces the length of the program. Easy to make a unified modification, such as PHPSLASH, each page needs to use a unified Header / Footer, so you only need to create two containing file slashhead.inc and slashfoot.inc files, and then use the request in each PHP program to include them The code can be within the code.

Note: Not only specifies the use of Header and Footer, if you like, you can add any of the plurality of requires.

(2). Public function library

For those frequent functions and variables, there is no need to define and declare in each program, but only need to put them in a public function / configuration file.

For example, dozens of functions in PHPslash, which are uniformly stored in functions.inc and is included in the configuration file config.php3. This will not need to define all pages to the function of all pages to the inside. It is only necessary to call directly.

(3) .html code generation function (library)

For some HTML code that often needs to be written, these HTML code can be generated with a dedicated function.

For example, a dialog box that displays the login, you can write a function of the content of this dialog called DSP_Login (). This is only necessary to call this function directly every time you need it. On the same way, you can make a function of the head and tail of the generated page, such as put_header () and put_footer (), and so on. This makes it easy to separate HTML code and PHP code.

(4). Package process

Try to hide the process of specific operations, replacing an object-oriented class or an action function. This kind of benefit is to encapsulate the process, making the main program simple and clear, and it is clear.

For example, a registration program, it needs to complete the display registration window, retrieve users, display errors, insert users, send email, etc. 5 functions. If these five functions are designed in general process, the code is relatively difficult to understand. If you replace 5 operation functions (or put the specific analysis operation process), then it is very simple and easy to understand. The following is the program main body:

DSP_REG ();

CHECK_USER ();

IF ($ RETURN == "True")

{

INSERT_USER ();

Mail_user ();

}

Else dsp_ERROR ();

It can be seen that such a program is easy to understand even beginners.

(5). Functional blockage

Try to divide the functionality of the program to be executed into a corresponding operation / action, which is divided, the advantage is easy to debug, and the provision is clear.

One of the best examples is PHPSLASH admin.PHP3. It summarizes all functions into an OP value. For example, to delete a user -> $ op = "userdelete". In fact, it still has a small trick: just call an OP but send another value user_OPS when the form is sent, this value has several possible: delete / add / update. Then it has a judgment array in the program, and it is determined that this user_ops should correspond to what OP, so it is convenient for design. Here, it is assumed that a simple system that releases news, the function you need is to publish news, delete news, update news.

Then it is summarized into the following 3 OP: post / delete / update, the following is a structural process (with SWICH statements):

SWICH ($ OP) {

Case "POST":

Postnews ();

Break;

Case "delete":

DELETE ();

Break;

Case "Update":

Update ();

Break;

DEFAULT:

DSP_NEWS ();

}

It can be seen from the above points that if it is possible to follow the designs mentioned above, then your design will be a good structure, the code is beautiful and easy to understand. However, this must also depend on good programming ideas. An example of a registration program is given below, which reflects most of this article and this programming specification mentioned. The entire program is only 5 hours from design to completion time.

Although it is not fast, it is basically a way to work, there is no logic error, and the structure is also clear.

Next, the reader will see how to use the above tips and principles to prepare a specific program.

3. Concrete programming

(1). Features:

What you need to do here is a registration process, its functional requirement is to automatically register a legitimate user, and will automatically generate an 8-bit password and send it to the user's mailbox. The identifier of the user is the only user name and the only and real email address.

The application process is: first enter the user name you want to register and his real email.

------> Then the system queries the user's name and mailbox existing in the database.

------> If one exists, the error is displayed and returned to re-enter

------> If both are not recorded in the system database, explain that the user can register, show the welcome screen

------> Display confirmation screen and register user information

-----> Automatically generate passwords and send it to user mailbox.

(2). Process:

Basically, it is similar to the application process. Operation according to different designs of each function:

a. Show Query Page

DSP_QUERY (); // White mode

Enter query data, next step

b. Execute Query ();

If success, next step

If it is not successful, the error is displayed and returned.

c. Display confirmation page

CONFIRM_REG (); DSP_REG (); // Save mode Send Form

d. Insert the user

REG ();

Join the judgment: Whether to refresh the page multiple times, if it does not display "Congratulations".

INSERT ();

Mail_user ();

Success ();

(3). Functional function:

It is used here to 10 functions. The functions of each function are as follows:

DSP_QUERY (); // Display the query screen and the confirmation screen and automatically select different OP according to different screens

Query: OP = Check confirmation: op = insert

Query (); // Query function, check whether the user and mailboxes entered by the query

Confirm_reg (); // Confirm that the function to register

REG (); insert the user's main function, complete the main action

Make_passwd (); // Generate 8-bit random password

INSERT (); // Insert a function of user information

Mail_user (); send registration information and user password to the user's mailbox

Success (); // Register a successful function, show successful information

Error_msg (); // Have a variety of modes of error prompt functions

Current_time (); // prompts the current system time function

(4). Page structure:

The techniques contained in Header and Footer are adopted. And all and database connections are defined in config.php3 and using Zsulib. This is called to object-oriented database connection technology in Zsulib.

The main structure is:

HEADER.INC

Register.php3 -------> Require "config.php3";

(5). Start specific writing

Write Header.inc, Footer.inc (omitted)

Write specific functions and related interfaces

(6). Solve key technical issues

a. How to write query ()?

The function required by this function is to query whether the username and mailbox have been registered. If one or all of them returns an error message, otherwise enter the confim_reg () function.

Design Idea: Whether the $ UserName and $ Email sent by the database query are to see if the null value is returned. Then determine if the query value of the two fields is == "". This completes the basic query function.

The following is query (); full code:

Function Query ($ UserName, $ Email)

{

$ SL_Q = New Userdb;

$ SL_Q-> Query ("SELECT UserName, Email from user where username = '$ usrname' or email = '$ email'");

$ SL_Q-> Next_Record (); $ tusername = $ sl_q-> record ["username"];

$ TEMAIL = $ SL_Q-> Record ["email"];

IF ($ usrname == $ tusername && $ tusername! = ") Error_MSG (" user_exist ");

Elseif ($ Email == $ TEMAIL && $ TEMAIL! = ") ERROR_MSG (" mail_exist ");

Elseif ($ tusername == "" && $ tmail == "&& $ usrname! =" "&& $ email! =" ")

CONFIRM_REG ($ UserName, $ Email); Else Error_MSG ("UnkNown");

}

b. How to write make_passwd ()?

This function is responsible for generating a random 8-bit password. Design Idea: The random factor generated by the Random daemon of the UNIX system can also utilize the random function of PHP to obtain the specified 8 for the password. You must use a function that can be cut from the string to a given length string. Here we use Substr.

The following is made_passwd (); basic code

Function make_passwd ()

{

$ RAN = rand ();

$ Passwd = SubStr ($ RAN, 0, 8);

Return $ Passwd;

}

c. How to write confim_reg ()?

It is not easy to achieve that you can prompt you to confirm that this feature is not easy. Double DSP_Query () must be called twice, but DSP_QUERY () needs to be done. Therefore, the confim_reg () must be transferred to the number of parameters to DSP_Query () let it know how to operate.

Design Idea: Let confim_reg () pass a $ msg to DSP_Query (), let it know that this is the confirmation information to conversion.

The following is confim_reg () full code:

Function Confirm_reg ($ UN, $ EM)

{$ TIME = Current_time ();

$ msg = "

Congratulations! In $ time This moment your username and mailbox have not been registered!

If you really want to be a member of this site, press to register the button.

DSP_QUERY ($ UN, $ EM, "I want to register", "$ msg");

}

d. How to write DSP_QUERY ()?

The function is a slightly complex point, that is, it is necessary to determine what tasks you want to implement according to $ msg.

Design Idea: Due to the query and confirmation of the registration, the query button should be automatically replaced with the registration button. This is implemented using parameters $ botton. It is determined whether it is inquiries or confirmed, it can be used to use the $ msg to judge (because the confirmation function will send a $ msg). In addition, you must distinguish between the two, you must automatically select $ OP =? So you can also assign a value to $ OP via $ msg. The query is: Check, confirm that it is INSERT.

The following is the basic code of DSP_QUERY ():

Function DSP_QUERY ($ UN, $ EM, $ BOTTON, $ MSG)

{

$ Head = "";

IF ($ BOTTON == ") $ botton =" query ";

IF ($ msg! = ") $ action =" insert "; // If there is confirmation information, INSERT

Else $ action = "check"; / / otherwise just a general query

$ CATION = "Welcome to this site to register. Once the account is entry into force, you can enjoy the first-stream of the first-stream of the site";

$ pos = "

Username:

Mailbox Name:

;

$ FOOT = "";

IF ($ msg == ") FANCYBOX (" 100% "," Query ", $ Head. $ POST."

". $ cation." ". $ foot," r ");

Else {

FancyBox ("100%", "confirm", $ head. $ post. "". $ msg. "". $ foot, "r");

}

}

e. Last judgment statement:

The operational process and function call of the judgment program are determined. Just give the code (very simple code) here (very simple code):

Switch ($ OP) {

Case "Check":

Query ($ UN, $ EM);

Break;

Case "insert":

REG ($ UN, $ EM);

Case "NULL":

Break;

DEFAULT:

DSP_QUERY ();

}

(7). End writing

转载请注明原文地址:https://www.9cbs.com/read-86617.html

New Post(0)