?
Solved

Session / variables no longer available (PHP)

Posted on 2016-10-24
4
Medium Priority
?
95 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 9

Accepted Solution

by:
Brian Tao earned 2000 total points
ID: 41857959
Did you put session_start(); on the other PHP scripts, too?
2
 
LVL 22

Expert Comment

by:Kim Walker
ID: 41857971
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 83

Expert Comment

by:Dave Baldwin
ID: 41858159
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 111

Expert Comment

by:Ray Paseur
ID: 41858187
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

800 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