我在joomal 2.5中通过rsform pro创建了一个表单我想在一个字段中禁止多个url地址我用这个代码
if(isset($_POST['form']['link']))
{
$bad = array('ebooks.com','suprbay.org');
if(in_array($_POST['form']['link'],$bad))
{
$redirectLink = 'index.php?option=com_rsform&formId='.$formId;
$RSadapter->redirect($redirectLink,'this url limited');
}
}
当用户使用"ebooks.com"或"suprbay.org"时,此代码是正确的,但当用户使用"www.suprbay.org"或"http://www.ebooks.com/635228/you-losing-weight/roizen-michael-f-oz-mehmet-c/"或者当使用这个加上所有东西时,这个代码不起作用
当url包含代码工作的主域时,我去了请帮帮我感谢
foreach ($bad as $badsite){
if (stristr($_POST['form']['link'],$badsite)){
$redirectLink = 'index.php?option=com_rsform&formId='.$formId;
$RSadapter->redirect($redirectLink,'this url limited');
break;
}
}
您可以尝试
$value = htmlspecialchars($_POST['form']['link']); // convert special characters
// note that you should'nt use $_POST
// joomla has it's own way to get request variables
// use JFactory::getApplication('site')->input->get('form', array(), 'post', 'array');
// this will return form array which you can work with
foreach($bad as $site)
{
if(strposi($value, $site) !== false)
{
// value contains one of invalid links
}
}
要找到URL的域,需要对其进行解析,从头开始并非易事。有一个有用的PHP函数parse_url,它将为您完成这项工作。
$parse_result = parse_url($_POST['form']['link']);
$link_domains = explode('.', $parse_result['host']);
$length = count($link_domains);
$link_main_domain = $link_domains[$length - 2].'.'.$link_domains[$length - 1];
然后,您只需像以前一样将$link_main_domain
与$bad
数组进行比较。
基本上parse_url
返回(除其他外)完整的域字符串。例如"subdomain.domain.tld"。代码的其余部分只是该域的最后两段。
一个更简单的解决方案是只在链接中搜索你想避开的域,但这并不能解释它出现在URL中而不是主机段中。
记住,你不能确定链接的位置。它可能会将某人转发到"坏"网站,无论其自己的域是什么。