所以,我是PHP和SQL的初學者。我正在創建一個簡單的博客。我有index.php,在那裏發佈博客和blog.php,它顯示博客文章。我希望能夠在blog.php上的每篇博文中添加一個編輯鏈接。一旦你點擊這個編輯鏈接,你可以在另一個頁面上編輯博客。我想知道什麼是最安全的方法,而不使用框架。這裏是blog.php的代碼:安全地編輯博客文章
<?php include("session_start.php")?>
<?php
$sql = "SELECT * FROM posts WHERE user_name='$user_name' ORDER BY post_date DESC";
$result = mysqli_query($connection, $sql) or die(mysqli_error($connection));
?>
<html>
<head>
<title>Blog</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" >
<link rel="stylesheet" href="styles.css" >
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="align-right">
<a href='index.php'>Post a Blog</a> | <a href='account_settings.php'>Account Settings</a> | <a href='logout.php' onclick="return confirm('Are you sure?')">Logout</a>
</div>
<h1>Blog</h1>
<?php
while($column = mysqli_fetch_assoc($result)){
?>
<h2><?php echo $column["post_title"]?></h2>
<div class="view-post-content"><?php echo $column["post_content"]?></div>
<p class="category">Category: <?php echo $column["post_category"]?></p>
<p>This post was written <?php echo $column["post_date"]?>.</p>
<?php
}
?>
</div>
</div>
</body>
</html>
<?php include "footer.php";?>
你在你的PHP應用程序的任何類型的用戶身份驗證設置的?這可能是第一個開始的地方。在博客文章的每個請求中,PHP會驗證用戶是誰,他說他是誰,他有權讀取/編輯/刪除給定的帖子等。 –
如果我們談論「安全的方式」,那麼它的一個廣泛的問題。但如果你只需要編輯博客文章,只需發送博客文章ID到編輯頁面,並通過其ID從數據庫獲取博客數據。 – arisalsaila
但是不發送博客ID有點不安全,因爲其他人可以猜測這些ID訪問頁面? – Julian