插件45:使用短地址

本文介绍了一个PHP插件的功能,该插件能够接收短地址并返回对应的长地址。通过提供一个示例代码,展示了如何利用此插件实现自动重定向功能。插件需要一个数据文件作为输入,该文件包含短地址与对应长地址的映射关系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?php // Plug-in 45: Use Short URL

// This is an executable example with additional code supplied
// To obtain just the plug-ins please click on the Download link

$token  = "f8c52"; // Replace with a token from Plug-in 44
$file   = "shorturls.txt";
$result = PIPHP_UseShortURL($token, $file);

if ($result)
{
   echo "The short token '$token' redirects to: ";
   echo "<a href='$result'>$result</a><br /><br />";
   echo "To automatically redirect there do not echo ";
   echo "anything, just output the result returned by ";
   echo "this function in a header, like this:<br /><br />";
   echo '<font face="Courier New">header("Location: $result");';
}
else echo "The short token '$token' is unrecognized";

// To turn this Plug-in into a program to redirect short URLs,
// delete the above example code and replace it with the four
// lines of code below, then save this file as, for example,
// go.php.
   
// $file   = "shorturls.txt";
// $result = PIPHP_UseShortURL($_GET['u'], $file);
// if ($result) header("Location: $result");
// else echo "That short URL is unrecognized";

function PIPHP_UseShortURL($token, $file)
{
   // Plug-in 45: Use Short URL
   //
   // This plug-in takes a short tail string as created by
   // Plug-in 44 and returns the original long URL. The
   // arguments required are:
   //
   //    $token: A short tail as supplied by Plug-in 44
   //    $file:  Location of a file containing the data
   //            for the redirects.

   $contents = @file_get_contents($file);
   $lines    = explode("\n", $contents);
   $shorts   = array();
   $longs    = array();

   if (strlen($contents))
      foreach ($lines as $line)
         if (strlen($line))
            list($shorts[], $longs[]) = explode('|', $line);

   if (in_array($token, $shorts))
      for ($j = 0 ; $j < count($longs) ; ++$j)
         if ($shorts[$j] == $token)
            return $longs[$j];

   return FALSE;
}

?>

插件说明:

本插件接受一个短地址,返回相应的URL长地址。他需要以下参数:

$token 短地址,根据它找到相应的长地址。

$file 插件45用到的数据文件

shorturls.txt内容:

37e08|http://pluginphp.com/longfoldername/index.html

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值