將 http://localhost/tinyurl.php?v=1 導向 http://www.wretch.cc/blog/showyounet
或是 http://localhost/tinyurl.php?v=2 導向 http://localhost/share/firefox_install.exe
若轉址失敗會導向 $o_url,下面範例是給 index.php。
ps, 轉址失敗是指內建的 $s_file (轉址對照表)找不到對應的資料。
轉址對照表的格式(假設 $separator 是 "—"):
1—http://www.wretch.cc/blog/showyounet—0— 2—http://localhost/share/firefox_install.exe—0— |
我自己寫的php源碼(可能有點差勁):
/* following is the php source code */ /* $_GET['v'] is the value v of the url */ $s_file = "database"; /* source of the file */ $o_url = "index.php"; /* if fail to match then go to this url */ $separator = "—"; /* for separator identify */ if(isset($_GET['v'])){ $v = $_GET['v']; $exist = file_exists ( $s_file ) ; if ( $exist ) { $file = file($s_file); $count = count($file); for($i = 0; $i < $count; $i++){ list($id, $value, $click) = explode ( $separator , $file[$i] ) ; /* $id for number, $value for whole url, $click for counter */ if($id == $v){ $sn = $i; /* $sn for the sequence of the file */ $w_file = fopen($s_file, "w"); for($i = 0; $i < $count; $i++){ if($sn == $i){ (int)$click; $click++; fwrite($w_file, $id . $separator . $value . $separator . $click. $separator . "\n"); }else{ fwrite($w_file, $file[$i] . "\n"); } } fclose($w_file); header("Location: " . $value); exit(0); } } } header("Location: " . $o_url); exit(0); }else{ header("Location: " . $o_url); exit(0); } |
0 comments:
Post a Comment