如何使用 php 创建 urlshortner
我使用链接 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定添加了重写规则吗?
检查一下。否则检查一下正在做的事情
并检查是否在那里看到任何东西。
另外:一种丑陋但快速的调试方法是添加
die('somehin');
并继续在脚本中移动它以查看脚本停止的位置编辑 2:
您的脚本可以像这样简单:
另外,根据您的评论,您的脚本在哪里?是在
url
文件夹下还是在根文件夹中?这可能会使重写规则起作用或不起作用编辑1:
另外:
LIMIT 1
(因为你只有1个短字符串的url)mysql_fetch_array
一次,location
更改为Location
,并在该句后添加exit
are you sure you have the rewrite rule added?
check that. otherwise check just doing
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 stopsEDIT 2:
your script could be simple as this:
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 ruleEDIT 1:
Also:
LIMIT 1
(since you would only have 1 url for the short string)mysql_fetch_array
oncelocation
forLocation
and addexit
after that sentence我不确定您的问题细节是否已经详细,但如果您认为这是从数据库获取缩短的 URL 的问题,那么至少尝试调试您的代码并查看周围发生的情况,例如:
我还开发了我自己的一个 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:
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.