Solved

Session / variables no longer available (PHP)

Posted on 2016-10-24
4
50 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
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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
calculated column 12 74
CONDITIONAL MAX STATEMENT IN T-SQL 3 37
Please show me easiest post form. 3 31
html input type 3 21
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

937 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

10 Experts available now in Live!

Get 1:1 Help Now