本文介绍了PHP会话变量不是preserved与阿贾克斯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用AJAX来加载新的PHP文件,并更新显示一个页面的网站。

I have a one page website that uses AJAX to load new php files and update the display.

我开始我的PHP会议的主页上,但是当我使用Ajax来更新内部HTML我需要这些会话变量被加载新的PHP文件。

I start my php session on the main page but when I use ajax to update inner html I need those session variables for the new php file being loaded.

这篇文章是与此类似: PHP会话变量不是preserved 。但我检查,我的php.ini有 session.use_cookies = 1

This post is similar to this one: PHP Session Variables Not Preserved . But I checked and my php.ini has session.use_cookies = 1

主页PHP:

<?php 
session_start();
if(isset($_SESSION['views']))
{$_SESSION['views']=$_SESSION['views']+1;}
else
{$_SESSION['views']=1;}
?>

在用户输入我用AJAX调用PHP文件并加载页面的小节:

After User Input I use ajax to call a php file and load a subsection of the page:

<?php    
if(isset($_SESSION['views']))
    { echo "Views: " . $_SESSION['views'];} 
    else 
    { echo "Views: NOT SET";}
?>

有人能告诉我,我错过了什么重要的一步?谢谢你。

Can someone please tell me what important step I am missing? Thank you.

更新:添加SESSION_ID()调用这两个我看到两个页面具有相同的Session_ID主,副页之后。然而,它仍然无法拉动会话变量,如果我做给它赋值两个名称相同的会话变量互相保持独立。

Update: After adding session_id() call to both the main and sub pages I see that both pages have the same Session_ID. However it still cannot pull the session variable and if i do assign it a value the two same name session variables stay independent of one another.

答到,这个问题产生了一个问题:我发现我必须设置在我的php.ini文件中的静态session_save路径。对于大多数付费的虚拟主机服务,他们只对会话默认的容器中,但它是受负载均衡。什么是releif。

Answer to the question that this question created: I found that I had to set a static session_save path in my php.ini file. With most paid webhosting services they just have a default container for sessions but it is affected by load balancing. What a releif.

推荐答案

我觉得你错过了在session_start()的Ajax调用的页面上。

I think you're missing session_start() on the page that Ajax calls.

您需要:

<?php
session_start();
if(isset($_SESSION['views']))
    { echo "Views: " . $_SESSION['views'];} 
    else 
    { echo "Views: NOT SET";}
?>

这篇关于PHP会话变量不是preserved与阿贾克斯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 01:15