SoFunction
Updated on 2025-04-13

Create the world's simplest PHP development model page 5/5

, 5); 
$data[asphotlist] = getArticleList(2, "clicks DESC, id DESC", 1, 3); 
dbDisconnect(); 

renderTpl(’’, $data); 

?> 

----------------------
<!-- BEGIN content -->
Number:{id}<br>Title:{title}<br>Content:{content}
<!-- END content -->


--------------------------
<?php 

require_once ""; 

dbConnect(); 
$data = array(); 
$data[content] = getArticle((int)$_GET[id]); 
dbDisconnect(); 

renderTpl(’’, $data); 

?>

8: Add implementation and template processing of articles (a step toward the Long March)

--------------------------
<form action="" method="post">
Title:<input type="text" size="20"><br>
Content:<textarea cols="50" rows="4"></textarea><br>
&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" value="submit">
</form>


---------------------------
<?php 

require_once ""; 

renderTpl(’’, array()); 

?> 



-----------------------------
<?php 

function doPostVar(&$data){ 
    $keys = array_keys($data); 
    foreach($keys as $key){ 
        $data[$key] = addslashes(htmlspecialchars(trim($data[$key]))); 
        if($data[$key]==null) $data[$key] = "$key default"; 
    } 

?> 


---------------------------
<!-- BEGIN success -->
{content}
<!-- END success -->


---------------------------
<?php 

require_once ""; 

dbConnect(); 
$data = action(); 
doPostVar($data); 
addArticle($data); 
$result[success][content] = "Article added successfully";
dbDisconnect(); 

renderTpl(’’, $result); 

function action(){ 
    $data = array(); 
    $data[title] = $_POST[title]; 
    $data[content] = $_POST[content]; 
    $data[datetime] = date(’Y-m-d H:i:s’); 
    $data[pid] = 1; 
    return $data; 


?> 

Such a simplest article publishing system is completed. I wonder if it has gained anything for you.

9. Summary


The program is finished, let’s summarize it and take a look at the implementation process. It should be said that it is simple and clear.
1. Unified implementation of database access interface. When changing the background database structure, just simply modify the corresponding interface function, and the other parts of the php code are ignored.
2. The entire system php code and html code are managed separately. The php code front desk is also very simple to implement, and you should have already discovered that it is basically around 10 lines of code.
3. Can expand functions at will. After wrapping the new function with functions, only a simple call to the corresponding function is added to the query display page of the specific front desk, and a line of code is done.
4. Code reuse. By wrapping the function, a large amount of unnecessary duplicate code work is reduced, and the efficiency should be improved a lot, right?
5. Portability. If you build other websites in the future and encounter the same implementation functions as this project, what can you do? Copy the functions and database structure here and modify the template of the new project. Can you complete the new project as soon as you want? There is no need to consider modifying the php code at all.
6. Clear structure code. Others can easily develop a project with you, and code conflicts will be minimized.

Of course, the situations encountered in actual work may be much more complicated than this example, but even the most complex tasks can be split.


10. Submission

I don’t know if you have learned things like jsp and servlet. They have many excellent design ideas, which are worth studying and copying (to be honest, those who have time should come into contact with Java, not to learn this language, but to learn its design patterns and excellent functional implementation methods). jsp has at least two things that are useful to us, action and filter.

Action is mainly used to process some event logic, just like the action() function we defined in, used to verify and obtain the data submitted by the form. Of course, the amount of code written in this way is the same as the amount of code in your previous method, and it seems that there is no difference, but it realizes the separation of code. Is the structure much clearer than your previous method?

filter is a filtering function used to filter and redirect network access and redirect some illegal requests and incorrect requests. Let’s take a look at its specific role through a backend management program.


Copy the following function into yours. This function is a filter function for background management login, and a function that does not cache web pages locally:

function windowNoCache($cache){ 
    if(!$cache || headers_sent()) return ; 
    header(’Expires: ’.date(’D,d M Y H:i:s’,mktime(0,0,0,1,1,2000)).’ GMT’); 
    header(’Last-Modified:’.gmdate(’D,d M Y H:i:s’).’ GMT’); 
    header(’Cache-control: private, no-cache,must-revalidate’); 
    header(’Pragma: no-cache’); 

function isAdminLogin(){ 
    if($_SESSION[relogin]=="ok") return; 
    if($_SESSION[adminuser]!=SYS_ADMIN_NAME){ 
        $_SESSION[relogin] = "ok"; 
        die("<script language=\"javascript\"> =’’; </script>"); 
    } 
    $_SESSION[relogin] = "no"; 


Then add the following file under the root directory
------------------------
<?php 

define(’SYS_ADMIN_NAME’, ’hello’); //Background management login name
define(’SYS_ADMIN_PASSWORD’, ’hello’); //Background management login password
include ’’; 
windowNoCache(true); 
isAdminLogin(); 

?> 



----------------------------
<?php 

require_once ""; 

renderTpl(’’, array()); 

?> 



------------------------------
<?php 

include ""; 

if($_POST[name]==SYS_ADMIN_NAME && $_POST[code]==SYS_ADMIN_PASSWORD){ 
    $_SESSION[adminuser] = SYS_ADMIN_NAME; 
    header("location: "); 
}else{ 
    renderTpl(’’, array()); 


?> 


Add the following file to the smart/template directory
------------------------------
<form name="form1" method="post" action="">
<input type="text" name="name" size="20" value="">
<input type="password" name="code" size="20" value="">
<input type="submit" name="Submit" value="Login ">
</form>


----------------------------
<h2>Hello welcome to log in to the background management</h2>

Now visit and see what happens, and then log in with the username and password set inside. This function can come in handy in many places on the web, and it should be a good method. As long as other background access pages are loaded, you no longer need to consider the background access permissions.


Eleven Appendix

The simple hidden file extension makes the viewer confused and makes him confused about what language you wrote the program.

Just take it as an example.

1. The name we modified is
2. Enter the command line window (dos window), and create a file under the web directory, with the file name ".htaccess".
3. Edit the ".htaccess" file and enter the following content

AddType application/x-httpd-php .tmp

Visit through the browser to see if it is OK.
Previous page12345Read the full text