Use Session in Parent Folder in PHP - 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.

Related

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 ?

Does a PHP session work across subdirectories?

I have a main directory named System with a sub-directory named Subsystem. My session from main directory is not working in the sub-directory.
When I echo session_save_path(); in both folders, they show me "/tmp".
Then, I tried to put session_save_path("../tmp"); in my sub-directory but it shows me "This webpage has a redirect loop".
session.php in System directory:
<?php
session_start( );
if (!($_SESSION['uid']))
{
header("Location:index.php");
}
else
{
$_SESSION['uid'] = $_SESSION['uid'];
}
?>
session.php in Sub-system folder:
<?php
session_save_path("../tmp");
session_start( );
if (!($_SESSION['uid']))
{
header("Location:index.php");
}
else
{
$_SESSION['uid'] = $_SESSION['uid'];
}
?>
I have Googled all over, but I still cannot get it to work.
The directory does not affect your session state (all directories of a given Apache-PHP website will access the same session in a standard configuration). You should not have to use session_save_path().
I think the problem in part is that you're setting 'uid' to itself ($_SESSION['uid'] = $_SESSION['uid'];) - therefore potentially never actually setting it to a value - and potentially redirecting indefinitely if it's not set.
I suggest this simple test to ensure that your sessions are, in fact, working:
/session_set.php
<?php
session_start();
$_SESSION['uid'] = 123;
/sub_dir/session_get.php
<?php
session_start();
echo $_SESSION['uid'];
A session creates a file in a temporary directory on the server where registered session variables and their values are stored. This data will be available to all pages on the site during that visit.
The location of the temporary file is determined by a setting in the php.ini file called session.save_path. so pleasse check this path.
Also [session-save-path()][1] Get and/or set the current session save path.
I think u dont need to write this line and check your php.ini for correct path.
for session i found some useful article http://www.tutorialspoint.com/php/php_sessions.htm
Thanks.

Sessions Management

I have problem with session. I create a different Session file with name Session.php & want to include this file in each page of website to manage session. can it work? suggest me please I am a fresher and working first time woth session .
my session.php file is as follow
<?php include("lib/config.php");
function start_session() {
$_SESSION['userid']=$rows[0];
$_SESSION['first_name']=$rows[2];
$_SESSION['last_name']=$rows[3];
$_SESSION['user_email']=$rows[4];
$_SESSION['user_password']=$rows[5];
$_SESSION['user_gen']=$rows[6];
$_SESSION['user_contact']=$rows[7];
$_SESSION['picture']="profile/".$rows_pic[0];
$user_id=$_SESSION['userid'];
$fnam=$_SESSION['first_name'];
$lnam=$_SESSION['last_name'];
$email=$_SESSION['user_email'];
$pic_ed=$_SESSION['picture'];
}
function destroy() {
session_destroy();
}
?>
You need to include session_start() at the top of your file then your function will surely work.
1) You have used $rows in function, what is that variable?
2) yes, you can include that file in all pages, but better you create session.php under lib folder and include that file in lib/config.php. So you just need to include config.php in all pages.
3) You need session_start() at very top most code of your php file.
What is the use of creating session.php file?
You can access session like that:
#session_start();
if you want to write new session than use like that $_SESSION['MySession'] = 'XYZ';
Accessing Session echo $_SESSION['MySession'];
You can access session $_SESSION['MySession'] on every page but you need to include #session_start() at the top of every php file.

session id changing on ajax call to file in subfolder

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

PHP Session in different folder

Problem PHP session in different folder.
I have problem with PHP session.
There are two folder: A AND B When I already logged in Folder A then i click link access to folder B when come to index.php file in Folder A. It doesn't to recognize session state.
PHP Code:
if(!isset($_SESSION))
{
session_cache_expire (21900);
$cache_expire = session_cache_expire();
session_start();
}
How can i check session redirect ?
f(!isset($_SESSION['a'])){
redirce to a
}
f(!isset($_SESSION['b'])){
redirce to b
}
Best Regards
Try this:
if(!session_id()) {
session_start();
session_cache_expire (21900);
}
Check your phpinfo() and look for suhosin.
If it is installed then you should disable it in order to let sessions be shared betwen different paths.
This is happening because the validity scope of the PHPSESSIONID cookie is restricted to the folder A, so in folder B it doesn't get fed the same cookies. You have to change that to the parent folder containing both A and B.

Categories