/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Read a Local File

All,

I am developing a speed test that exports through AJAX to PHP and into a MySQL database. One of the pieces of data I will need is the client-side computer name (not IP). Our company has a number of small laptops and I need a way to know which machine is pulling a particular speed.

My thoughts are that PHP won’t work since it is server-side, and there’s no way to get a computer’s name through any browser due to various settings. However, I do have access to all these computers and can create a text file (or PHP if needed) that only contains the computer’s name.

I’m out of ideas and have beaten Google to death searching for a solution. Considering I have full access to these computers, is there a way to read a file, or file name, and have this information put into a script for input into MySQL?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@TW79authorMar 26.2014 — 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

[CODE]<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>[/CODE]


extract.php

[CODE]<?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";
?>

[/CODE]
Copy linkTweet thisAlerts:
@Sup3rkirbyMar 26.2014 — There really were just two small things that needed to be corrected for this script to work. The first was when setting the javascript variable '[B]$compname[/B]', you need to wrap the echoed PHP data in quotations as it is a string. The second was in your [B]$testData[/B] variable being passed to MySQL, you are calling a variable named '[I]compname[/I]' when it is actually '[B]$compname[/B]'.
[CODE]
var $compname = "<?php echo (isset($_GET['compname'])) ? $_GET['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;
[/CODE]


Just for a little more clarification since I feel I might have caused some confusion if you were basing anything off previous code I've helped you with here. In javascript I have a habit of starting all of my variable names off with a dollar sign. I do this as it mimics PHP and saves me the trouble of having to think about it when switching between languages. It is not required in javascript but is in PHP. Your code has a mix of the two so be careful when calling variables as the names must be exact.
Copy linkTweet thisAlerts:
@TW79authorMar 26.2014 — Once again, you have saved the day. Thank you very much.
×

Success!

Help @TW79 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...