<!DOCTYPE html>
<html>
<head>
	<title>网址收藏 - 在线书签管理工具</title>
	<link rel='stylesheet' type='text/css' href='style.css'>
	<script src='https://code.jquery.com/jquery-3.6.0.min.js'></script>
	<script src='script.js'></script>
</head>
<body>
	<h1>网址收藏 - 在线书签管理工具</h1>
	<div id='add-form'>
		<h2>添加网址</h2>
		<form id='add-url-form'>
			<label for='url-name'>名称</label>
			<input type='text' id='url-name' name='url-name' required><br>
			<label for='url-link'>网址</label>
			<input type='text' id='url-link' name='url-link' required><br>
			<label for='url-note'>备注</label>
			<input type='text' id='url-note' name='url-note'><br>
			<button type='submit'>添加</button>
		</form>
	</div>
	<div id='urls'>
		<h2>已添加的网址</h2>
		<table>
			<thead>
				<tr>
					<th>名称</th>
					<th>网址</th>
					<th>备注</th>
					<th>操作</th>
				</tr>
			</thead>
			<tbody>
				<?php include 'get_urls.php'; ?>
			</tbody>
		</table>
	</div>
</body>
</html>
<pre><code>
style.css:

</code></pre>
<p>body {
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
}</p>
<p>h1 {
text-align: center;
margin: 20px 0;
}</p>
<p>#add-form {
margin: 20px;
padding: 20px;
border: 1px solid #ccc;
}</p>
<p>#add-form h2 {
margin-top: 0;
}</p>
<p>form label {
display: inline-block;
width: 60px;
}</p>
<p>form input {
display: inline-block;
width: 200px;
margin-bottom: 10px;
}</p>
<p>form button {
display: block;
margin-top: 10px;
}</p>
<p>#urls {
margin: 20px;
}</p>
<p>table {
border-collapse: collapse;
width: 100%;
}</p>
<p>th, td {
padding: 10px;
text-align: left;
border-bottom: 1px solid #ccc;
}</p>
<p>th {
background-color: #eee;
}</p>
<p>td:last-child {
text-align: center;
}</p>
<p>.locked {
background-color: #ccc;
}</p>
<p>.editing td {
padding: 0;
border-bottom: none;
}</p>
<p>.editing input {
width: 100%;
}</p>
<p>.editing button {
display: none;
}</p>
<pre><code>
get_urls.php:

</code></pre>
<?php
$urls_file = 'urls.txt';

if (file_exists($urls_file)) {
	$urls = file($urls_file);
	$urls = array_map('trim', $urls);

	foreach ($urls as $url) {
		$url_parts = explode('|', $url);
		$name = $url_parts[0];
		$link = $url_parts[1];
		$note = isset($url_parts[2]) ? $url_parts[2] : '';
		$locked = isset($url_parts[3]) ? $url_parts[3] : '0';
		$locked_class = $locked == '1' ? 'locked' : '';
		$id = md5($name . $link . $note);

		echo '<tr id='$id' class='$locked_class'>';
		echo '<td>$name</td>';
		echo '<td><a href='$link' target='_blank'>$link</a></td>';
		echo '<td>$note</td>';
		echo '<td>';
		if ($locked == '0') {
			echo '<button class='delete-url'>删除</button>';
			echo '<button class='edit-url'>编辑</button>';
			echo '<button class='lock-url'>锁定</button>';
		}
		echo '</td>';
		echo '</tr>';
	}
}
?>
<pre><code>
script.js:

</code></pre>
<p>$(function() {
// 添加网址
$('#add-url-form').submit(function(e) {
e.preventDefault();</p>
<pre><code>	var name = $('#url-name').val();
	var link = $('#url-link').val();
	var note = $('#url-note').val();

	$.post('add_url.php', {name: name, link: link, note: note}, function(data) {
		if (data == 'success') {
			location.reload();
		} else {
			alert(data);
		}
	});
});

// 删除网址
$(document).on('click', '.delete-url', function() {
	var tr = $(this).closest('tr');
	var id = tr.attr('id');

	$.post('delete_url.php', {id: id}, function(data) {
		if (data == 'success') {
			tr.remove();
		} else {
			alert(data);
		}
	});
});

// 编辑网址
$(document).on('click', '.edit-url', function() {
	var tr = $(this).closest('tr');
	var id = tr.attr('id');
	var name = tr.find('td:first-child').text();
	var link = tr.find('td:nth-child(2) a').attr('href');
	var note = tr.find('td:nth-child(3)').text();

	var edit_form = '&lt;form id='edit-url-form'&gt;';
	edit_form += '&lt;label for='edit-url-name'&gt;名称&lt;/label&gt;';
	edit_form += '&lt;input type='text' id='edit-url-name' name='edit-url-name' value='' + name + '' required&gt;&lt;br&gt;';
	edit_form += '&lt;label for='edit-url-link'&gt;网址&lt;/label&gt;';
	edit_form += '&lt;input type='text' id='edit-url-link' name='edit-url-link' value='' + link + '' required&gt;&lt;br&gt;';
	edit_form += '&lt;label for='edit-url-note'&gt;备注&lt;/label&gt;';
	edit_form += '&lt;input type='text' id='edit-url-note' name='edit-url-note' value='' + note + ''&gt;&lt;br&gt;';
	edit_form += '&lt;button type='submit'&gt;保存&lt;/button&gt;';
	edit_form += '&lt;button type='button' class='cancel-edit'&gt;取消&lt;/button&gt;';
	edit_form += '&lt;/form&gt;';

	tr.html(edit_form);
	tr.addClass('editing');

	$('#edit-url-form').submit(function(e) {
		e.preventDefault();

		var edit_name = $('#edit-url-name').val();
		var edit_link = $('#edit-url-link').val();
		var edit_note = $('#edit-url-note').val();
		var password = prompt('请输入密码');

		$.post('edit_url.php', {id: id, name: edit_name, link: edit_link, note: edit_note, password: password}, function(data) {
			if (data == 'success') {
				location.reload();
			} else {
				alert(data);
			}
		});
	});

	$(document).on('click', '.cancel-edit', function() {
		location.reload();
	});
});

// 锁定网址
$(document).on('click', '.lock-url', function() {
	var tr = $(this).closest('tr');
	var id = tr.attr('id');
	var locked = tr.hasClass('locked') ? '0' : '1';
	var password = prompt('请输入密码');

	$.post('lock_url.php', {id: id, locked: locked, password: password}, function(data) {
		if (data == 'success') {
			location.reload();
		} else {
			alert(data);
		}
	});
});
</code></pre>
<p>});</p>
<pre><code>
add_url.php:

</code></pre>
<?php
$urls_file = 'urls.txt';

if (!isset($_POST['name']) || !isset($_POST['link'])) {
	echo '缺少参数';
	exit;
}

$name = trim($_POST['name']);
$link = trim($_POST['link']);
$note = isset($_POST['note']) ? trim($_POST['note']) : '';
$id = md5($name . $link . $note);

if (file_exists($urls_file)) {
	$urls = file($urls_file);
	$urls = array_map('trim', $urls);

	foreach ($urls as $url) {
		$url_parts = explode('|', $url);
		$existing_id = md5($url_parts[0] . $url_parts[1] . (isset($url_parts[2]) ? $url_parts[2] : ''));

		if ($existing_id == $id) {
			echo '已存在相同的网址';
			exit;
		}
	}
}

$url_data = '$name|$link|$note|0
';
file_put_contents($urls_file, $url_data, FILE_APPEND);

echo 'success';
?>
<pre><code>
delete_url.php:

</code></pre>
<?php
$urls_file = 'urls.txt';

if (!isset($_POST['id'])) {
	echo '缺少参数';
	exit;
}

$id = $_POST['id'];

if (!file_exists($urls_file)) {
	echo '文件不存在';
	exit;
}

$urls = file($urls_file);
$urls = array_map('trim', $urls);

foreach ($urls as $index => $url) {
	$url_parts = explode('|', $url);
	$existing_id = md5($url_parts[0] . $url_parts[1] . (isset($url_parts[2]) ? $url_parts[2] : ''));

	if ($existing_id == $id) {
		unset($urls[$index]);
		file_put_contents($urls_file, implode('
', $urls));
		echo 'success';
		exit;
	}
}

echo '未找到对应的网址';
?>
<pre><code>
edit_url.php:

</code></pre>
<?php
$urls_file = 'urls.txt';

if (!isset($_POST['id']) || !isset($_POST['name']) || !isset($_POST['link']) || !isset($_POST['note']) || !isset($_POST['password'])) {
	echo '缺少参数';
	exit;
}

$id = $_POST['id'];
$name = trim($_POST['name']);
$link = trim($_POST['link']);
$note = trim($_POST['note']);
$password = $_POST['password'];
$locked = false;

if (!file_exists($urls_file)) {
	echo '文件不存在';
	exit;
}

$urls = file($urls_file);
$urls = array_map('trim', $urls);

foreach ($urls as $index => $url) {
	$url_parts = explode('|', $url);
	$existing_id = md5($url_parts[0] . $url_parts[1] . (isset($url_parts[2]) ? $url_parts[2] : ''));

	if ($existing_id == $id) {
		if (isset($url_parts[3]) && $url_parts[3] == '1') {
			$locked = true;
		}

		if ($locked) {
			echo '该网址已被锁定';
			exit;
		}

		if ($password != 'password') {
			echo '密码错误';
			exit;
		}

		$urls[$index] = '$name|$link|$note|' . (isset($url_parts[3]) ? $url_parts[3] : '0');
		file_put_contents($urls_file, implode('
', $urls));
		echo 'success';
		exit;
	}
}

echo '未找到对应的网址';
?>
<pre><code>
lock_url.php:

</code></pre>
<?php
$urls_file = 'urls.txt';

if (!isset($_POST['id']) || !isset($_POST['locked']) || !isset($_POST['password'])) {
	echo '缺少参数';
	exit;
}

$id = $_POST['id'];
$locked = $_POST['locked'];
$password = $_POST['password'];

if (!file_exists($urls_file)) {
	echo '文件不存在';
	exit;
}

$urls = file($urls_file);
$urls = array_map('trim', $urls);

foreach ($urls as $index => $url) {
	$url_parts = explode('|', $url);
	$existing_id = md5($url_parts[0] . $url_parts[1] . (isset($url_parts[2]) ? $url_parts[2] : ''));

	if ($existing_id == $id) {
		if ($password != 'password') {
			echo '密码错误';
			exit;
		}

		$urls[$index] = '$url_parts[0]|$url_parts[1]|' . (isset($url_parts[2]) ? $url_parts[2] : '') . '|$locked';
		file_put_contents($urls_file, implode('
', $urls));
		echo 'success';
		exit;
	}
}

echo '未找到对应的网址';
?>
<pre><code></code></pre>
网址收藏 - 在线书签管理工具

原文地址: https://www.cveoy.top/t/topic/mPvP 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录