2014-07-03 39 views
0

我開發了一個牙科診所的應用程序,它在當天的預設時間發送自動電子郵件和短信提醒。由於數據庫是本地的,這將是安裝在客戶端機器上的本地php應用程序。我想把它作爲一個月度訂閱服務。爲了實現訂閱邏輯,我的想法是在我的服務器上保留一個有過期日期的文本文件。本地應用程序將讀取文本文件的內容以確保訂閱未過期。file_get_contents或文件函數掛起

現在的問題是,當我在我的腳本中使用file()或file_get_contents()函數來讀取我的服務器上的文本文件(例如www.abcde.com/expiration.txt)時,它會永遠保持閱讀文本文件。現在,如果我直接將文本文件的URL放在單獨的瀏覽器窗口中,它會立即打開它。一旦我這樣做,PHP腳本工作正常。過了一會兒,同樣的循環開始。

  • 我在這裏錯過了什麼嗎?
  • 有沒有更好的方法來實現訂閱邏輯?

我的php應用程序是使用wamp服務器的'local'。

+1

我懷疑'WWW。 abcde.com/expiration.txt'是您的服務器上的有效本地相對路徑,您需要將該方案預先寫入:http:// www.abcde.com/expiration.txt。請勿將內容放在遠程網站上,請記住。爲什麼不使用數據庫而不是文本文件? – DanFromGermany

+0

嗨。感謝您的答覆。我無法使用客戶端數據庫。我只從中獲取數據並使用它發送消息。遠程網站是我自己公司的網站,它將託管不同客戶的許可證到期細節。 – user2443689

回答

0

如果我明白你說的話,你想爲這些消息創建訂閱系統嗎?

您可以在您的數據庫使用一個新的表3周的cols:ID,CLIENT_ID,sub_time

它看起來很容易檢查,如果客戶端仍然是這樣,我認爲該消息的用戶。你只需要比較sub_time和當前時間,看看是否($ sub_time + 3600 * 24 * $ nb_of_day_in_last_month)> = $ current_time,或類似的東西......:D

+0

感謝您的回覆。我無法添加或編輯或修改數據庫。它是客戶的數據庫。我只是從該數據庫獲取名稱,手機,電子郵件地址等記錄以發送消息。我沒有使用我自己的任何一種數據庫。任何其他想法? – user2443689

+0

aahh damnit,你仍然可以用一個文件做到這一點,你使用像數據庫:)我發佈一個新的答案asap與腳本:p – Bobot

+0

謝謝。我非常感謝你的幫助。 – user2443689

0

所以,我有的新方法是這一次,希望它會正常工作對您

文件名爲爲例:subscribes.txt(我用一個「」 delimitor,你可以改變它,因爲它適合你)

; a comment line 
43,1404399704 
75,1404406800 
104,1404399200 
6,1404399500 

而且小腳本

$file = 'subscribes.txt'; 
$delim= ','; // set the delimitor of your cols 

$time = time(); // get time to use it in all the script 

// this part is to get the last month number of days 
$y = date('Y'); // year 
$lm = date('n') - 1; // last month 
$y = ($lm == 0) ? $y - 1 : $y; // if we are in jan., year - 1 
$lm = ($lm == 0) ? 12 : $lm; // if we are in jan., last month was dec. 
$lm = mktime(0, 0, 0, $lm, 1, $y); // so, the last month 
$lm_days = intval(date("t",$lm)); // get the number of days in last month 

// you can get the current month number of days with : 
// $lm_days = intval(date('t')); 

// end of the part 


$file = file_get_contents($file); // read file content 

$file = explode(PHP_EOL, $file); // transform it in array 

foreach($file as $lk => $line){ 

    if(!empty($line) && !preg_match('/^;(.*)/', $line)){ // if line not empty and don't start with ; 

     $cid= null; 
     $st = null; 

     list($cid,$st) = explode($delim, trim($line)); // explode the 3 cols of the line 

     $st = $st + 3600*24*$lm_days; 

     if($time >= $st){ 

      // send notifications etc etc ... (eg: your script) 
     } 
     else{ 

      // else we will delete the line to light the file 
      unset($file[$lk]); 
     } 
    } 
} 

// once done, letz write the lighted file 
$file = implode(PHP_EOL, $file); // convert array to string 

file_put_contents($file, $file); // put it back in the file 

而當你想添加一個訂閱,你只需要運行這個

$file_name = 'subscribes.txt'; 
$delim= ','; // set the delimitor of your cols 

$time = time(); // get time to use it in all the script 

$file = file_get_contents($file_name); // read file content 

$file = explode(PHP_EOL, $file); // transform it in array 

$file[] = $cid.$delim.time(); // add to array a new entry 
// there you can add multiple entries, let it fit to you :P 

$file = implode(PHP_EOL, $file); // convert array to string 

file_put_contents($file, $file); // put it back in the file 

告訴我,如果它仍然掛起或什麼的,我會盡力去找其他的解決方案:)

+0

我衷心感謝您的努力幫助我。我所做的是爲包含到期日期的每個客戶端創建一個單獨的文本文件。例如,文本文件「license201938474.txt」用於客戶端A,其內容爲2016-01-31到期日期。現在在我的php腳本中,我使用file_get_contents('license201938474.txt')讀取文件,並使用strtotime()函數獲取時間戳。然後,我將該時間戳與當前時間戳進行比較,以查看到期日期的時間戳是否大於當前時間戳。 – user2443689

+0

$phonenumber = ''; \t $licenseData = ''; \t $expiryTimestamp = ''; \t $curtime = time(); \t $phonenumber = preg_replace("/[^0-9]/","",$prefs['Office-Phone']); \t $licenseData = file_get_contents('http://www.abcde.com/license'.$phonenumber.'.txt'); \t $expiryTimestamp = strtotime($licenseData); \t echo "
Exp TimeStamp:".$expiryTimestamp."
Cur Timestamp:".$curtime; \t $timediff = $expiryTimestamp - $curtime; \t echo "
Time Diff:".$timediff; \t echo ($timediff > 0); \t if((expiryTimestamp - time()) < 0){ \t \t echo ""; \t \t die(); \t \t }
user2443689

+0

np ^^我喜歡測試腳本^^我剛剛看到您從網絡服務器獲取文件,而不是從本地獲取文件,您是否測試了是否有權獲取它? (如果你不在ftp,我認爲你不能把內容放在遠處的網址上)。如果文件與腳本位於同一臺服務器上,請嘗試使用本地路徑(如./path/to/my/file.txt)訪問它,如果距離較遠,也許應該使用另一種方法獲取該文件..也許[cURL](http://www.php.net/manual/fr/book.curl.php):p – Bobot