简体   繁体   English

退出时导致“ 500-内部服务器错误”的php语句

[英]php statement causing “500 - Internal server error” when logged out

I have this function in index.php... 我在index.php中有此功能...

function getCurrentUserID(){
  var currentUserID;
  if (confirmLogin()){
    currentUserID = "<?php echo htmlentities($_SESSION['user_id']); ?>";
  } else {
    currentUserID=-1;
  }
  return currentUserID;
}

When I'm logged in it works nicely and returns the user_id. 登录后,它可以很好地工作并返回user_id。 The second I log out (which refreshes index.php) I hit a "500 - Internal server error" that I can only resolve by DELETING the php line from the function (if I even comment out using javascripts // notation I still see the 500 error). 第二次我注销(刷新index.php)时,我碰到“ 500-内部服务器错误”,我只能通过从函数中删除php行来解决(如果我什至使用javascripts //注释掉,我仍然会看到500错误)。 Once I've deleted the line I can then refresh the page, log back in, and if I want add php reference to the function again! 删除该行后,我可以刷新页面,重新登录,如果我想再次向函数添加php引用!

Does anyone have any idea how to resolve this problem permanently though? 有谁知道如何永久解决此问题? I don't know why the php script is problematic since it's inside a "confirmLogin()" check (not to the mention the fact it's sometimes commented out altogether!) Feels like I might have put this in the wrong place but I really don't know what the right place is! 我不知道为什么php脚本会出现问题,因为它位于“ confirmLogin()”检查中(更不用说它有时会被完全注释掉了!)感觉就像我可能把它放在错误的位置,但是我真的不知道不知道正确的地方是什么! Thanks for any thoughts. 感谢您的任何想法。

The PHP code is being parsed server-side, so way before any JS comes to life - that's why it doesn't matter if your line is inside the JS function. PHP代码是在服务器端进行解析的,因此在任何JS出现之前就已经解决了-这就是为什么您的行是否在JS函数内部并不重要。

What causes your problem is fact you're trying to access an array element under the non-existing key. 导致您出现问题的原因是您正在尝试访问不存在的键下的数组元素。 In PHP it is illegal. 在PHP中,这是非法的。 The quickest fix is to check, if the value is set before echoing it: <?php isset($_SESSION['user_id']) ? echo $_SESSION['user_id' : echo '-1'; ?> 最快的解决方法是在回显值之前检查是否已设置该值: <?php isset($_SESSION['user_id']) ? echo $_SESSION['user_id' : echo '-1'; ?> <?php isset($_SESSION['user_id']) ? echo $_SESSION['user_id' : echo '-1'; ?>

This will resolve your issue but, in general, I would try to avoid mixing programming languages like that. 这样可以解决您的问题,但是总的来说,我会尽量避免混合使用这种编程语言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM