プラグイン44:URLブレークアドレスの確立
<?php // Plug-in 44: Create Short URL
// This is an executable example with additional code supplied
// To obtain just the plug-ins please click on the Download link
$long = "http://pluginphp.com/longfoldername/index.html";
$redirect = "go.php";
$len = 5;
$file = "shorturls.txt";
$result = PIPHP_CreateShortURL($long, $redirect, $len, $file);
echo "The URL '$long' can now be accessed from: ";
echo "<a href='/$result'>/$result</a>";
function PIPHP_CreateShortURL($url, $redirect, $len, $file)
{
// Plug-in 44: Create Short URL
//
// This plug-in takes a long URL and shortens it. The
// arguments required are:
//
// $url: A URL including the preceding http://
// $redirect: A PHP page on the server to use for URL
// redirection, It should have a short
// name, such as /go.php.
// $len: The number of characters to use in the
// short URL's tail.
// $file: Location of a file containing the data
// for the redirects.
$contents = @file_get_contents($file);
$lines = explode("
", $contents);
$shorts = array();
$longs = array();
if (strlen($contents))
foreach ($lines as $line)
if (strlen($line))
list($shorts[], $longs[]) = explode('|', $line);
if (in_array($url, $longs))
for ($j = 0 ; $j < count($longs) ; ++$j)
if ($longs[$j] == $url) return $redirect .
"?u=" . $shorts[$j];
do $str = substr(md5(rand(0, 1000000)), 0, $len);
while (in_array($str, $shorts));
file_put_contents($file, $contents . $str . '|' . $url .
"
");
return $redirect . "?u=$str";
}
?>
プラグインの説明:
プラグインは、簡略化が必要なURLアドレスとその他のデータを受け入れ、URLブレークアドレスを返します.彼は以下のパラメータが必要です.
$url:簡略化されたURLアドレスが必要
$redirect:サーバの前のPHPプログラムの名前で、URLの短いアドレスを元のターゲットアドレスにリダイレクトします.
$len:URLの短いアドレスに表示される文字数です.この数値が大きいほど、サポートされるURLアドレスが多くなります.例えば3文字で4096個のURLアドレスをサポートすることができ、
本カードは16進数の0~0とa~fの計16文字を使用する
$file URLの短いアドレスを保存するファイル名.