【问题标题】:Session is restarting on a specific page会话正在特定页面上重新启动
【发布时间】:2012-06-03 08:23:25
【问题描述】:

我正在为一个 uni 项目开发一个基于 php 的站点。

一切正常,我正在使用会话来存储已登录用户的数据。

我有一个页面,用户可以使用 jquery 标记图像。 jquery 使用 php 保存一个带有标签的 txt 文件。在那个 php 页面中,我添加了“session_start();”在顶部,以便我还可以在 mysql 数据库中存储一些信息。

但我发现,当我标记图像时,实际发生的是会话正在重新启动,因此用户正在注销,并且数据库中没有存储任何内容。

有什么想法我可能做错了吗? (它在我项目的所有其他页面上都有效,只有这个 javascript+php 组合似乎导致了问题)。

这是来自 php 页面的代码,它被 javascript 调用。它来自第三方 jquery 插件,我已经添加了自己的代码:

session_start();

    include 'notes.class.php';
    require_once '../db.php';

    if (isset($_POST['image']) && !empty($_POST['image']))
    $oNote = new note('../jquery-notes_notes/', '', (string) strip_tags($_POST['image']), '.note');
        $NoteName = md5(strip_tags($_POST['image']));
        $NoteName .= '.note';
        $ImageID = $_SESSION['CurrentImage'];

        //get the name of the last poster if the user is logged in, its the username, otherwise its the comment name
        if ($_SESSION['logged_in'] == 1) {
            $LastNoteBy = $_SESSION['username'];
        } else {
            $LastNoteBy = 'Guest: '.$_SESSION['commentname'];
        }

        //get relevant info for this image
        $DBQueryImageData = mysqli_query($dblink, "SELECT * FROM images WHERE image_id = '$ImageID'");
        while($row = mysqli_fetch_array($DBQueryImageData)) {
            $DBImageUserID = $row['user_id'];
            $DBImageName = $row['image_name'];
            $DBGivenName = $row['given_name'];
            $DBImageLasteNoteBy = $row['last_note_by'];
            $DBImageProjectID = $row['project_id'];
        }

        //get the project name of the related project
        $DBQueryProjectName = mysqli_query($dblink, "SELECT * FROM projects WHERE project_id = '$DBImageProjectID'");
        while($row = mysqli_fetch_array($DBQueryProjectName)) {
            $DBProjectName = $row['project_name'];
        }

        //get the username of the owner of the previously obtained user_id
        $DBQueryUsername = mysqli_query($dblink, "SELECT * FROM users WHERE user_id = '$DBImageUserID'");
        while($row = mysqli_fetch_array($DBQueryUsername)) {
            $DBUsername = $row['username'];
            $DBEmail = $row['email'];
            $DBNotify = $row['notify'];
        }

        //only send off the email if the user has asked to receive notifications
        if ($DBNotify == 1) {

            //compare the current poster to the last poster saved for this image, if they are different, and if the current poster is not the owner, email the owner of the project
            //if the last poster is not the same as the current poster
            if ($DBImageLasteNoteBy != $LastNoteBy) {

                //if the current poster is not the owner
                if ($LastNoteBy != $DBUsername) {

                    if (strlen($DBGivenName) > 4) {
                        $DBImageName = $DBGivenName;
                    }

                    //send the email
                    $to = $DBEmail;
                    $subject = "New comment on your project image";
                    $headers = 'From: test@test.com' . "\r\n" .
                    'Reply-To: test@test.com' . "\r\n" .
                    'X-Mailer: PHP/' . phpversion();
                    $additionalParameters = '-ODeliveryMode=d'; 
                    $body = 'Hi '.$DBUsername.','."\n\n".'A comment has been made by '.$LastNoteBy.' on your image: '.$DBImageName.' in your project: '.$DBProjectName."\n\n".
                    'http://'.$_SERVER['SERVER_NAME'].'/saeproj/index.php?page=project&id='.$DBImageProjectID;

                    mail($to, $subject, $body, $headers, $additionalParameters);

                }
            }
        }

        mysqli_query($dblink, "UPDATE images SET note = '$NoteName', last_note_by = '$LastNoteBy' WHERE image_id = '$ImageID'") or die(mysql_error());

更新:我刚刚注意到我没有在 iMac 上运行的 MAMP 版本上注销。但是我正在我的托管网络空间上注销(ipower 是主机)。

更新 2: 我刚刚检查过,会话 ID 没有改变,但会话 ['logged-in'] 被更改为 0。如果会话 ID 没有改变,则会话没有改变重新启动,对吗?

【问题讨论】:

  • 你的问题很难回答。通常是的,它没有重新启动,但另一方面,您实际上需要调试它才能确定。但这需要你自己做,所以我在这里帮不上什么忙。
  • 可能有和没有 www 的问题。但是根据你的描述是不可能猜到的。 :-(
  • 有趣的是,我刚刚意识到托管帐户上的 register_globals 设置为“on”。将其关闭可解决此问题。

标签: php jquery session


【解决方案1】:

事实证明,在托管的网络空间上,register_globals 设置为“开启”。关闭它可以解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-25
    • 1970-01-01
    • 2018-07-04
    • 1970-01-01
    • 2017-08-02
    • 2011-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多