2011-05-10 45 views
0

嗯,我正在寫一個PHP代碼來編輯這些標籤內的標籤和數據,但我有很大的麻煩讓我的頭周圍的東西。無法得到正確的想法

基本上我有類似這樣的XML文件,但更大的

<users> 
<user1> 
    <password></password> 
    </user1> 
</users> 

和我使用的嘗試和改變USER1標籤的PHP代碼時只需運行是這樣

function mod_user() { 
    // Get global Variables 
    global $access_level; 

    // Pull the data from the form 
    $entered_new_username = $_POST['mod_user_new_username']; 
    $entered_pass = $_POST['mod_user_new_password']; 
    $entered_confirm_pass = $_POST['mod_user_confirm_new_password']; 
    $entered_new_roll = $_POST['mod_user_new_roll']; 
    $entered_new_access_level = $_POST['mod_user_new_access_level']; 

    // Grab the old username from the last page as well so we know who we are looking for 
    $current_username = $_POST['mod_user_old_username']; 

    // !!-------- First thing is first. we need to run checks to make sure that this operation can be completed ----------------!! 

    // Check to see if the user exist. we just use the normal phaser since we are only reading and it's much easier to make loop through 
    $xml = simplexml_load_file('../users/users.xml'); 

    // read the xml file find the user to be modified 
    foreach ($xml->children() as $xml_user_get) 
    { 
     $xml_user = ($xml_user_get->getName()); 

     if ($xml_user == $entered_new_username){ 
      // Set array to send data back 
      //$a = array ("error"=>103, "entered_user"=>$new_user, "entered_roll"=>$new_roll, "entered_access"=>$new_access_level); 

      // Add to session to be sent back to other page 
      // $_SESSION['add_error'] = $a; 
      die("Username Already exist - Pass"); 
      // header('location: ../admin.php?page=usermanage&task=adduser'); 
     } 
    } 

    // Check the passwords and make sure they match 
    if ($entered_pass == $entered_confirm_pass) { 
     // Encrypt the new password and unset the old password variables so they don't stay in memory un-encrytped 
     $new_password = hash('sha512', $entered_pass); 

     unset ($entered_pass, $entered_confirm_pass, $_POST['mod_user_new_password'], $_POST['mod_user_confirm_pass']); 
    } 

    else { 

     die("passwords did not match - Pass"); 
    } 

    if ($entered_new_access_level != "") { 
     if ($entered_new_access_level < $access_level){ 
      die("Access level is not sufficiant to grant access - Pass"); 
     } 
    } 

    // Now to load up the xml file and commit changes. 
    $doc = new DOMDocument; 
    $doc->formatOutput = true; 
    $doc->perserveWhiteSpace = false; 
    $doc->load('../users/users.xml'); 

    $old_user = $doc->getElementsByTagName('users')->item(0)->getElementsByTagName($current_username)->item(0); 

    // For initial debugging - to be deleted 
    if ($old_user == $current_username) 
     echo "old username found and matches"; 

    // Check the variables to see if there is something to change in the data. 
    if ($entered_new_username != "") { 
     $xml_old_user = $doc->getElementsByTagName('users')->item(0)->getElementsByTagName($current_username)->item(0)->replaceChild($entered_new_username, $old_user); 

     echo "Username is now: " . $current_username; 
    } 

    if ($new_pass != "") { 
     $current_password = $doc->getElementsByTagName($current_user)->item(0)->getElementsByTagName('password')->item(0)->nodeValue; 

     //$replace_password = $doc 
    } 
} 

用戶名輸入更改我得到這個錯誤

Catchable fatal error: Argument 1 passed to DOMNode::replaceChild() must be an instance of DOMNode, string given, called in E:\xampp\htdocs\CGS-Intranet\admin\html\useraction.php on line 252 and defined in E:\xampp\htdocs\CGS-Intranet\admin\html\useraction.php on line 201 

有人可以向我解釋如何d O此或告訴我他們是如何做到這一點..它可能做一點意義,我看它是如何做:■

感謝

+0

當你運行你的代碼時會發生什麼?你會得到什麼錯誤? – 2011-05-10 01:32:07

+0

對於用戶名和密碼,我建議使用數據庫,它更加強大和安全 – Ibu 2011-05-10 01:35:06

+0

好吧修復它 – cyclobs 2011-05-10 01:36:25

回答

1

$entered_new_username是一個字符串,所以你需要把它包DOM對象,通過類似$doc->createElement()

$xml_old_user = $doc->getElementsByTagName('users')->item(0)->getElementsByTagName($current_username)->item(0)->replaceChild($doc->createElement($entered_new_username), $old_user); 

這可能不是很正確,但希望它指向你在正確的方向。

0

沒事了就寫,更換,我希望節點,但我已經遇到了其他問題,我得把(IE:它是更換整個樹而不是僅僅改變節點名稱)

反正碼我用的是

// For initial debugging - to be deleted 
if ($old_user == $current_username) 
    echo "old username found and matches"; 

// Check the variables to see if there is something to change in the data. 
if ($entered_new_username != "") { 
    try { 
    $new_node_name = $doc->createElement($entered_new_username); 

    $old_user->parentNode->replaceChild($new_node_name, $old_user); 
    } 
    catch (DOMException $e) { 
     echo $e; 
    } 
    echo "Username is now: " . $current_username; 
} 

if ($new_pass != "") { 
    $current_password = $doc->getElementsByTagName($current_user)->item(0)->getElementsByTagName('password')->item(0)->nodeValue; 

    //$replace_password = $doc 
} 

$doc->save('../users/users.xml');