Solved

Session / variables no longer available (PHP)

Posted on 2016-10-24
4
74 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 500 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 110

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

Technology Partners: 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

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

731 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