<!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>
<p>/* style.css */
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;
}</p>
<p>.editing input {
width: 100%;
margin-bottom: 5px;
}</p>
<p>.editing button {
display: none;
}</p>
<p>.editing .cancel-edit {
display: block;
margin-top: 10px;
}</p>
<p>/* get_urls.php */</p>
<?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\">\n";
		echo "<td>$name</td>\n";
		echo "<td><a href=\"$link\" target=\"_blank\">$link</a></td>\n";
		echo "<td>$note</td>\n";
		echo "<td>\n";
		if ($locked == '0') {
			echo "<button class=\"delete-url\">删除</button>\n";
			echo "<button class=\"edit-url\">编辑</button>\n";
			echo "<button class=\"lock-url\">锁定</button>\n";
		}
		echo "</td>\n";
		echo "</tr>\n";
	}
}
?>
<p>/* script.js */
$(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=&quot;edit-url-form&quot;&gt;';
	edit_form += '&lt;input type=&quot;text&quot; id=&quot;edit-url-name&quot; name=&quot;edit-url-name&quot; value=&quot;' + name + '&quot; required&gt;&lt;br&gt;';
	edit_form += '&lt;input type=&quot;text&quot; id=&quot;edit-url-link&quot; name=&quot;edit-url-link&quot; value=&quot;' + link + '&quot; required&gt;&lt;br&gt;';
	edit_form += '&lt;input type=&quot;text&quot; id=&quot;edit-url-note&quot; name=&quot;edit-url-note&quot; value=&quot;' + note + '&quot;&gt;&lt;br&gt;';
	edit_form += '&lt;button type=&quot;submit&quot;&gt;保存&lt;/button&gt;';
	edit_form += '&lt;button type=&quot;button&quot; class=&quot;cancel-edit&quot;&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>
<p>/* add_url.php */</p>
<?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\n";
file_put_contents($urls_file, $url_data, FILE_APPEND);

echo 'success';
?>
<p>/* delete_url.php */</p>
<?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("\n", $urls));
		echo 'success';
		exit;
	}
}

echo '未找到对应的网址';
?>
<p>/* edit_url.php */</p>
<?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("\n", $urls));
		echo 'success';
		exit;
	}
}

echo '未找到对应的网址';
?>
<p>/* lock_url.php */</p>
<?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("\n", $urls));
		echo 'success';
		exit;
	}
}

echo '未找到对应的网址';
?>
网址收藏 - 添加、编辑、删除和锁定网址

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

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