Page 1 of 1

Check if file is equal to

Posted: Tue Oct 16, 2012 10:53 am
by FrederickGeek8
How do I do something so if would be like
[syntax=php]<?php
$contents = file_get_contents('controller.txt');
if($contents == 'hey'){
echo 'it is hey';
}elseif($contents === 'no'){
echo 'it is no';
}
?>[/syntax]

In javascript/jquery?

Re: Check if file is equal to

Posted: Thu Oct 18, 2012 5:42 pm
by jacek
You can't. JavaScript has no access to the file on the server so it has no way to check if it exists. The closest you can get is to create a script to do the check for you and make an AJAX request to it.

Re: Check if file is equal to

Posted: Wed Oct 24, 2012 12:10 am
by FrederickGeek8
Even if the file is in the public domain? So what if it was like 'http://somesite.org/controller.txt'

Re: Check if file is equal to

Posted: Sat Oct 27, 2012 9:41 pm
by jacek
FrederickGeek8 wrote:Even if the file is in the public domain? So what if it was like 'http://somesite.org/controller.txt'

For a URL like that you can get the contents using an AJAX request (google it, many many example), this is only possible if the file is hosted on the same domain as the website.

Re: Check if file is equal to

Posted: Sat Nov 03, 2012 2:06 am
by FrederickGeek8
OK. So I was able to achieve this from

[syntax=javascript]var previous = "bad2";

setInterval(function() {
var ajax = new XMLHttpRequest();
ajax.onreadystatechange = function() {
if (ajax.readyState == 4) {
if (ajax.responseText != previous) {
window.location.reload();
previous = ajax.responseText;
}
}
};
ajax.open("POST", "control", true); //Use POST to avoid caching
ajax.send();
}, 500);[/syntax]

the variable previous is generated by PHP when the page is reload. Now this all works perfectly fine if I am accessing this from its external ip, but if I am trying to access it from a domain name linked to that ip, it does not work.

Re: Check if file is equal to

Posted: Sat Nov 10, 2012 9:53 pm
by jacek
FrederickGeek8 wrote:but if I am trying to access it from a domain name linked to that ip, it does not work.

For security reasons pretty much every browser will not allow you to make a request to another domain like this, no way around it :(