2012-02-08 43 views
1

我正在爲主要聯繫人表單編寫一個腳本,需要將前10個導聯發送給電子郵件1,第二個導聯到電子郵件2,等等,直到到達電子郵件4,然後它回到電子郵件1.如何構建一個PHP轉子

這是一個旋轉器我已經爲着陸頁建成,但它每次旋轉1次,而不是等待10次,然後旋轉。我如何修改這個以適應我的需求?

另外,它顯然不能在每一次「刷新」時發生。需要有一組獨立的代碼,這些代碼將會在表單的action =「whatever.php」中出現,並且代碼將會增加它。

<?php 

//these are the email addresses to be rotated 
$email_address[1] = '[email protected]'; 
$email_address[2] = '[email protected]'; 
$email_address[3] = '[email protected]'; 
$email_address[4] = '[email protected]'; 

//this is the text file, which will be stored in the same directory as this file, 
//count.txt needs to be CHMOD to 777, full privileges, to read and write to it. 
$myFile = "count.txt"; 

//open the txt file 
$fh = @fopen($myFile, 'r'); 
$email_number = @fread($fh, 5); 
@fclose($fh); 

//see which landing page is next in line to be shown. 
if ($email_number >= count($email_address)) { 
    $email_number = 1; 
} else { 
    $email_number = $email_number + 1; 
} 

//write to the txt file. 
$fh = fopen($myFile, 'w') or die("can't open file"); 
$stringData = $email_number . "\n"; 
fwrite($fh, $stringData); 
fclose($fh); 

//include the landing page 
echo $email_address[$email_number]; 

//terminate script 
die(); 

?> 
+2

[在PHP數組是零索引。](http://php.net/manual/en/language.types.array.php) – 2012-02-08 04:37:37

回答

1

我從你的問題理解什麼是會有表單提交線索,當鉛被提交,它必須遵循你的邏輯。如我錯了請糾正我。

如果是這樣, 使用兩個文本文件,如track.txt。這個文本文件的初始內容是1,0。這意味着潛在客戶將被髮送到第一封電子郵件ID 0次。

所以在表單的動作腳本中包含以下代碼。

<?php 
$email_address[1] = '[email protected]'; 
$email_address[2] = '[email protected]'; 
$email_address[3] = '[email protected]'; 
$email_address[4] = '[email protected]'; 

$myFile = "track.txt"; 

//open the txt file 
$fh = @fopen($myFile, 'r'); 
$track = @fread($fh, 5); 
@fclose($fh); 

$track = explode(",",$track); 
$email = $track[0]; 
$count = $track[1]; 

if($count >= 10) 
{ 
    $count=0; 
    if($email >= count($email_address)) 
    { 
    $email = 1; 
    } 
    else 
    { 
    $email++; 
    } 
} 
else 
{ 
    $count++; 
} 

$track = $email.",".$count; 

//write to the txt file. 
$fh = fopen($myFile, 'w') or die("can't open file"); 
fwrite($fh, $track); 
fclose($fh); 

//send lead to $email 

?>