Simple XML Registration
The following is sample code to build an XML user registration that uses PHP Simple XML to do all of the processing
<?xml version="1.0"?> <users> <user user_id="0" status="1" access="0"> <user_name>dirkDirk</user_name> <user_pass><![CDATA[$2y$10$5RcTqwdZDE2NTJxiZuUF1.aaxFVUCaDYO7KAbLtbSEtGz7qXdxqky]]></user_pass> </user> <user user_id="1" status="1" access="1"> <user_name>lanceLance</user_name> <user_pass><![CDATA[$2y$10$3D2jTVhvihST8N6ycSgEl.mggiAJYlhSwTQIVC1XrX7fVwYBOAbbq]]></user_pass> </user> <user user_id="2" status="1" access="2"> <user_name>hermHerm</user_name> <user_pass><![CDATA[$2y$10$R0M6lNYnUv5z5Clj6xk92eJxC8u08dj7jpKvftL2.qkq7ZCEJN.l.]]></user_pass> </user> </users>
.flex_wrap { display:flex; flex-flow:row wrap; } .sel_control { margin:1rem; border:1px solid #000; border-radius:0.25rem; padding:0.25rem; } .btn_control { margin:1rem; border:1px solid #000; border-radius:0.25rem; padding:0.25rem 0.5rem; background-color:#000; color:#fff; } #user_message { margin:1rem; border:1px solid #000; border-radius:0.25rem; padding:0.25rem 0.5rem; min-height:5rem; }
<div class="flex_wrap"> <input type="text" id="txtUsername" name="txtUsername" minlength="8" maxlength="20" /> <input type="password" id="txtPassword" name="txtPassword" minlength="8" maxlength="20" /> <input type="button" id="btnLogin" value="Register" class="btn_control" /><br/> </div> <div id="user_message"></div>
window.addEventListener("DOMContentLoaded", (event) =>{ let txtUsername = document.getElementById("txtUsername"); let txtPassword = document.getElementById("txtPassword"); let btnLogin = document.getElementById("btnLogin"); let user_message = document.getElementById("user_message"); btnLogin.addEventListener("click", function() { let error_str = validate_login(); let data = ""; let msg = ""; let accessLevel = -1; let xmlhttp; let return_obj; if (error_str == "") { // PACKAGE THE DATA data = "txtUsername="+ txtUsername.value +"&txtPassword="+ txtPassword.value; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { returnObj = JSON.parse(xmlhttp.responseText); if (returnObj.errorString != "") { msg = "Error: "+ returnObj.errorString; } else { msg += returnObj.userName +"r/>ID: "+ returnObj.userId +"r/>"; accessLevel = parseInt(returnObj.accessLevel); switch (accessLevel) { case 0: msg += "Registered as Adminitrator"; break; case 1: msg += "Registered as Manager"; break; case 2: msg += "Registered as User"; break; default: msg += "not registered"; } } user_message.innerHTML = msg; } } xmlhttp.open("POST","register_user.php",true); xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); xmlhttp.send(data); } else { user_message.innerHTML = error_str } },false); let validate_login = function() { let error_str = ""; if (txtUsername.value.trim() == "") { error_str += "You must enter a usernamer/>"; } else { if (txtUsername.value.length 8) { error_str += "Your username must be at least 8 characters longr/>"; } } if (txtPassword.value.trim() == "") { error_str += "You must enter a passwordr/>"; } else { if (txtPassword.value.length 8) { error_str += "Your password must be at least 8 characters longr/>"; } } return error_str; }; });
<?php class SimpleXMLExtended extends SimpleXMLElement { public function addCData($cdata_text) { $node= dom_import_simplexml($this); $no = $node->ownerDocument; $node->appendChild($no->createCDATASection($cdata_text)); } } // GET THE POSTED DATA - reg_add.php $txtUsername = $_POST['txtUsername']; $txtPassword = $_POST['txtPassword']; $xml_file = 'site_users_sx_1.xml'; $error_str = ''; $counter = 0; if (file_exists($xml_file)) { $current_id = -1; $new_id = 0; $xml_t = simplexml_load_file($xml_file) or die ('Cannot open XML file'); $xml = new SimpleXMLExtended($xml_t->asXML()); // CHECK FOR EXISTING RECORD WITH ENTERED USERNAME foreach($xml->children() as $existing_user) { $att = 'user_id'; $counter++; $current_id = (int)$existing_user->attributes()->$att; if ($current_id > $new_id) { $new_id = $current_id; } if ($txtUsername == $existing_user->user_name) { $error_str = 'Error: Username Already Exists'; } } if ($error_str == "") { $new_id++; $new_user = $xml->addChild('user'); $new_user->addAttribute('user_id',$new_id); $new_user->addAttribute('status','1'); $new_user->addAttribute('access','2'); // USER_NAME $new_user->addChild('user_name', $txtUsername); // USER_PASS - NEED TO ENCRYPT AND WRITE AS CDATA $new_user->user_pass = NULL; $new_user->user_pass->addCData(password_hash($txtPassword, PASSWORD_DEFAULT)); // SAVE XML TO FILE $xml->asXML($xml_file); } } else { /* FILE DOES NOT EXIST, SO CREATE XML TO BE SAVED TO FILE */ $new_id = 0; $xml_t = new SimpleXMLElement('<users></users>'); $xml = new SimpleXMLExtended($xml_t->asXML()); $new_user = $xml->addChild('user'); $new_user->addAttribute('user_id',$new_id); $new_user->addAttribute('status','1'); $new_user->addAttribute('access','2'); // USER_NAME $new_user->addChild('user_name', $txtUsername); // USER_PASS - NEED TO ENCRYPT AND WRITE AS CDATA $new_user->user_pass = NULL; $new_user->user_pass->addCData(password_hash($txtPassword, PASSWORD_DEFAULT)); // SAVE XML TO FILE $xml->asXML($xml_file); } $output = array('errorString' => $error_str, 'userName' => $txtUsername, 'userId' => $new_id, 'accessLevel' => 2); header('Content-Type:application/json'); echo json_encode($output, JSON_FORCE_OBJECT); ?>