先創建一個新聞需要的庫
這樣id值可以逐級遞增
然后隨便寫個值,讓他輸出一下看看
模板引入
但是這樣不夠美觀,這就涉及到了引入html模板
模板引入是html有一個的地方值可以通過php代碼去傳入過去,其他的html界面直接調用,這樣頁面美觀,輸出查詢的結果也可以顯示在上面
大括號括起來的就是
這樣就把php的值傳遞到了前端代碼中顯示的更美觀
模板調用會造成安全問題,例如標題改成
在訪問新聞網站的時候就會執行php代碼
成功輸出了123,這是因為在模板調用中用到哦leval函數,標題傳入到這個函數中并且執行
在調用的html模板寫一個php代碼
也會被執行代碼
為了安全的考慮就有人開發出了第三方模板
mvc模型
這樣就不安全了,怎么樣才能安全
下載好模板第三方插件,smarty
放在網站同一個目錄,以上代碼調用即可
同樣在調用文件寫入一個phpinfo代碼
這就不會允許
最新版本沒有,但是之前版本有過安全問題
復現
<?php
include '../liuyanban/config.php';
$template=file_get_contents('new.html');//讀取文件內容$id=$_GET['id'] ?? '1';
$sql="select * from new where id='$id'";
$result=mysqli_query($con,$sql);
while($row=mysqli_fetch_row($result)){$news_title=$row[1];$news_content=$row[3];$news_image=$row[4];
}$template=str_replace('{news_title}',$news_title,$template);
$template=str_replace('{news_content}',$news_content,$template);
$template=str_replace('{news_image}',$news_image,$template);//替換括號tetle,替換內容為第二個值,替換的對象是第三個值,就上面取得文件內容
eval('?>'.$template);
<!-- news_template.html -->
<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>新聞節目</title><style>body {font-family: Arial, sans-serif;background-color: #f4f4f4;margin: 0;padding: 20px;}.news-container {background-color: #fff;padding: 20px;border-radius: 8px;box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);max-width: 800px;margin: 0 auto;}.news-title {font-size: 24px;font-weight: bold;color: #333;margin-bottom: 10px;}.news-image {max-width: 100%;height: auto;border-radius: 8px;margin-bottom: 20px;}.news-content {font-size: 16px;color: #555;line-height: 1.6;}</style>
</head>
<body>
<div class="news-container"><h1 class="news-title">{news_title}</h1><img class="news-image" src="{news_image}" alt="新聞圖片"><p class="news-content">{news_content}</p>
</div>
</body>
</html>
jian
kang