我在网上找到了 [此 URL 缩短代码][1]。
现在我想添加一些东西。如果用户输入来自 bit.ly、TinyURL.com 或 tr.im 的链接,我想向用户抛出错误。我想让它说,“抱歉,我们不缩短短 URL。” 我知道我需要创建一个if
andelse
语句,但我不知道我需要调用什么。
编辑:
非常感谢您的输入!不幸的是,我完全糊涂了。我在哪里放置这些代码建议?在 index.php 的顶部。现在我有以下php代码..
<?php
require_once('config.php');
require_once('functions.php');
if ($url = get_url())
{
$conn = open_connection();
$res = mysql_query("SELECT `id` FROM `urls` WHERE `url`='$url' LIMIT 1", $conn);
if (mysql_num_rows($res))
{
// this URL entry already exists in the database
// get its ID instead
$row = mysql_fetch_object($res);
$id = $row->id;
}
else
{
// a new guy, insert it now and get its ID
mysql_query("INSERT INTO `urls`(`url`) VALUES('$url')", $conn);
$id = mysql_insert_id($conn);
}
// now convert the ID into its base36 instance
$code = base_convert($id, 10, 36);
$shorten_url = "{$config['host']}/$code";
// and beautifully display the shortened URL
$info = sprintf('
<span class="long-url" style="visibility: hidden;">%s</span>
<span style="visibility: hidden">%d</span> <br>
Link to drop:
<a class="shorteen-url" href="%s">%s</a>
<span style="visibility: hidden">%d</span>',
$_GET['url'], strlen($_GET['url']),
$shorten_url, $shorten_url,
strlen($shorten_url));
}
?>
我没有使用$info = sprintf
...也许我应该$info = sprintf
用以下建议之一替换?