14

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

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.

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

Main Page PHP:

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

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.

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.

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.

5
  • When I use session_start() in the new file it creates a new session. Is there anyway to link these sessions? Commented Aug 1, 2012 at 23:07
  • What "links" the sessions is the session cookie, which should be in the Cookie header of the HTTP request... Commented Aug 1, 2012 at 23:15
  • You're not using a load-balanced webserver, are you? (when using the default file-based backing store for PHP sessions, there is naturally no way for hosts to share session information) Commented Aug 2, 2012 at 5:25
  • I am using a paid webhosting provider, IPage and this may well be the case. I guess the thing to do would be to create a local lamp server and see if it does the same thing as my paid hosting provider. Thank you for you input. Commented Aug 2, 2012 at 16:22
  • 1
    Agree that testing on a local server would add useful information. PHP allows you to define & register your own methods for reading and writing session information, so if your hosting provider offers DB access for persistent storage then you can use the DB instead of the file based backing store for PHP sessions, which should work properly in a load balanced environment. Commented Aug 2, 2012 at 18:38

6 Answers 6

27

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

You need:

<?php
session_start();
if(isset($_SESSION['views']))
    { echo "Views: " . $_SESSION['views'];} 
    else 
    { echo "Views: NOT SET";}
?>
Sign up to request clarification or add additional context in comments.

6 Comments

This creates a new session and $_SESSION['views'] are different numbers for the main page and sub page.
Is this on a different subdomain or path on the server? It should use the same session if the cookie matches.
I just ran session_id() from both files and it is the same. but it still wont pull the variable.
Can you monitor the HTTP headers sent to and from the server to make sure the same session cookie is being sent between the two pages? Maybe try var_dump($_SESSION); and see how the two differ. They should share the same data if the same session is in fact being used.
For this question you were the first to answer correctly, adding session_start() would fix the problem normally but in my case something else strange is going on and I will keep working towards fixing it. Thank you for you helpful advice. There is only one cookie and the web headers are the same but for some reason php will not get the session variable and there are no errors in my log. I will try on a local LAMP server.
|
6

You need to start session session_start() in the other PHP file also, the one you are calling through AJAX.

Comments

2

I ran into what i thought was the same issue when running PHP 7 on IIS Server 2012 today.

I had added:

if(!isset($_SESSION)) 
{ 
    session_start(); 
} 

to the start of each AJAX file but kept recieving the following PHP Notice:

PHP Notice: A session had already been started - ignoring session_start()

A bit of searching lead me to this thread which pointed me in the right direction to resolving the issues I encountered. Hopefully the following information will assist others encountering the same issue.

After checking the session.save_path value was set, in my case C:\Windows\Temp, I thought it best to check the folder permissions match those of the user account I was running IIS under.

In my case it turned out that the directory I had nominated for session storage (in php.ini) did not have the same user (security permissions) assigned to it as the one which was running the IIS site.

Interestingly sessions worked fine when not using AJAX requests prior to me adding the new user permissions. However AJAX did not pick up the session until I had corrected the permissions issue. Adding the same user account that IIS is running under immediately resolved this issue.

Comments

1

In the case of using a paid web hosting service the default session save path is automatically set like this:

http://php.net/session.save-path
session.save_path = "/tmp/"

You need to place the static path to your root folder there.

1 Comment

where's there (the default session save path) ? Where can I find it?
1

You're trying to use existing session data from your application in an ajax call. To do that, change how you're calling session_start like so:

// With ajax calls
if (session_status()==1) {
    session_start(); 
}

When making ajax calls to php scripts that need existing session data, use session_start after session_status.

http://php.net/session_status

Comments

0

Need to initialize the session before you trying to login through ajax call.

session_start();

Initialize on the top of the page from where you start the login ajax call.

So that the SESSIONID will be created and stored the browser cookie. And sent along with request header during the ajax call, if you do the ajax request to the same domain

For the successive ajax calls browser will use the SESSIONID that created and stored initially in browser cookie, unless we clear the browser cookie or do logout (or set another cookie)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.