(修改)
您好,
在下面的表单中,当提交的网址开头有“http://”时,<div class="urlfield"><input name="url" type="url" id="url" maxlength="500"></div>
的字段很好。
但是,如果仅使用“www”提交网址,则无效。在它前面,或既没有“http://”也没有“www。”
如果提交的网址在开头有任何或没有以下内容,我怎样才能使其有效:
http://
www.
http://www.
提前致谢,
约翰
形式:
echo '<div class="submittitle">Submit an item.</div>';
echo '<form action="http://www...com/.../submit2.php" method="post">
<input type="hidden" value="'.$_SESSION['loginid'].'" name="uid">
<div class="submissiontitle"><label for="title">Story Title:</label></div>
<div class="submissionfield"><input name="title" type="title" id="title" maxlength="1000"></div>
<div class="urltitle"><label for="url">Link:</label></div>
<div class="urlfield"><input name="url" type="url" id="url" maxlength="500"></div>
<div class="submissionbutton"><input name="submit" type="submit" value="Submit"></div>
</form>
';
submit2.php:
<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){header('Location: http://www...com/.../submit2.php');}
require_once "header.php";
if (isLoggedIn() == true)
{
$remove_array = array('http://www.', 'http://', 'https://', 'https://www.', 'www.');
$cleanurl = str_replace($remove_array, "", $_POST['url']);
$cleanurl = strtolower($cleanurl);
$cleanurl = preg_replace('/\/$/','',$cleanurl);
$cleanurl = stripslashes($cleanurl);
$title = $_POST['title'];
$uid = $_POST['uid'];
$title = mysql_real_escape_string($title);
$title = stripslashes($title);
$cleanurl = mysql_real_escape_string($cleanurl);
$site1 = 'http://' . $cleanurl;
$displayurl = parse_url($site1, PHP_URL_HOST);
function isURL($url1 = NULL) {
if($url1==NULL) return false;
$protocol = '(http://|https://)';
$allowed = '[-a-z0-9]{1,63}';
$regex = "^". $protocol . // must include the protocol
'(' . $allowed . '\.)'. // 1 or several sub domains with a max of 63 chars
'[a-z]' . '{2,6}'; // followed by a TLD
if(eregi($regex, $url1)==true) return true;
else return false;
}
if(isURL($site1)==true)
mysql_query("INSERT INTO submission VALUES (NULL, '$uid', '$title', '$cleanurl', '$displayurl', NULL)");
else
echo "<p class=\"topicu\">Not a valid URL.</p>\n";
} else {
show_loginform();
}
if (!isLoggedIn())
{
if (isset($_POST['cmdlogin']))
{
if (checkLogin($_POST['username'], $_POST['password']))
{
show_userbox();
} else
{
echo "Incorrect Login information !";
show_loginform();
}
} else
{
show_loginform();
}
} else
{
show_userbox();
}
require_once "footer.php";
?>
答案 0 :(得分:1)
如果没有协议(http://
),浏览器应该考虑网址相对。
删除主机(www ... com)部分,并从以下斜杠开始,使用相对于域的文档根目录的URL。
或者,使用相对于表单所在页面的网址的网址(例如,可以是"../../something/else/submit.php"
。
要动态构建相对URL而不诉诸域根,您可能会发现预定义变量$_SERVER['PHP_SELF']
很有用(具体内容可能因所讨论的Web服务器而异)。
答案 1 :(得分:0)
对我来说看起来很好,但是在几个点回显传递的url的值以查看代码失败的位置。
编辑尝试制作$cleanurl = mysql_real_escape_string($cleanurl);
$cleanurl = mysql_real_escape_string(trim($cleanurl));