会话在新页面视图中被破坏 [英] Session getting destroyed on new page view
问题描述
成功登录后,我将保存会话变量.
Upon successful login, I'm saving the session variable.
当用户转到应用中的不同页面时,即使我没有明确销毁会话,会话也会消失.我该如何解决这个问题?
When the user goes to different pages in the app, the session is gone even though I didn't explicitly destroy the session. How do i fix this?
这是会话似乎消失的页面.
Here is a page where the session appears to disappear.
<?php
include 'core/init.php';
include 'core/sendmessage.php';
$user_info = $_SESSION['user_id'];
$getUser = mysql_query("SELECT * FROM users WHERE user_id = ".$uid);
$user_info = array();
while($currentRow = mysql_fetch_array($getUser)){
$user_info['firstname'] = $currentRow['first_name'];
$user_info['lastname'] = $currentRow['last_name'];
$user_info['username'] = $currentRow['username'];
}
?>
在 core/init.php
中,我有会话启动方法.
Within core/init.php
I have the session start method.
<?php
session_start();
require 'database/connect.php';
require 'functions/users.php';
require 'functions/general.php';
if (logged_in() === true) {
$user_data = user_data($_SESSION['user_id'],'first_name','last_name','username');
}
$errors = array();
?>
推荐答案
session_start() 根据通过 GET 或 POST 请求传递的会话标识符创建会话或恢复当前会话,或通过 cookie 传递.(访问PHP:session_start)>
session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie. (Visit PHP: session_start)
在每个页面的开头添加 session_start()
(在 标签之后).
Add session_start()
on the beginning of each page (after your <?php
tag).
就你而言:
<?php
if(!isset($_SESSION)) session_start(); //--> ADD this line
include 'core/init.php';
include 'core/sendmessage.php';
$user_info = $_SESSION['user_id'];
...
这篇关于会话在新页面视图中被破坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!