我正在使用AJAX将javascript变量传递给PHP。一切都运行良好,花花公子,然后我改变了我的PHP函数将如何执行的安全性。这是我的javscript代码:
function writetofile(file, wellname, api){
$.ajax({
type: 'post',
url: 'process.php',
data: {
api: api,
wellname: wellname,
fileName: file,
writetoFile: true
},
success: function(data){
output(api + " : " + wellname + " has been added to: " + file);
}
});
}
PHP:
// process.php
if ($_POST['writetoFile'])){ // i need to check here if writetofile is true, which it is
$api = $_POST['api'];
$wellname = $_POST['wellname'];
$fileName = $_POST['fileName'];
$break = "\n";
$str = $api. " : ". $wellname . $break;
$file = fopen($fileName,"a");
fwrite($file, $str);
fclose($file);
}
当我执行javascript代码时,它不会写入文件,是的,我已经检查过是否接受了ajax请求。它是,但我尝试过:
PHP
if ($_POST['writetoFile'] === true)){}
但这似乎也无济于事。谢谢你的帮助。
答案 0 :(得分:2)
在打开<?php
代码
error_reporting(E_ALL);
ini_set('display_errors', 1);
您会发现if
声明中有一个额外的右括号 -
if ($_POST['writetoFile'] === true))
------^