php域验证
我需要一些帮助,因为我面临两个问题 没有域扩展名的链接.com、.net、ect将存储在 作为单个词的数据库 脚本允许自缩短短url,这是一个主要的 问题 我怎么能 检查域扩展,否则无法提交 及 检查用户是否试图缩短我自己的链接并失败 我的代码:php域验证,php,url-shortener,Php,Url Shortener,我需要一些帮助,因为我面临两个问题 没有域扩展名的链接.com、.net、ect将存储在 作为单个词的数据库 脚本允许自缩短短url,这是一个主要的 问题 我怎么能 检查域扩展,否则无法提交 及 检查用户是否试图缩短我自己的链接并失败 我的代码: function remove_http($url) { $disallowed = array('http://', 'https://', 'http//', 'https//'); foreach($disallowed as $d)
function remove_http($url)
{
$disallowed = array('http://', 'https://', 'http//', 'https//');
foreach($disallowed as $d) {
if(strpos($url, $d) === 0) {
return str_replace($d, '', $url);
}
}
return $url;
}
$url_to_shorten = get_magic_quotes_gpc() ? stripslashes(trim($_REQUEST['url'])) : trim($_REQUEST['url']);
if(!empty($url_to_shorten) || parse_url($url_to_shorten, PHP_URL_SCHEME) )
{
require('framework/core/config.xml.php');
// check if the URL has already been shortened
$already_shortened = mysql_result(mysql_query('SELECT id FROM ' . DB_TABLE. ' WHERE long_url="' . mysql_real_escape_string(remove_http($url_to_shorten)) . '"'), 0);
if(!empty($already_shortened))
{
// URL has already been shortened
$shortened_url = getShortenedURLFromID($already_shortened);
}
else
{
// URL not in database, insert
mysql_query('LOCK TABLES ' . DB_TABLE . ' WRITE;');
mysql_query('INSERT INTO ' . DB_TABLE . ' (long_url, created, creator) VALUES ("' . mysql_real_escape_string(remove_http($url_to_shorten)) . '", "' . time() . '", "' . mysql_real_escape_string($_SERVER['REMOTE_ADDR']) . '")');
$shortened_url = getShortenedURLFromID(mysql_insert_id());
mysql_query('UNLOCK TABLES');
}
echo BASE_HREF . $shortened_url;
}
function getShortenedURLFromID ($integer, $base = ALLOWED_CHARS)
{
$length = strlen($base);
while($integer > $length - 1)
{
$out = $base[fmod($integer, $length)] . $out;
$integer = floor( $integer / $length );
}
return $base[$integer] . $out;
}
使用parse_url函数。我不确定这里的问题是什么。你的代码不工作吗?@Jack parse_url$url_to_shorten,PHP_url_方案无法验证,因此,如果你提交http://lol,函数将删除http并将lol添加到SQL中,但代码无法确保其lol.tld不仅仅是wortd。以及防止我自己的链接被一次又一次地缩短的方法