按123优化代码1 在addphp中没有对用户输入进行足够的验证可能会导致安全问题。比如可能会得到恶意的输入例如包含脚本或恶意代码的URL。可以使用更强的过滤和验证例如使用正则表达式和其他过滤器。2 在deletephp中$_POSTid应该是$_POSTids因为在mainjs中我们使用的是$inputtype=checkboxcheckedserialize它将所有选中的checkbox的ID
<p>文件名:index.html</p>
<!DOCTYPE html>
<html>
<head>
<title>网址收藏夹</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<h1>网址收藏夹</h1>
<form>
<label for="name">网址名:</label>
<input type="text" id="name" name="name" required>
<pre><code> <label for="url">URL:</label>
<input type="url" id="url" name="url" required>
<label for="status">状态:</label>
<select id="status" name="status">
<option value="正常">正常</option>
<option value="异常">异常</option>
<option value="未知">未知</option>
</select>
<label for="password">密码:</label>
<input type="password" id="password" name="password">
<button type="submit">添加</button>
<button id="delete">删除</button>
<button id="lock">锁定</button>
<button id="unlock">解锁</button>
<input type="text" id="search" placeholder="搜索">
</form>
</header>
<main>
<table>
<thead>
<tr>
<th>网址名</th>
<th>URL</th>
<th>状态</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<!-- 网址信息通过PHP读取txt文件动态生成 -->
</tbody>
</table>
</main>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="main.js"></script>
</code></pre>
</body>
</html>
<p>文件名:main.js</p>
<p>$(document).ready(function() {
// 添加网址
$('form').submit(function(event) {
event.preventDefault();
let name = $('#name').val();
let url = $('#url').val();
let status = $('#status').val();
let password = $('#password').val();
if (url.trim() === '') {
alert('URL不能为空');
return;
}
$.ajax({
type: 'POST',
url: 'add.php',
data: {name: name, url: url, status: status, password: password},
success: function(response) {
$('tbody').append(response);
$('form')[0].reset();
}
});
});</p>
<pre><code>// 删除网址
$('#delete').click(function() {
let ids = $('input[type="checkbox"]:checked').map(function() {
return $(this).val();
}).get();
if (ids.length === 0) {
alert('请选择要删除的网址');
return;
}
$.ajax({
type: 'POST',
url: 'delete.php',
data: {ids: ids},
success: function(response) {
$('input[type="checkbox"]:checked').closest('tr').remove();
}
});
});
// 锁定网址
$('#lock').click(function() {
let ids = $('input[type="checkbox"]:checked').map(function() {
return $(this).val();
}).get();
if (ids.length === 0) {
alert('请选择要锁定的网址');
return;
}
$.ajax({
type: 'POST',
url: 'lock.php',
data: {ids: ids},
success: function(response) {
$('input[type="checkbox"]:checked').closest('tr').find('.status').text('锁定');
}
});
});
// 解锁网址
$('#unlock').click(function() {
let password = prompt('请输入密码');
let ids = $('input[type="checkbox"]:checked').map(function() {
return $(this).val();
}).get();
if (ids.length === 0) {
alert('请选择要解锁的网址');
return;
}
$.ajax({
type: 'POST',
url: 'unlock.php',
data: {password: password, ids: ids},
success: function(response) {
if (response == 'success') {
$('input[type="checkbox"]:checked').closest('tr').find('.status').text('正常');
} else {
alert('密码错误');
}
}
});
});
// 搜索网址
$('#search').keyup(function() {
let keyword = $(this).val().toLowerCase();
$('tbody tr').hide().filter(function() {
return $(this).text().toLowerCase().indexOf(keyword) > -1;
}).show();
});
</code></pre>
<p>});</p>
<p>文件名:style.css</p>
<ul>
<li>{
box-sizing: border-box;
}</li>
</ul>
<p>body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}</p>
<p>header {
background-color: #f2f2f2;
padding: 20px;
}</p>
<p>main {
max-width: 800px;
margin: 20px auto;
}</p>
<p>table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}</p>
<p>th, td {
border: 1px solid #ccc;
padding: 10px;
text-align: center;
}</p>
<p>th {
background-color: #f2f2f2;
}</p>
<p>tr:nth-child(even) {
background-color: #f2f2f2;
}</p>
<p>input[type="text"], input[type="url"], select, input[type="password"] {
display: block;
width: 100%;
padding: 8px;
margin: 10px 0;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 16px;
}</p>
<p>input[type="checkbox"] {
margin: 10px;
}</p>
<p>button[type="submit"], button {
background-color: #4CAF50;
color: white;
padding: 8px 16px;
margin: 10px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
}</p>
<p>button[type="submit"]:hover, button:hover {
background-color: #3e8e41;
}</p>
<p>#search {
float: right;
width: 200px;
height: 30px;
margin-top: -32px;
padding: 5px;
border-radius: 5px;
border: 1px solid #ccc;
font-size: 16px;
}</p>
<p>.hide {
display: none;
}</p>
<p>文件名:add.php</p>
<?php
// 过滤用户输入
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
$url = filter_input(INPUT_POST, 'url', FILTER_VALIDATE_URL);
$status = filter_input(INPUT_POST, 'status', FILTER_SANITIZE_STRING);
$password = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING);
// 检查URL是否合法
if (!$url) {
echo 'URL不合法';
exit;
}
// 保存网址信息到txt文件
$file = fopen('data.txt', 'a');
fwrite($file, "$name\t$url\t$status\t$password\n");
fclose($file);
// 动态添加网址信息到表格
echo "<tr>\n";
echo "<td>$name</td>\n";
echo "<td><a href=\"$url\" target=\"_blank\">$url</a></td>\n";
echo "<td class=\"status\">$status</td>\n";
echo "<td><input type=\"checkbox\" value=\"$id\"></td>\n";
echo "</tr>\n";
?>
<p>文件名:delete.php</p>
<?php
// 读取txt文件中的网址信息
$file = file('data.txt');
// 获取要删除的网址ID
$ids = explode(',', $_POST['ids']);
// 逐行检查网址信息
foreach ($file as $key => $line) {
// 获取网址信息的ID
$id = $key + 1;
// 如果是要删除的网址,则将其从txt文件中删除
if (in_array($id, $ids)) {
unset($file[$key]);
}
}
// 将更新后的网址信息重新写入txt文件
file_put_contents('data.txt', implode('', $file));
?>
<p>文件名:lock.php</p>
<?php
// 读取txt文件中的
原文地址: https://www.cveoy.top/t/topic/bnUj 著作权归作者所有。请勿转载和采集!