我正在Haxe制作一个Web应用程序并将其编译为PHP。我在本地服务器(php -S
)上测试PHP代码。
这是代码:
//...
switch(page) {
//...
case "user":
//if logged in, display profile; if not, redirect to login
var loggedIn = false;
//check if user is logged in
if (Session.exists("username") && Session.exists("password")) {
var username:String = Session.get("username");
var password:String = Session.get("password");
//check the password
var conn = Mysql.connect({user: "..." pass: "...", host: "127.0.0.1", database: "..."});
var dbpass = conn.request("SELECT password FROM users WHERE username = \'" + username + "\';").results().first().password;
if (password == dbpass)
loggedIn = true;
}
if (!loggedIn) {
returnPage += File.getContent("../html/login.html");
} else {
//TODO add profile page
}
}
服务器发出此错误(编译时没有错误):
uncaught exception: Unable to call <exists>
in file: /.../lib/haxe/ds/StringMap.class.php line 31
#0 /.../lib/Open.class.php(9): haxe_ds_StringMap->__call('exists', Array)
#1 /.../open/index.php(11): Open::main()
#2 {main}
这就是真正奇怪的部分开始的地方:当我改变代码中的某些东西时(它不会影响应用程序,甚至评论都会这样做),不要构建它并重新加载页面,它突然起作用。但是当我构建代码时,它会再次出错。
它是服务器中的一些错误还是我在某处犯了错误?
我将测试服务器移到Apache,问题仍然存在。
我发现exists
类的StringMap
函数由于某种原因没有编译成PHP,所以我编辑代码是这样的:
//...
if (Session.get("username") != null && Session.get("password") != null) {
它现在有效。当我没有编译文件时,不知道为什么输出会改变,但这并不重要。