开发者

how to create urlshortner by using php

开发者 https://www.devze.com 2023-03-09 04:07 出处:网络
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 af

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 lin开发者_StackOverflow社区k ,,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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消