[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Session / variables no longer available (PHP)

Posted on 2016-10-24
4
Medium Priority
?
106 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 84

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
Read about the 3 stages of the buyer's journey: awareness, consideration, and decision.
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…
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 …
Suggested Courses

650 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