PHP SESSIONS data getting cleared? - php

I am able to print out the data contained in $_SESSIONS right before a header(Location) redirect and a die() after the header redirect.
However, when I am redirected, there is no data contained in $_SESSIONS. I've checked the session_id() that they're the same. I've tried session_write_close() right before the redirect and that doesn't help. What else could be the problem?

Make sure you are using $_SESSION rather than $_SESSIONS. Also, make sure the current session is loaded in the script with session_start();
If you are sure the code is correct, run the script a couple of times and check your PHP session temporary directory. The problem could very possibly be the PHP server does not have the correct permissions to write to the session files directory.

Related

Session doesn't get passed from page to page with eval()

I'm trying to get a session to pass from page to page while using eval(). Basically I have one page that handles all other requests and just gets the pages output via an eval() call.
Everything works fine, but for some reason the session information keeps resetting on every refresh. The login system, which also uses sessions, doesn't reset with every page refresh, though.
If you go to http://fretfast.com and view the source code, you can see the contents of $_SESSION starting on line 221.
My question is, how does the login system still work but the other session information keeps getting reset? The firstActivity and lastActivity variables are set on the configuration page that is included on the main file which handles all requests. These only get set if a session has not already been started, like so:
if ( session_id() == '' ) {
session_start();
// set other $_SESSION['trail'] variables
}
The requests and requestTimes variables are set inside the object that retrieves a given page's contents via eval().
If anyone has any idea what the problem may be or needs any information I would be glad to provide it. Thanks in advance.
Your check never evaluates to true, so the session_start() never executes.
Unless you specifically changed (or emptied) the session id (either by code or in your php.ini), it defaults to PHPSESSID (and a quick firebug check to your url confirms that).
Skip the check altogether, and just issue the session_start() at the beginning of your file.
P.S. Why do you use eval() ? NEVER use eval() !

PHP Session variable not accessible

I am having a problem in accessing a session variable.I have one page lets say test.php, when i use print_r($_SESSION) here, it prints all the session data.But when i use Redirect then i am unable to access session data on test2.php i-e print_r prints empty array, even though i have session_start() at the top of my script.
Then i tried header("Location: test2.php") and now session data is accessible.
But i want the page to be redirected on onClick of a button.
Please help ..
Make sure you have session_start(); in the head of both files and you're not browsing in a private browsing mode.
I was using the path http://localhost/project/orders.php in href then i changed it to just orders.php and it worked
Cheers
Sometimes, accessing with http or https or http://www can make the difference of variables in session being accessed or not. Please browse through all in order to be certain, as sometimes saved urls are with www whereas the session is created with simple http. It matters in session accessibility.

PHP $_SESSION not working as expected

I have a PHP website I'm maintaining and I've confirmed that this worked at one point.
We have a website utilizing a login system which stores a logged in user's information in a $_SESSION['user'] variable. The site used to log out the user when clicking /logout.php which essentially removed that portion of the session, then header() redirected to the homepage.
As of recently, the /logout.php file with session_start() at the top somehow doesn't see the session information when print_r() is used to output it for debugging purposes.
If I go to another page, I see the session info just fine, but not on the logout page...which is exactly why I cannot remove the session info, because it's not accessible.
I thought $_SESSION was global on the site until the browser was closed. I've never had this happen and I know the session instance was started on this page, so it's weird that it's not showing me the session data.
Any ideas? I'm totally stumped on this one!
Code: /logout.php
<?
#session_start() is inside this file
require_once($_SERVER['DOCUMENT_ROOT'].'/includes/config.php');
unset($_SESSION['user']);
header("location: /");
exit();
?>
The checking of $_SESSION['user'] is site-wide and I call to various items below it when needed for different things. Someone else built this site and I'm trying to debug why it's not working for them all of a sudden.
If the domain/subdomain is the same as the rest of the page, I would say this sounds like a typical session vs. output error. Make sure you have enabled all errors, and display them, as you might have printed output to the client before calling session_start(). This will break the function and making sessions unavailable.
To fix the problem(if it is the case), you should remove all output before session_start. Even a space before <?php will be considered output by Apache(and other). Also make sure you have disabled BOM(Byte Order Mark) in the document(any decent editor will let you change this, just look for something like "Current file setings").
Always remember the first line of your PHP code should be session_start(); and nothing else. If all your going to do is unset the session variables and destroy the session, Try removing the require_once($_SERVER['DOCUMENT_ROOT'].'/includes/config.php'); and add the session_start() and the session_destroy() at the end of the logout.php file and see if it works.
Are you accessing logout.php from the same exact domain that you set the session to begin with (i.e. example.com vs. www.example.com/logout.php)
As for just unsetting specific session data, it would be best to call session_destroy() and then unset your cookies to kill the session.

$_SESSION variables not transferring from page to page

If I write the following code:
session_start();
$_SESSION['user_id']='daniel';
the variable stays fine as long as I'm on the page on which it was created, and the second I try to call $_SESSION['user_id'] from another page, I don't get a response.
Can anyone tell me what mistake I'm making?
You should be using session_start() on every page you want to use sessions on.
As long as:
You are doing session_start() on the other page. Note: you don't make this call once. You do it on every page that wants to access the session information;
The other page can see your cookie from this site (ie sufficiently similar domain); and
The other page is running on the same server.
then it can see it. Construct a simple test case and verify this and then work out why what you're doing is different.
You must have session_start() on every page
Ensure that the PHPSESSID cookie is actually being set, and that no headers / content have been sent before you call session_start()

PHP: Possible reasons for $_SESSION['var'] to be mysteriously undefined with flash upload

I am having trouble with a session id not being defined.
The session variable is being called on a page that a form posts to via an ajax/jquery query.
The session variable is called on every other page without a problem. The variable is set when a user logs in, and is checked by an auth.php file. The auth.php file finds the session variable just fine, however, the insert.php file spits out an error: "Undefined index: var....".
Is there a reason for a session variable to get lost?
EDIT:
I am calling session_start in my auth.php file. I get an error message if I try to start another session.
Is it possible that the session variable is not being found because the file is posted to via an ajax request?
EDIT/ANSWER:
I found out what was wrong. I was using a flash uploader, and sessions are lost when making an upload with flash. Oh what a night.
Thanks for all your help!
Is the page 'insert.php' has session started? It may be the reason.
Try to start it if the PHP setting is not set to auto start.
session_start();
I also find out print out the $_SESSION sometimes handy.
print_r($_SESSION);
From my experience, there is another possibility, which is the other file is called from different host/servername/IP number, which cause the session variables not found. Although this is quite remote possibility.
In your case, try to check it out just in case.
I found out what was wrong. I was using a flash uploader, and sessions are lost when making an upload with flash.
If you redirect not include insert.php you have use session_start();, because session not existing in that page, as far as I know php sessions working only in one page without extra configuration.

Categories