【问题标题】:How to avoid 500 Internal Server Error if a JS session variable null如果 JS 会话变量为空,如何避免 500 内部服务器错误
【发布时间】:2015-03-17 12:00:55
【问题描述】:

我创建了这个脚本来读取各种网络资源。此脚本会更新所有通知(邮件、帖子、朋友)和他们自己的下拉窗口的详细信息以及两个侧面更新栏。我在我的顶级 manu.php 页面中使用了这个脚本。

此脚本适用于 login 用户。

现在我使用 JavaScript var uid = '<? echo $session->id; ?>'; 传递会话 id 变量,因此当用户未登录时,我的浏览器控制台显示 500 Internal Server Error,因为这里会话没有 ID,因此它通过 uid=''

我该如何克服这个问题?

这是我的 JavaScript 脚本:

var uid = '<? echo $session->id; ?>';
    function addmailno(type, msg){
    //Do something for display mail/friend/post notification
    }

    function addmailup(type, msg){
    //Do something for display mail/post/friend drop-down.
    }

    function addside(type, msg){
    //Do something for display all friend/new post in side bar.
    }

    function waitFormailno(){
        $.ajax({
            type: "GET",
            url: "serverforupandside.php",
            cache: false,
            async : false,
            dataType : 'json',
            data: "uid="+ uid,
            timeout:15000, 
            success: function(data){ 
                addmailno("MyDivClass", data);
                addmailup("MyDivClass", data);
                addside("MyDivId", data);
                setTimeout(waitFormailno, 15000);
            },
            error: function(){
                setTimeout(waitFormailno, 15000); 
            }
        });
    }

$(document).ready(function(){
    waitFormailno();
});

serverforupandside.php

<?php
include("db.php");
include_once("mysession.php");

while (true) {
if($_GET['uid']){
global $dbh;

//All php query is here one after one

//Output is here by data
$data = array();

$data['upmail'] = $upmail;
$data['upfollow'] = $upfollow;
$data['uppost'] = $uppost;
// etc all

    if (!empty($data)) {
        echo json_encode($data);
        flush();
        mysqli_close($dbh);
    }
    }
    mysqli_close($dbh);
}
?>

【问题讨论】:

  • 您在if($_GET['uid']){ 遇到错误吗?尝试将其更改为 if(!empty($_GET['uid'])){
  • 另外,你在哪里使用$_GET['uid'] 在你的代码中进一步?
  • 我知道这种方法只是为了在 php 中获取传递变量。没有$_GET['uid']先生应该怎么做更好?
  • 我不清楚你的陈述“没有 $_GET..”。您是否能够确定导致 500 错误的原因?为什么要使用无限循环?while (true)
  • 我试图让我的脚本像长轮询一样。所以使用while (true)。如果会话未登录,则在 chrome 浏览器控制台中显示 500 error

标签: javascript php jquery long-polling


【解决方案1】:

您的 Javascript 代码应修改为:

function waitFormailno(){
        $.ajax({
            type: "GET",
            url: "serverforupandside.php",
            cache: false,
            async : false,
            dataType : 'json',
            data: "uid="+ uid,
            timeout:15000, 
            success: function(data){ 
                addmailno("MyDivClass", data);
                addmailup("MyDivClass", data);
                addside("MyDivId", data);
            }
        });
    }

$(document).ready(function(){
    setInterval(waitFormailno, 15000); // calls a function or evaluates an expression at specified intervals (in milliseconds)
});

PHP 代码:

<?php
include("db.php");
include_once("mysession.php");

if (!empty($_GET['uid'])) {
    global $dbh;

    //All php query is here one after one

    //Output is here by data
    $data = array();

    $data['upmail']   = $upmail;
    $data['upfollow'] = $upfollow;
    $data['uppost']   = $uppost;
    // etc all

    if (!empty($data)) {
        echo json_encode($data);
    }


}

此外,您应该在填充 $data 数组之前添加验证。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-13
    • 1970-01-01
    • 2019-01-17
    • 2011-10-17
    • 2010-11-15
    相关资源
    最近更新 更多