I'm trying to get a javascript function to call a PHP function to write a text file to my web server so i can store simple data for my game without using databases. But this will allow random people to call that function from the console (opened by pressing F12) and therefor write files to my web server.
Is there any way to fix this? Is everything wrong and do i have to start all over? Or is it just not possible?
My current code:
test.html:
<script src="js/php.js"></script>
<button onclick="phpFunc('test.php', 'write(\'Hello, World!\')', alertText);">test</button>
js/php.js:
function getReq() {
var req = false;
if (XMLHttpRequest) req = new XMLHttpRequest();
else {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
return false;
}
}
}
return req;
}
function phpFunc(url, func, success) {
sfunc = func.split(")")[0].split("(");
rfunc = sfunc[0];
rparams = sfunc[1];
var req = getReq();
if (!req) return false;
req.onreadystatechange = function() { if (req.readyState == 4 && req.status === 200) success(req.responseText); }
req.open("GET", url + "?t=" + Math.random() + "&func=" + rfunc + "¶ms=" + rparams, true);
req.send();
}
function alertText(text) {
alert(text);
}
test.php:
<?php
function write($text) {
$file = fopen("test.txt", "w");
fwrite($file, $text);
fclose($file);
}
{
$func = $_GET["func"];
$params = explode(", ", $_GET["params"]);
for ($i = 0; $i < count($params); $i++) {
if (is_numeric($params[$i])) $params[$i] = (int)$params[$i];
else $params[$i] = str_replace(array("\"", "'"), "", $params[$i]);
}
call_user_func_array($func, $params);
}
?>
Also english is not my first language so i'm not sure if everything was typed correctly.