session id changing on ajax call to file in subfolder - php

I am calling an ajax file in my top directory ("/filename.php")
The ajax file is located in a sub directory ("/uploads/filename.php")
When I call the start_session() function and then echo the session id at the top of each page, the session id is different on the file in the top directory than the session id on the file in the sub directory.
I need access to the session variables created in the top directory from the sub directory. What do I have to do to accomplish this?

The session ID should be the same, regardless of what directory your script is in (unless server-side config defines otherwise). You are not naming your session are you? If not, you can try naming the session example:
file.php
session_name("example");
session_start();
echo session_id();
other/file.php
session_name("example");
session_start();
echo session_id();

I don't know how I can do this, but I can pass the session variables for a username and password via ajax, check them with the database, and then get information based on that

Related

Accessing custom sessions

I have a page that creates different sessions depending on different users it renames the session file to fit that user
session_name("name");
session_start();
//i then put in a variable in the session file
$sn=$_POST["name"];
$_SESSION['name'] = $sn;
now in another file I want to access the session with the session name I just created
session_start();
echo $_SESSION['name'];
but it just creates a new session file and says undefined variable $_SESSION['name'].
Please see the following
Using session_name() in PHP - Cannot Access Data
TLDR: At each startup request time (as already mentioned) the session will be renamed to PHPSESSID unless you call session_name( 'fObj') before session_start() on every page

PHP sessions and previous dir

I have catalog named "scripts"(PATH: /home/olo/www/site/scripts). In this catalog i have file called "login.php". This file create SESSION after user login. I have file called "index.php"(PATH: /home/olo/www/site). My SESSIONS created in "scripts" don't work in "index.php". It showed me, that SESSION isn't set. Can anybody help me ? Thanks a lot.
You must start session in every page after login
try to add this code at the top of your index.php file
<?php
session_start();
?>
Your session isn't restored. You'll have to call session_start()
See: PHP docs
session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.

Accessing session vars outside the directory they were created in

I have never ran into this problem before and I have been trying to troubleshoot this for quite a while now.
I am setting my session variables on the main.php page and when I do a var_dump() on a page in the root directory (note the paths), it prints out an empty array.
It seems to only pick up the session variables within the same directory they are created in. However, I need to access session vars in at least the two directories mentioned below. Is this possible? I appreciate any suggestions on how to accomplish this.
It is important that the changes I make to the session variables in one directory reflect in the other (i.e. unset, update value, etc.).
Edit: All sessions are started in the same parent directory, the only problem is many session variables are created in the sub-directory and then need to be accessed in the parent (root) directory.
Many thanks in advance!
actions/search/main.php
session_start();
$_SESSION['id'] = 22;
$_SESSION['name'] = 'Bob';
manageajax.php
session_start();
if(isset($_GET['handleSess']) && $_GET['handleSess']==1){
//do stuff with session vars
var_dump($_SESSION); // output array(0) {}
}
If I am not mistaken, you have to set the sessions/cookies in the root of your domain (mydomain.com/)so you can access them from any location in your root path. in what path (or file) do you start the session ?

Use Session in Parent Folder in PHP

I have 2 folders/directories:
login/helper.php
dashboard/index.php
I have set a session in helper.php in the login folder. I am trying to retrieve a session on the index page in the dashboard folder. Somehow i cannot retrieve the session in another folder or a parent directory.
Here is the Code on the login/helper.php
session_start();
$_SESSION['userID'] = $checklogin['userID'];
Here is the code on the dashboard/index.php
echo $_SESSION['userID'];
Is there a way to make a session available in a parent directory and all it's folders?
Kind Regards
Just start the session again in dashboard/index.php:
session_start();
echo $_SESSION['userID'];
In your case looks like you need to add session_start() at the starting of your file.
But as per coding standards I would suggest to put that session_start() in a common file and may be try to include that file in your all pages, that way you don't need to include session_start(0) everywhere.

Problems accessing same SESSION variables from different files

I'm developing a site using Wordpress.
My permalink structure is set to show post/page name. So accessing a page called store will look like this: www.mysite.com/store/?some=arguments
In all my WP templates, I'm able to output all my SESSION variables using print_r($_SESSION);
Doing the same from a file called from jQuery.ajax only outputs some of the SESSION varaibles.
I've used the following code to see if the cookie path is same for both files, and they are:
$sessCookie = ini_get('session.cookie_path');
echo 'session.cookie_path: '.$sessCookie;
I also have this code in my files to make sure session is started:
if (!session_id())
session_start();
Why am I not able to output the same session variables from a WP template and a php file called from jQuery.ajax?
UPDATE
jQuery.ajax calls jquery.php file. At the top of this file, it has the following code:
require_once($_SERVER['DOCUMENT_ROOT'].'/wp-blog-header.php');
This code fires functions.php. In this file I have the following code:
function session_manager() {
if (!session_id())
session_start();
// Get variables in query string
$_SESSION['action'] = (isset($_GET['action']) ? $_GET['action'] : '');
$user_geo_data = get_geoip_record();
$_SESSION['user_geo_location'] = get_object_vars($user_geo_data);
}
When functions.php is fired from jquery.php, it seems that session_id() returns false, thus I create a new session.
Is there a way to keep using the same session?
UPDATE 2
It seems that WP config kills all GLOBAL variables when initialized.
http://wordpress.org/support/topic/wp-blog-headerphp-killing-sessions
Wordpress can use its own session handler, and overrides the default session handler to do so. So in essence you've got two different sessions, even though they share the same ID. The cookie path is merely how the client-side cookie operates. What you need to check is session_save_path(), and check if WP is running sessions through the database instead of the default file handler.
The reason two sessions are fired up is because the first one is browser-based (through a cookie) and the second one, with Ajax, is essentially server-side and doesn't have access to the session cookie.
The session cookie is where the session ID is stored and is used to identify an existing session. A server-side Ajax script doesn't have access to the browser's cookies, thus fires up a new session.
It can be worse if the main script uses an alternate session "save handler" than the Ajax script, resulting in two separate sessions, stored in two different places.

Categories