gpt4 book ai didi

php - 页面刷新时 session 被破坏

转载 作者:行者123 更新时间:2023-12-03 22:57:31 25 4
gpt4 key购买 nike

我有一个 php 文件,它在第一次加载时工作正常,但刷新页面后 session 被破坏。在刷新 php 页面时,我收到警告:session_regenerate_id(): session 对象销毁失败

<?php
/** functions.php **/
function startSession() {
$session_name = 'sec_session_id'; // Set a custom session name
session_name($session_name);
session_start(); // Start the PHP session
session_regenerate_id(true); // regenerated the session
}
?>

<!-- controller.js -->
function LoadFriend() {
var listFriend = $('#list_member_friend');
var UrlToPass = 'action=load';
listFriend.html('loading..');
$.ajax({
url : 'ajax.php',
type : 'POST',
data : UrlToPass,
success: function(responseText) {
listFriend.html(responseText);
}
});
}
loadFriend();
<!-- end controller.js -->

<?php
/** ajax.php **/
$action = $_GET['action']
switch($action) {
case "load":
/** PRINT LIST FRIEND **/
...
}
?>

<?php
/** index.php **/
include('functions.php');
startSession();
$userid = 'HENRY';
if(!isset($_SESSION['user_id'])) {
$_SESSION['user_id'] = $userid;
}
?>
<html>
<head>
<title>Load List</title>
<script src="controller.js" type="text/javascript"/>
</head>
<body>
<div id="list_container">
<div id="list_member_friend" class="list_member_friend">
<!-- this is where it generates session error -->
<!-- load list of members friend with ajax script controller.js from member.php -->
</div>
</div>
</body>
</html>

在member.php中我也开始 session

<?php 
/** member.php **/
include('functions.php');
startSession()
$db = new MySQLi('localhost','root','abcdefg','social');
$query = "SELECT fid, fname, fage, fgender FROM memberFriends";
$query .= " WHERE memberid = '" . $_SESSION['user_id'] . "'"; <!-- this is the problem -->
$db->prepareQuery($query);
$db->execute();
..... (Load List);
?>

问题:

  1. 为什么刷新页面后session值没有传入member.php?

  2. 将 session 值传递到的最佳方法是什么 成员(member).php?

最佳答案

存在语法错误。应该是 -

if(!isset($_SESSION['userid'])) {

isset是一个函数。它应该由 isset() 调用,而不是 isset[]

关于php - 页面刷新时 session 被破坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30723452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com