Solved

Session / variables no longer available (PHP)

Posted on 2016-10-24
4
43 Views
Last Modified: 2016-10-25
I am trying to use php sessions on my site but the data is not available to the php ajax scripts or other pages.

It is pretty simple, I'm using:

session_start();

Then I set some session variables:

$_SESSION["myvar"] = "blah";

The session variables are available to the page itself but if I go to any other page on my site now and run

echo $_SESSION["myvar"];

I get NULL

Also I'm running some simple ajax calls to php scripts within the same page and when I check the session variables in those I also get NULL.

What's going on?
0
Comment
Question by:intoxicated_curveball
4 Comments
 
LVL 9

Accepted Solution

by:
Brian Tao earned 500 total points
Comment Utility
Did you put session_start(); on the other PHP scripts, too?
2
 
LVL 21

Expert Comment

by:Kim Walker
Comment Utility
session_start(); must be executed before the file generates any output. If you have any echos or header statements before session_start();, the session will not start.
1
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
To reiterate what Brian and Kim have said, every one of your PHP pages that are involved in your sessions in any way show have this as the first 3 lines of your PHP code.  This includes all pages that are called by AJAX routines.  The error reporting is there so you can see any error messages that are generated.  This is especially important when you are developing the site code.
<?php
error_reporting(E_ALL);
session_start(); 

Open in new window

1
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Here is how PHP sessions work.
https://www.experts-exchange.com/articles/11909/PHP-Sessions-Simpler-Than-You-May-Think.html

If you're following the guidance in that article and still "losing" session data, try running the script captioned A Script to Test Your Session Handler.  If that works, but you still have trouble, please set up the SSCCE and post it here, so we can install it and run it on our own systems.  

Most of the time this failure is about omitting session_start() or putting it inside some conditional logic, however in AJAX scripts it may be wise to use session_write_close() before the asynchronous script ends.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Read about the 3 stages of the buyer's journey: awareness, consideration, and decision.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now