www.webdeveloper.com
Results 1 to 4 of 4

Thread: [RESOLVED] Read a Local File

  1. #1
    Join Date
    Mar 2014
    Location
    Corpus Christi, TX
    Posts
    88

    resolved [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?

  2. #2
    Join Date
    Mar 2014
    Location
    Corpus Christi, TX
    Posts
    88

    Workaround

    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>
    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"; 
    ?>

  3. #3
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    1,031
    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 '$compname', you need to wrap the echoed PHP data in quotations as it is a string. The second was in your $testData variable being passed to MySQL, you are calling a variable named 'compname' when it is actually '$compname'.
    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;
    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.

  4. #4
    Join Date
    Mar 2014
    Location
    Corpus Christi, TX
    Posts
    88
    Once again, you have saved the day. Thank you very much.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles