I've come to the conclusion that I cannot read a local file to get the computer name. However, since I am using a task scheduler to launch the website, I can append the URL to include a "?compname=test" where "test" is the computer name, manually put into each VBS.
I tried this, and the compname is set, but Javascript gives me errors when trying to put the variable into my post. At one point it was transferring a combination of letters and numbers, but not the computer name as set in the URL.
Below is my script for both the Javascript and the PHP processing entry into MySQL.
Thoughts anyone?
index.php
<html>
<head>
<script src="http://speedof.me/api/api.js" type="text/javascript"></script>
</head>
<body>
<h2>Rental Xpress Internet Speed Test</h2>
<div id="msg"></div>
<script type="text/javascript">
SomApi.account = "SOM5326f3c941ee5"; //your API Key here
SomApi.domainName = "rxserver.no-ip.biz"; //your domain or sub-domain here
SomApi.config.sustainTime = 4;
SomApi.onTestCompleted = onTestCompleted;
SomApi.onError = onError;
var msgDiv = document.getElementById("msg");
function btnStartClick() {
msgDiv.innerHTML = "<h3>Speed test in progress. Please wait...</h3>";
SomApi.startTest();
}
function onTestCompleted(testResult) {
msgDiv.innerHTML =
"<h3>"+
"Download: " +testResult.download +"Mbps <br/>"+
"Upload: " +testResult.upload +"Mbps <br/>"+
"Latency: " +testResult.latency +"ms <br/>"+
"Jitter: " +testResult.jitter +"ms <br/>"+
"Test Server: "+testResult.testServer +"<br/>"+
"IP: " +testResult.ip_address +"<br/>"+
"Hostname: " +testResult.hostname +"<br/>"+
"</h3>";
ajaxrequest('extract.php', 'context', testResult);
}
function onError(error) {
msgDiv.innerHTML = "Error "+ error.code + ": "+error.message;
}
function get_XmlHttp() {
// create the variable that will contain the instance of the XMLHttpRequest object (initially with null value)
var xmlHttp = null;
if(window.XMLHttpRequest) { // for Forefox, IE7+, Opera, Safari, ...
xmlHttp = new XMLHttpRequest();
} else if(window.ActiveXObject) { // for Internet Explorer 5 or 6
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlHttp;
}
// sends data to a php file, via POST, and displays the received answer
function ajaxrequest(php_file, tagID, testResult) {
// create pairs index=value with data that must be sent to server
<?php $compname = $_GET['compname']; ?>
var $compname = <?php echo $compname ?>
var $testData = "download="+testResult.download+"&upload="+testResult.upload+"&latency="+testResult.latency+"&jitter="+testResult.jitter+"&testServer="+testResult.testServer+"&ip_address="+testResult.ip_address+"&hostname="+testResult.hostname+"&compname="+compname;
var request = get_XmlHttp();
request.open("POST", php_file, true); // set the request
// adds a header to tell the PHP script to recognize the data as is sent via POST
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.send($testData); // calls the send() method with datas as parameter
console.log($testData);
// If the response is received completely, will be transferred to the HTML tag with tagID
request.onreadystatechange = function() {
if (request.readyState == 4) document.getElementById(tagID).innerHTML = request.responseText;
}
}
(function(){ setTimeout(btnStartClick, 1500); })();
</script>
<div id="msg"></div>
<div id="data">This string will be sent with Ajax to the server, via POST, and processed with PHP</div>
<div id="context">Here will be displayed the response from the php script.</div>
</body>
</html>
extract.php
<?php
$con = mysqli_connect("***","speedtest");
$computerName = gethostbyaddr($_SERVER['REMOTE_ADDR']);
$download = (isset($_POST['download'])) ? $_POST['download'] : "";
$upload = (isset($_POST['upload'])) ? floatval($_POST['upload']) : "";
$latency = (isset($_POST['latency'])) ? $_POST['latency'] : "";
$jitter = (isset($_POST['jitter'])) ? $_POST['jitter'] : "";
$testServer = (isset($_POST['testServer'])) ? $_POST['testServer'] : "";
$testServerip_address = (isset($_POST['ip_address'])) ? $_POST['ip_address'] : "";
$hostname = (isset($_POST['hostname'])) ? $_POST['hostname'] : "";
$compname = (isset($_POST['compname'])) ? $_POST['compname'] : "";
$query = "INSERT INTO data (download, upload, latency, jitter, testServer, testServerip_address, hostname, computerName) VALUES ('$download', '$upload', '$latency', '$jitter', '$testServer', '$testServerip_address', '$hostname', '$compname')";
if(!mysqli_query($con,$query)) die('Error; ' . mysqli_error($con));
echo "bueno";
?>