-2

我已经使用链接http://devlup.com/programming/php/create-url-shortener-php/853/创建了 url shortner ,为此我使用的是 EasyPhp5.3.6.0,但我没有找到所需的输出,这意味着在单击缩短的 url 后它不会重定向到原始页面。可能我认为问题出在数据库端这是我在数据库端执行的步骤,请让我知道有什么问题

首先,我转到配置-> PhpMyAdmin 链接,然后我在这里创建了名为“leaf”的数据库,我没有选择名为“Collat​​ion”的下拉列表,我将表名设为“团队”,字段数为“3”,然后我修改了如图所示的字段以下

**Field            id                   url                        shortened**
Type             INT                 VARCHAR                     VARCHAR

Lenght/Values    255                 10000                       10000  
Default          None                None                        None

然后我将“id”设为主键

这些是 php 代码的一部分,其中数据库处理在 sortner.php 中进行

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("leaf", $con); //Replace with your MySQL DB Name
$urlinput=mysql_real_escape_string($_POST['url']); 
$id=rand(10000,99999);
$shorturl=base_convert($id,20,36);
$sql = "insert into team values('$id','$urlinput','$shorturl')";
mysql_query($sql,$con);
echo "Shortened url is <a href=\"http://projects.devlup.com/url/". $shorturl ."\">http://devlup.com/". $shorturl ."</a>";
mysql_close($con);
?>

在decoder.php中

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("leaf", $con); //Replace with your MySQL DB Name


$de= mysql_real_escape_string($_GET["decode"]);



$sql = 'select * from team where shortened="$de"';

$result=mysql_query("select * from team where shortened='$de'");

while($row = mysql_fetch_array($result))
{
$res=$row['url'];
header("location:$res");
}

请让我知道这里有什么问题,我的所有文件都在根文件夹(www)下,这意味着 C:\Program Files\EasyPHP-5.3.6.0\www\test

4

2 回答 2

1

你确定你添加了重写规则吗?

Options +FollowSymLinks -Indexes -MultiViews
RewriteEngine on
#
# Internally rewrite shortened URL requests to de-shortened URL lookup script filepath plus query string
RewriteRule ^([\w\d]{4})$ decoder.php?decode=$1 [L]

检查一下。否则检查只是做

http://yourdomain.com/decoder.php?decode=<URL>

并检查你是否在那里看到任何东西。

另外:一个丑陋但快速的调试方法是添加die('somehing');并继续通过脚本移动它以查看脚本停止的位置

编辑2:

您的脚本可能很简单:

$result = mysql_query("SELECT * FROM team WHERE shortened = '$de' LIMIT 1");
$row = mysql_fetch_array($result);
$res = $row['url'];
header("Location: $res");
exit;

另外,根据您的评论,您的脚本在哪里?它是在文件夹下url还是在根文件夹中?这可以使重写规则起作用或不起作用

编辑1:

还:

  • 添加到查询的末尾LIMIT 1(因为短字符串只有 1 个 url)
  • 您不需要保持迭代,只需调用mysql_fetch_array一次
  • location在该句子之后更改Location并添加exit
于 2011-05-28T19:19:25.783 回答
0

我不确定您的问题详细信息是否已经详细,但如果您认为从数据库获取缩短的 URL 存在问题,那么至少尝试调试您的代码并查看周围发生了什么,例如:

$de= mysql_real_escape_string($_GET["decode"]);

echo "Shortened code: $de \n";

$result = mysql_query("select * from team where shortened='$de'");

while($row = mysql_fetch_array($result))
{
    var_dump($result);
    $res=$row['url'];
    echo "Raw URL: $res \n";
    //header("location:$res");
}

我自己也开发了一个 URL 缩短脚本,分发在这里。我对如何制作它非常了解,所以如果您提供足够的信息,我可能会为您提供帮助。你可以试试我的演示:http ://trisle.net/u/

希望我能帮上忙。

于 2011-05-28T21:10:49.260 回答