【问题标题】:PHP Session LostPHP 会话丢失
【发布时间】:2013-08-10 00:09:36
【问题描述】:

我知道那里有很多问题,但他们没有提供太多信息或使用其他方法进行身份验证。开始吧:

我有一个登录页面和主页。 我在 $_SESSION 中登录保存会话 在每个页面中,我都会创建 session_start(),然后检查用户是否在登录时查看 $_SESSION。

当我登录时,它会将我重定向到主页。 当我尝试 login.php 时,它会正确检测到即时消息并重定向到 main.php。 当我刷新 main.php 时,它会正确检测到我的会话。

然后,我想在 DB 中进行更改,所以我使用 jquery 函数 $.post 将发布数据发送到 action.php 并做一些工作人员。 Action.php 回显“true”,因此 $.post 处理程序可以检测到更改是否已正确进行并重定向。 Action.php 回显“false”,因此 $.post 处理程序警告发生了错误。

当我对 DB 进行更改时,它会完成并回显 true,因此 main.php 刷新自身显示更改,但不是刷新,而是返回 LOGIN.PHP 丢失所有会话数据。

这里有一些代码:

开始会话

function sec_session_start() {
    $session_name = 'sec_session_id'; // Set a custom session name
    $secure = false; // Set to true if using https.
    $httponly = true; // This stops javascript being able to access the session id. 

    ini_set('session.use_only_cookies', 1); // Forces sessions to only use cookies. 
    $cookieParams = session_get_cookie_params(); // Gets current cookies params.
    //session_set_cookie_params($cookieParams["lifetime"], $cookieParams["path"],              $cookieParams["domain"], $secure, $httponly);
    session_set_cookie_params(7200, "/", $cookieParams["domain"], $secure, $httponly); 
    session_name($session_name); // Sets the session name to the one set above.
    session_start(); // Start the php session
    session_regenerate_id(true); // regenerated the session, delete the old one.     
    }

ma​​in.php 和 login.php 和 action.php 的顶部

include('../db_connect.php');
include('../functions.php');

sec_session_start(); // Our custom secure way of starting a php session. 

if(!login_check($mysqli)){
    header('Location: ../');
}

将数据发布到 action.php 的 JavaScript 函数

function createTeam(){

    var serializedData = $('#new_team_form').serialize();

    $.post('action.php', serializedData , function (resp) {
        if(resp == "false"){
            $('#exists').html("El grupo que intentas crear ya existe o ha habido un error en la petición.");
        }else{
            window.location ="./";
        }
    });
    return false; //Needs to return false cause its the submit button of form
}

action.php - 回显的将是 $.post() 的返回

<?php

include '../db_connect.php';
include '../functions.php';

sec_session_start(); // Our custom secure way of starting a php session. 


$team = $_POST['team'];
$_SESSION['user_id'] = $user_id;



if ($stmt = $mysqli->prepare("SELECT name FROM teams WHERE name = ? LIMIT 1")) { 
        $stmt->bind_param('s', $team); // Bind "$user_id" to parameter.
        $stmt->execute(); // Execute the prepared query.
        $stmt->store_result();


        if($stmt->num_rows == 1) { 
            echo "false1".$team;
        } else {
            //Creamos el grupo en la base de datos y ponemos al usuario en dicho grupo

            if ($stmt = $mysqli->prepare("INSERT INTO teams (id, name) VALUES (DEFAULT, '$team')")) { 
                $stmt->execute(); // Execute the prepared query.
                if($stmt = $mysqli->prepare("UPDATE  members SET  team =  '".$team."' WHERE  members.id ='".$user_id."' LIMIT 1 ")) {
                    $stmt->execute(); // Execute the prepared query.
                    echo "true";
                }else{
                    echo "false2";
                }

            }else{
                echo "false3";
            }
        }
     } else {
        echo "false4";
     }

?>

【问题讨论】:

    标签: php session login mysqli


    【解决方案1】:

    正如我理解你的代码和你问的问题......通过main.php没有会话转移到action.php,除了所有的东西都运行正常..

    使用这个语法...

    //会话以以下形式检索值:

    $_SESSION['user_id'] = $user_id;

    //并转移到检索会话的另一个页面:

    $user_id = $_SESSION['user_id'];

    我认为在您的 action.php 中,会话没有以检索形式正确声明。把这个写在action.php中

    $user_id = $_SESSION['user_id'];

    【讨论】:

    • 我不太理解你的回答,但我认为你想说的是:$_SESSION['user_id'] = $user_id 必须重写为 $user_ide = $_SESSION['user_id']。多么愚蠢的错误,现在一切顺利,谢谢
    • 对不起..我的回答没有得到很好的解释但是它的意思是和你在评论中提到的一样有一个语法错误..所以最后你的问题是解决...这是我的荣幸..
    猜你喜欢
    • 2012-05-09
    • 2012-06-03
    • 1970-01-01
    • 2011-12-05
    • 2020-07-11
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    相关资源
    最近更新 更多