A PHP template, mainly to reflect the idea_PHP tutorial
思路:
欲在速度和易用(主要指的是美工设计的方便性)之间取得一个平衡点.于是采用了由html文件生成php文件的办法(编译?)
也想在分离显示逻辑和分离html代码之间平衡一下
例如一个论坛首页(index.php):
代码: |
require('./template.php'); //由html生成的php文件的前缀,区别使用多种风格. $tpl_prefix = 'default'; //模板文件名 $tpl_index = 'index'; $tpl = new Template($tpl_prefix); $cats = array( array('forum_id'=>'1','forum_cat_id'=>'0','forum_name'=>'PHP学习'), array('forum_id'=>'2','forum_cat_id'=>'0','forum_name'=>'MYSQL学习') ); $forums = array( array('forum_id'=>'3','forum_cat_id'=>'1','forum_name'=>'PHP高级教程'), array('forum_id'=>'4','forum_cat_id'=>'1','forum_name'=>'PHP初级教程'), array('forum_id'=>'5','forum_cat_id'=>'2','forum_name'=>'MYSQL相关资料') ); if ($cats) { if ($tpl->chk_cache($tpl_index))//检查判断是否需要重新生产PHP模板文件. { $tpl->load_tpl($tpl_index);//加载html模板文件. //替换PHP语句 $tpl->assign_block("{block_cat}",""); $tpl->assign_block("{/block_cat}","}?>"); $tpl->assign_block("{block_forum}"," \nif(\$forum['forum_cat_id'] == \$cat['forum_id']) {?>"); $tpl->assign_block("{/block_forum}","}\n}?>"); //生产PHP模板文件. $tpl->write_cache($tpl_index); } } //包含PHP模板文件. include($tpl->parse_tpl($tpl_index)); ?> |
对应的html模板文件(index.html):
代码: | |||||||
{block_cat}
{/block_cat} |
经过处理,里面的{block_forum}{block_cat}标签被替换成PHP循环语句,用于显示数组种所有元素.
生成的PHP模板文件(default_index.php):
代码: | |||||||
}?> |
default_index.php is included in index.php, so that it can be displayed normally.
In this way, the HTML template file can be modified and beautified with dw, which should be more convenient for artists.
template.php
Code: |
/***************************************************** ***************************** * ” ” ” ” ” ” Forum usage * * * ********************************** *************************************************/ class Template { //$this->$template, stores template data. var $template = ''; //Template path. var $tpl_path = ''; //Template prefix (style name). var $tpl_prefix = ''; //cache path (compiled path). var $cache_path = ''; //css file path. var $css_path = ' '; //header file path. var $header_path = ''; //footer file path var $footer_path = ''; /** * Initialize template path. */ function Template($root = 'default') { //Template prefix (style name). $this->tpl_prefix = $root ; //Template file path. $this->tpl_path = './templates/' . $root . '/'; //Generated PHP file storage path. $ this->cache_path = './template_data/' .$this->tpl_prefix . '_'; return true; } /** * chk_cache, check whether the "compiled" template needs to be updated. Judgment basis: last modification time, whether the "compiled" file exists. */ function chk_cache($tpl_index) { $tpl_file = $this->tpl_path . $tpl_index . '.html'; $cache_file = $this->cache_path . $tpl_index . '. php'; //Determine whether it needs to be updated. if(!file_exists($cache_file)) { } return true; } elseif(filemtime($tpl_file) & gt ; filemtime($cache_file)) { ='') { return $this->cache_path . $tpl_index . '.php'; } /** * Output template file. */ function load_tpl ($tpl_index) { $tpl_file = $this->tpl_path . $tpl_index . '.html'; $fp = fopen($tpl_file, 'r'); $this ->template = fread($fp, filesize($tpl_file)); fclose($fp); } /** * Load template file. */ function write_cache( $tpl_index) { $cache_file = $this->cache_path . $tpl_index . '.php'; //Variable display. $this-> template = preg_replace("/({=)(.+?)(})/is", "=\2?>", $this->template); // Interface language replacement. $this->template = preg_replace("/{lang +(.+?)}/ies", "$lang['main']['\1']", $this- >template); $fp = fopen($cache_file, 'w'); flock($fp, 3); fwrite($fp, $this->template) ; fclose($fp); } /** * Replace block. */ function assign_block($search,$replace) { $this->template = str_replace($search,$replace,$this ->template); } } ?> |

To protect the application from session-related XSS attacks, the following measures are required: 1. Set the HttpOnly and Secure flags to protect the session cookies. 2. Export codes for all user inputs. 3. Implement content security policy (CSP) to limit script sources. Through these policies, session-related XSS attacks can be effectively protected and user data can be ensured.

Methods to optimize PHP session performance include: 1. Delay session start, 2. Use database to store sessions, 3. Compress session data, 4. Manage session life cycle, and 5. Implement session sharing. These strategies can significantly improve the efficiency of applications in high concurrency environments.

Thesession.gc_maxlifetimesettinginPHPdeterminesthelifespanofsessiondata,setinseconds.1)It'sconfiguredinphp.iniorviaini_set().2)Abalanceisneededtoavoidperformanceissuesandunexpectedlogouts.3)PHP'sgarbagecollectionisprobabilistic,influencedbygc_probabi

In PHP, you can use the session_name() function to configure the session name. The specific steps are as follows: 1. Use the session_name() function to set the session name, such as session_name("my_session"). 2. After setting the session name, call session_start() to start the session. Configuring session names can avoid session data conflicts between multiple applications and enhance security, but pay attention to the uniqueness, security, length and setting timing of session names.

The session ID should be regenerated regularly at login, before sensitive operations, and every 30 minutes. 1. Regenerate the session ID when logging in to prevent session fixed attacks. 2. Regenerate before sensitive operations to improve safety. 3. Regular regeneration reduces long-term utilization risks, but the user experience needs to be weighed.

Setting session cookie parameters in PHP can be achieved through the session_set_cookie_params() function. 1) Use this function to set parameters, such as expiration time, path, domain name, security flag, etc.; 2) Call session_start() to make the parameters take effect; 3) Dynamically adjust parameters according to needs, such as user login status; 4) Pay attention to setting secure and httponly flags to improve security.

The main purpose of using sessions in PHP is to maintain the status of the user between different pages. 1) The session is started through the session_start() function, creating a unique session ID and storing it in the user cookie. 2) Session data is saved on the server, allowing data to be passed between different requests, such as login status and shopping cart content.

How to share a session between subdomains? Implemented by setting session cookies for common domain names. 1. Set the domain of the session cookie to .example.com on the server side. 2. Choose the appropriate session storage method, such as memory, database or distributed cache. 3. Pass the session ID through cookies, and the server retrieves and updates the session data based on the ID.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1
Easy-to-use and free code editor

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.