dongqian0763 2012-05-31 08:46
浏览 7
已采纳

PHP - 最佳实践 - 网站模板[关闭]

I just wanted to see if the way I am constructing my website templates was the best and most dynamic way of doing it. Currently I have my website playout however for the areas that I want the content to change I use the following code.

<? 
 if (preg_match("/\W/",$page) || !file_exists("../$page.html")) {
  include("../home.html");
 }
 else {
  include("../$page.html");
 }
?>

The navigation then links to that page by saying something like,

<a href="index.php?page=about">About</a>

Is this the best way to construct a template using PHP or are there better ways? Thankyou for the help.

  • 写回答

2条回答 默认 最新

  • doubianyan9749 2012-05-31 09:02
    关注

    There is no best or most dynamic way of creating website templates.

    All you can do is learn from experience and others.

    Your code is one way to switch between templates. Some immediate improvements could be:

    • Use && instead of || because of short circuiting properties of the AND operator. If the page does not match then there is no need to do the file check

    • Use a array lookup table to map page value to filename template (e.g. 'home'->'homepage.html' etc.), mainly this helps in preventing serious exploits where external input is used for file paths.

    • Don't file check because why have a link with a non existent template?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)