ランダムリンクCGIをPHPへ

質問させていただきます。
ランダムリンクのCGIがあるのですがこれを機能は変えず同じ動作をするPHPに変えたいのです。以下にCGIの内容を書きます。
 ↓
ファイル名 rand_link.pl

#!/usr/local/bin/perl
##############################################################################
# Random Link Version 1.0 #
# Copyright 1996 Matt Wright mattw@scriptarchive.com #
# Created 7/15/95 Last Modified 7/30/95 #
# Scripts Archive at: http://www.scriptarchive.com/ #
##############################################################################
# COPYRIGHT NOTICE #
# Copyright 1996 Matthew M. Wright All Rights Reserved. #
# #
# Random Link may be used and modified free of charge by anyone so long as #
# this copyright notice and the comments above remain intact. By using this #
# code you agree to indemnify Matthew M. Wright from any liability that #
# might arise from it's use. #
# #
# Selling the code for this program without prior written consent is #
# expressly forbidden. In other words, please ask first before you try and #
# make money off of my program. #
# #
# Obtain permission before redistributing this software over the Internet or #
# in any other medium. In all cases copyright and header must remain intact.#
##############################################################################
# Define Variables

$linkfile = "database.txt";

# Options
$uselog = 1; # 1 = YES; 0 = NO
$logfile = "/home/scriptarchive/demos/links/rand_log";

$date = `date +"%D"`; chop($date);

# Done
##############################################################################

open (LINKS, "$linkfile");

srand(); # kick rand
$nlines=@file=<LINKS>; # inhale file & get # of lines
print "Location: $file[int rand $nlines]\n\n"; # print a random line

close (LINKS);

if ($uselog eq '1') {
open (LOG, ">>$logfile");
print LOG "$ENV{'REMOTE_HOST'} - [$date]\n";
close (LOG);
}

exit;


database.txtにhttp://www.gac.jp/などを1行ずつ改行で書き、HTMLに<a href="rand_link/rand_link.pl">★ </a>を埋め込みクリックのたびにランダムリンクさせる。(携帯対応)

PHP改造の知識がまったくないので自分ではどうにもできないです、、どうかよろしくお願いします。

投稿日時 - 2007-09-23 19:46:38

QNo.3370321

すぐに回答ほしいです

このQ&Aは役に立ちましたか?

5人が「このQ&Aが役に立った」と投票しています

[  前へ  |  次へ ]

回答(1件中 1~1件目)

ANo.1

とりあえずランダムリンクの処理だけですが。(ログの処理はしていません)
link.phpとしています。
index.htmlに<a href="link.php">★</a>と記述。

link.php
<?php
$linkfile = "./database.txt"; //読み込むファイル
$link = "./index.html"; //デフォルトのリンク

//ファイルの存在チェック
if (file_exists($linkfile)) {
$lines = @file($linkfile);//ファイルを配列に読み込む
$link_temp = rtrim($lines[array_rand($lines)]);//ランダムで1件取得して行末の改行を削除
$link = ($link_temp)? $link_temp: $link;//空の場合はデフォルト
}
header("location:".$link);
?>

ファイルを読み込む際に「file」関数を使っています。
これはdatabase.txtのファイルサイズがある程度のサイズまで(10MBくらいかな)だったら問題ないと思います。
これより大きくなる場合は、「fopen」などの関数で読み込むほうがいいです。

投稿日時 - 2007-09-24 19:25:14

お礼

回答してくださってありがとうございます。
みごと動作いたしました。
「fopen」関数はすこし自分でもやってみようと思います。
今回はありがとうございました。

投稿日時 - 2007-09-25 03:10:46

あなたにおすすめの質問

Bing ショッピング

ノートPC人気ランキング

ノートPC人気ランキング

今、いちばん売れているノートパソコンは?人気ランキングをチェックしてみよう。

デザインで選ぶ加湿器

デザインで選ぶ加湿器

機能はもちろん、デザインにもこだわりたい冬の必需品。Bing ショッピングで見つけよう!

[PR] お役立ち情報