2012-11-30 70 views
-1

晚上好。按值查找索引

首先,我對這個問題的長度感到抱歉。我想徹底確保所有信息與我的意圖一起呈現。

我目前正在尋找開發一種簡單的基於Web的方法從我的桌面應用程序收集統計信息。這樣做的主要目的是通過對最終用戶的更新進行故障排除並提供更多相關的修補程序和功能添加。

詳細

  • 我將使用PHP 5.2.12,並利用$_GET搶從URL參數,並將其分配給變量。
  • 然後這些變量將被json_encode寫入文件(稱爲「statsfile」或「stats文件」)。
  • 其中一個參數/變量將是某種GUID。這個GUID將由我的應用程序基於從硬件ID創建的單向散列生成(儘管在代碼示例中,這尚未表示)。

的問題

我所面臨的問題涉及到裏面找到我的JSON統計文件中的特定GUID。

這是我想要做的;

  • 1)找到提交的statsfile
  • 2a)中如果不存在,則添加新的索引到所述陣列和填充內部
  • 2b中的數據)。如果它確實存在內部GUID,覆蓋陣列中現有的數據與提交的數據

重要的是,在最後,我留下了一個有效的JSON統計文件。劇本相對安全也很重要。

佈局和代碼

我的JSON的佈局如下:

[ 
    { 
     "guid": "spritchard", 
     "api": "apichoice", 
     "build": "2200", 
     "temp1": "1", 
     "temp2": "2", 
     "temp3": "3" 
    }, 
    { 
     "guid": "helloworld", 
     "api": "someapi", 
     "build": "3500", 
     "temp1": "4", 
     "temp2": "5", 
     "temp3": "6" 
    } 
] 

我使用的PHP代碼如下;

<?php 
$apikey=$_GET["apikey"]; 
if ($apikey=="apikey8634215") 
    { 
    $gpuapi=$_GET["api"]; 
    $buildno=$_GET["build"]; 
    $temp1=$_GET["temp1"]; 
    $temp2=$_GET["temp2"]; 
    $temp3=$_GET["temp3"]; 
    $guid=$_GET["guid"]; 

    $statsfile = "./api/application/StatsFile.json"; // Assign the stats file 
    $fh = fopen($statsfile, 'a') // 'a' for append, 'w' for write 
      or die("Stats File Unavailable"); 
    $sdata = array('guid' => $guid, 'api' => $gpuapi, 'build' => $buildno, 'temp1' => $temp1, 'temp2' => $temp2, 'temp3' => $temp3); 
    fwrite($fh, json_encode($sdata)); 
    fclose($fh); 
    echo json_encode($sdata); 

    // reopen stats file to encode data as it should be 
// $cdata = fopen($statsfile, 'r') or die("Stats File Unavailable"); 
// $encodedata = fread($cdata, filesize($statsfile)); 
// $decodedata = json_decode($encodedata); 
// fclose($cdata); 
// $fh = fopen($statsfile, 'w') or die("Stats File Unavailable"); 
// fwrite($fh, json_encode($encodedata)); 
// fclose($fh); 
    } 
else 
    { 
    die("No Such Usage"); 
    } 
?> 

你可以看到我有$apikey用來檢查一個特定的參數是否存在。如果不是,那麼整個腳本就會死亡。這只是一個佔位符設計,目的是爲了在開發系統時防止基本級別的濫用,​​並且我將在單獨的問題中調查更合適的解決方案。

註釋掉的區域是我試圖用新數據重寫文件並使其生效的JSON代碼。我不知道如何去查找數組中正確的GUID,但我也在努力弄清楚如何重寫數據,以便將其正確編碼爲JSON。取消註釋該代碼的最終結果是JSON中的很多斜槓,而JSON本身無效。此外,我不確定我在這裏展示的代碼是否安全。無論如何,我可以使用POST方法,但目前,找出如何基於它的GUID更新特定索引是個問題。

因此,爲了澄清我的問題,我將如何去從JSON statsfile中查找特定的GUID,如果它存在更新數據,或者使用提交的數據添加新的數組索引?

+0

你爲什麼不使用專爲存儲和檢索以及搜索信息而設計的系統(比如數據庫)的任何原因? sqlite應該可用於大多數PHP安裝。 – MatsLindh

+0

我沒有物理或根用戶訪問服務器,因爲它託管在共享虛擬主機上。另外,從軟件方面來說,通過報表工具解析JSON文件而不是處理數據庫對我來說更容易。 –

回答

1
$apikey=$_GET["apikey"]; 
if ($apikey=="apikey8634215") { 
    $newObject = new stdClass(); 
    $newObject->guid = $_GET["guid"]; 
    $newObject->api = $_GET["api"]; 
    $newObject->build = $_GET["build"]; 
    $newObject->temp1 = $_GET["temp1"]; 
    $newObject->temp2 = $_GET["temp2"]; 
    $newObject->temp3 = $_GET["temp3"]; 
    $statsFile = "./api/application/StatsFile.json"; 
    $stats = file_exists($statsFile) 
     ? json_decode(file_get_contents($statsFile)) 
     : array() 
    ; 
    $found = false; 
    foreach ($stats as $key => $statItem) { 
     if ($statItem->guid == $newObject->guid) { 
      $stats[$key] = $newObject; 
      $found = true; 
      break; 
     } 
    } 
    if (!found) { 
     $stats[] = $newObject; 
    } 
    if (file_put_contents($statsFile, json_encode($stats)) === false) { 
     die("Could not save stats"); 
    } 
} else { 
    die("No Such Usage"); 
} 

此代碼缺少獲取參數存在檢查,您應該以某種方式驗證輸入。