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?
Check if file is equal to
Re: Check if file is equal to
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.
- FrederickGeek8
- Posts: 148
- Joined: Wed Nov 30, 2011 10:31 pm
Re: Check if file is equal to
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
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.
- FrederickGeek8
- Posts: 148
- Joined: Wed Nov 30, 2011 10:31 pm
Re: Check if file is equal to
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.
[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
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