- 質問・相談ならMSN相談箱 >
- デジタルライフ >
- ソフトウェア >
- フリーウェア
質問させていただきます。
ランダムリンクの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
この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
あなたにおすすめの質問
- Frank Lloyd Wright... Frank Lloyd Wrightの名言
- Matthew Tanについて(歌手... Matthew Tanについて(歌手)
- Matthew subject3 ... Matthew subject3 jujoghrt これってウイルスメール?
- Charles Wright & T... Charles Wright & The Watts 103rd Street Rhythm Band
- \n"; ?> のエラー \n"; ?> のエラー
- style.visibility="... style.visibility="hidden";
- 絵の題名を教えてください!!(Tho... 絵の題名を教えてください!!(Thomas Wrightが描いたもの?)
- String str = "a\\b... String str = "a\\b\\c";
- PTH-intact PTH-intact
- print "Content-typ... print "Content-type: text/html\n"; の行方
Bing ショッピング
今、いちばん売れているノートパソコンは?人気ランキングをチェックしてみよう。
機能はもちろん、デザインにもこだわりたい冬の必需品。Bing ショッピングで見つけよう!