如何使用 php 创建 urlshortner

发布于 2024-11-10 10:07:09 字数 1903 浏览 2 评论 0原文

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

首先我转到配置->PhpMyAdmin 链接,然后我在这里创建了名为“leaf”的数据库,我没有选择名为下拉列表的“排序规则”,我创建了表名作为“团队”,字段数为“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

I have created url shortner by using the link http://devlup.com/programming/php/create-url-shortener-php/853/ for this i am using EasyPhp5.3.6.0,,but i am not finding the required output,,that means after clicking shortened url it is not redirecting to the original page.Possibly i am thinking the issue is on database side
This is the steps which i did in database side,please let me know anything wrong

First i went to Configuration->PhpMyAdmin link ,,Then i created Database named "leaf" here i didnt selected 'Collation' named dropdown i made table name as "team" with number of fields as "3" then i modified fields like shown below

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

Lenght/Values    255                 10000                       10000  
Default          None                None                        None

Then i made 'id' as Primary Key

These are some part of php code where database handling going one
in 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);
?>

In 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");
}

Kindly let me know anything is wrong here,,My all files are under root folder(www) that means C:\Program Files\EasyPHP-5.3.6.0\www\test

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请帮我爱他 2024-11-17 10:07:09

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

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('somehin'); 并继续在脚本中移动它以查看脚本停止的位置

编辑 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

are you sure you have the rewrite rule added?

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]

check that. otherwise check just doing

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

and check if you see anything there.

also: an ugly but fast way to debug is add die('somehing'); and keep moving it trough the script to see where the script stops

EDIT 2:

your script could be simple as this:

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

Also, from your comments, where is your script? is it under the folder url or in the root folder? that could make work or not the rewrite rule

EDIT 1:

Also:

  • add to the end of the query LIMIT 1 (since you would only have 1 url for the short string)
  • you don't need to keep iteration, just call mysql_fetch_array once
  • change location for Location and add exit after that sentence
心凉怎暖 2024-11-17 10:07:09

我不确定您的问题细节是否已经详细,但如果您认为这是从数据库获取缩短的 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/

希望我能提供帮助。

I'm not sure if your problem detail is already detailed, but if you think it is a problem with getting the shortened URL from the database, then at least trying to debug your code and see what's happening around, something like:

$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");
}

I also developed an URL shortener script myself, distributed here. I know quite a lot about how to make it so probably I can help you if you provide enough information. You can try my demo at: http://trisle.net/u/

Hope I can help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文