您现在的位置是:首页 > cms教程 > 帝国CMS教程帝国CMS教程

帝国CMS检测字段重复教程

怀亦2025-08-12帝国CMS教程已有人查阅

导读帝国CMS检测字段重复,其实这个帝国CMS就自带的,大家可以参考帝国CMS判断标题是否重复的代码,改成你的需要判断的代码就可以了。

帝国CMS检测字段重复,其实这个帝国CMS就自带的,大家可以参考帝国CMS判断标题是否重复的代码,改成你的需要判断的代码就可以了。
但是有些情况帝国自带的标题重复方法达不到要求,下面代码号教大家如何自己手写一个帝国CMS检测字段重复教程。
方法一
HTML 示例代码:
<label for="siteurl">输入网址:</label>
<input type="text" id="siteurl" name="siteurl" oninput="checkSiteUrl()">
<span id="result"></span>
<script>
function checkSiteUrl() {
const siteurl = document.getElementById('siteurl').value;
if (siteurl.trim() === '') return;
// 发送 AJAX 请求到后端
fetch('/check-url', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ siteurl })
})
.then(response => response.json())
.then(data => {
const resultElement = document.getElementById('result');
if (data.exists) {
resultElement.style.color = 'red';
resultElement.textContent = '此网址已存在!';
} else {
resultElement.style.color = 'green';
resultElement.textContent = '此网址可用。';
}
})
.catch(error => console.error('Error:', error));
}
</script>
后端php代码:
<?php
header('Content-Type: application/json');
// 假设这是数据库中的已有 URL 列表
$existing_urls = [
"http:// .qq.com/",
"https://qq.com",
"http://example.com"
];
// 获取前端传来的数据
$input_url = $_POST['siteurl'] ?? '';
if (empty($input_url)) {
echo json_encode(['exists' => false]);
exit;
}
/**
* 对 URL 进行标准化处理
*/
function normalize_url($url) {
// 解析 URL
$parsed_url = parse_url($url);
// 提取域名部分
$host = strtolower($parsed_url['host'] ?? '');
if (substr($host, 0, 4) === ' .') {
$host = substr($host, 4); // 去掉  .
}
// 提取路径部分并去掉多余的斜杠
$path = isset($parsed_url['path']) ? rtrim($parsed_url['path'], '/') : '';
// 返回标准化后的 URL
return [
'host' => $host,
'path' => $path
];
}
/**
* 生成所有可能的 URL 变体
*/
function generate_variations($normalized_url) {
$variations = [];
$protocols = ['http', 'https'];
$host = $normalized_url['host'];
$path = $normalized_url['path'];
foreach ($protocols as $protocol) {
// 不带  .
$variations[] = $protocol . '://' . $host . $path;
// 带  .
$variations[] = $protocol . ':// .' . $host . $path;
}
return $variations;
}
// 标准化输入的 URL
$normalized_input = normalize_url($input_url);
// 生成所有可能的变体
$variations = generate_variations($normalized_input);
// 检查变体是否存在于数据库中
$exists = false;
foreach ($variations as $variation) {
if (in_array($variation, $existing_urls)) {
$exists = true;
break;
}
}
// 返回结果
echo json_encode(['exists' => $exists]);
?>
方法二
1.后台-》管理数据表-》找到title字段-》输入表单替换html代码
找到:
<input type=text name=title value="<?=ehtmlspecialchars(stripSlashes($r[title]))?>" size="60">
替换成:
<input type=text name=title value="<?=ehtmlspecialchars(stripSlashes($r[title]))?>" size="60" onblur="checkTitle()">
在最下面添加如下代码:(JQ库自己加入就进了,这里的链接用的绝对地址)
<script type="text/javascript" src="/js/jquery-1.7.2.min.js"></script>
<script type="text/javascript">function checkTitle()
{
var classid=<?=$_GET[classid]?>;               var id=<?=$_GET[classid]?>;                var tit=document.add.title.value;
console.log("我是标题="+tit);
console.log("我是classid="+classid);
console.log("我是id="+id);
$.ajax({
url: 'ReTitleAjax.php?classid='+classid+'&id='+id+'&title='+tit,
dataType:"json",
cache: false,
error: function(){
alert("检测失败,请重试");
},
success: function(data){                if (data==1){
alert('重复标题');
}                if (data==0){                    //alert('可以添加信息');
console.log("可以添加信息");
}
}
});
}</script>
2.新建一个PHP文件ReTitleAJAX.php添加如下代码到PHP代码中
define('EmpireCMSAdmin','1');
require("../class/connect.php");
require("../class/db_sql.php");
require("../class/functions.php");
require LoadLang("pub/fun.php");
require("../data/dbcache/class.php");
$link=db_connect();
$empire=new mysqlquery();
$editor=1;
//验证用户
$lur=is_login();
$logininid=$lur['userid'];
$loginin=$lur['username'];
$loginrnd=$lur['rnd'];
$loginlevel=$lur['groupid'];
$loginadminstyleid=$lur['adminstyleid'];
$classid=(int)$_GET['classid'];
$id=(int)$_GET['id'];
$title=AddAddsData($_GET['title']);
$where='';
if($id)
{
$where=' and id<>'.$id;
}
//已审核
$num=$empire->gettotal("select count(*) as total from {$dbtbpre}ecms_".$class_r[$classid][tbname]." where title='".addslashes($title)."'".$where." limit 1");
//未审核
if(empty($num))
{
$num=$empire->gettotal("select count(*) as total from {$dbtbpre}ecms_".$class_r[$classid][tbname]."_check where title='".addslashes($title)."'".$where." limit 1");
}
echo json_encode($num)
然后把这个ReTitleAjax.php文件传到管理员目录也就是(/e/admin/目录下就可以了)

本文标签:帝国cms字段 

很赞哦! ()

相关源码

  • 自适应车行汽车租赁二手车行业企业网站模板为汽车租赁与二手车交易场景深度优化,采用PbootCMS内核开发,聚焦车辆展示、租赁流程与服务介绍三大核心模块。响应式布局确保PC与移动端数据实时同步,后台一键管理车辆信息查看源码
  • (自适应响应式)html5蓝色智能水表营销型网站pbootcms模板下载PbootCMS内核开发,为智能水表企业打造的营销型网站解决方案,本模板基于PbootCMS内核开发,为智能水表及相关行业企业设计,采用HTML5+CSS3技术构建,具有响应式布局。查看源码
  • (自适应)英文绿色精密模具零件加工五金零件pbootcms外贸网站模板本模板基于PbootCMS开发,为五金零件、精密模具加工等英文外贸企业设计。采用响应式布局,适配各类移动设备,是五金零件企业开展国际贸易的专业展示平台。查看源码
  • (自适应)绿色宠物门诊兽医行业网站模板该绿色清爽风格网站模板专为宠物门诊与兽医诊所设计,基于PbootCMS内核开发,采用响应式布局确保手机、PC等多端体验一致,帮助宠物医疗机构高效构建线上服务平台。查看源码
  • pbootcms模板(PC+WAP)传媒广告影视公司网站源码基于PbootCMS内核开发的全自适应传媒文化网站模板,为影视公司、广告传媒企业打造,同时支持多行业快速适配。通过替换文字图片即可转换为其他行业网站查看源码
  • (自适应响应式)瓷砖大理石装修建材类网站pbootcms模板html5模板本模板基于PbootCMS开发,针对瓷砖、大理石等建材行业特点进行优化设计。采用响应式布局技术,确保产品展示效果在不同设备上都能合理呈现。模板特别强化了石材纹理的视觉表现力,帮助建材企业更好地展示产品质感。查看源码
分享笔记 (共有 篇笔记)
验证码: