Php - 点击计数器并转发到另一个网页

时间:2015-02-18 14:29:01

标签: php counter forward

我有以下脚本,我正在尝试实现此目的:当用户点击链接时,它会激活计数器,然后将访问者转发到另一个网站。它转发到其他网站就好了,但它没有增加计数器。如果我向前推进,它工作正常,所以我的问题是如何实现点击,计算,转发到另一个网站?

    <?php 

/** 
 * Create an empty text file called couponsbyemailB.txt and  
 * upload to the same directory as the page you want to  
 * count hits for. 
 *  
 * Add this line of code on your page: 
 * <?php include "freecouponsbyemailB.php"; ?> 
 */ 

// Open the file for reading -- tracks by banner clicked
$fp = fopen("couponsbyemailB.txt", "r"); 

// Get the existing count 
$count = fread($fp, 1024); 

// Close the file 
fclose($fp); 

// Add 1 to the existing count 
$count = $count + 1; 

// Display the number of hits 
// If you don't want to display it, comment out this line 
//echo "<p>Page views:" . $count . "</p>"; 

// Reopen the file and erase the contents -- tracks by banner clicked
$fp = fopen("couponsbyemailB.txt", "w"); 

// Write the new count to the file 
fwrite($fp, $count); 

// Close the file 
fclose($fp); 

header('Location: http://www.inboxdollars.com/?r=ref18222798'); // redirect to new web page

?> 

1 个答案:

答案 0 :(得分:1)

更新计数器可以更容易:

$count = file_get_contents("couponsbyemailB.txt");
$count = $count + 1;
file_put_contents("couponsbyemailB.txt", $count);

请记住,刷新页面不会再次触发计数器,您必须再次调用您的php文件。