sabecs
asked on
PHP session variables not passed
I have a file that checks login details and sets session variables as below:
File is located in www.mywebsite.com/subfolder/check_login.php
$_SESSION['user'] = "OK";
$_SESSION['id'] = $row_user['id'];
$_SESSION['contact'] = $row_user['contact'];
If I execute my test.php file below in www.mywebsite.com/subfolder/test.php
Session variables are OK,
But if I put test.php in my root folder www.mywebsite.com/test.php session variables are not found?
Test.php
<?php session_start();
echo "session user".$_SESSION['user'];
echo $_SESSION['userid'];
echo $_SESSION['contact'];
?>
What is causing this and how can I rectify?
Thanks in advance for your feedback.
File is located in www.mywebsite.com/subfolder/check_login.php
$_SESSION['user'] = "OK";
$_SESSION['id'] = $row_user['id'];
$_SESSION['contact'] = $row_user['contact'];
If I execute my test.php file below in www.mywebsite.com/subfolder/test.php
Session variables are OK,
But if I put test.php in my root folder www.mywebsite.com/test.php session variables are not found?
Test.php
<?php session_start();
echo "session user".$_SESSION['user'];
echo $_SESSION['userid'];
echo $_SESSION['contact'];
?>
What is causing this and how can I rectify?
Thanks in advance for your feedback.
did your check_login.php page has the session_start() define in the top of the page?
Session variables may be influenced by the subdomain or directory structure because session cookies are not set with all the parameters you want. In other words, a session cookie from the "main" page may not be available to the other pages that are not in the same directory tree.
You may want to set your session cookie yourself, something like this:
You may want to set your session cookie yourself, something like this:
// START THE SESSION AND SET THE COOKIE
$sess_name = session_name();
if (session_start())
{
// MAN PAGE http://us.php.net/manual/en/function.setcookie.php
setcookie($sess_name, session_id(), NULL, '/');
}
You have included the check_login.php file in the test.php file.
Please check the path is correct or not.
Please check the path is correct or not.
Also, this article may be helpful to you. Best regards, ~Ray
https://www.experts-exchange.com/articles/Web_Development/Web_Languages-Standards/PHP/PHP-login-logout-and-easy-access-control.html
https://www.experts-exchange.com/articles/Web_Development/Web_Languages-Standards/PHP/PHP-login-logout-and-easy-access-control.html
Session variables are specific to a domain, so if your domain name points to the subdirectory, then any session variables are not available to any script not beneath that domain name.
looks like two diffrent sub folders to me.. u are testing it in www.mywebsite.com/test.php where the check login is www.mywebsite.com/subfolder/check_login.php
they should both be the same e.g
www.mywebsite.com/subfolder/check_login.php
www.mywebsite.com/subfolder/test.php
they should both be the same e.g
www.mywebsite.com/subfolder/check_login.php
www.mywebsite.com/subfolder/test.php
ASKER
Thanks for your feedback, I can see session variables getting created in
www.mywebsite.com/subfolder/
Can I get www.mywebsite.com/test.php to look for sessions in www.mywebsite.com/subfolder/
www.mywebsite.com/subfolder/
Can I get www.mywebsite.com/test.php to look for sessions in www.mywebsite.com/subfolder/
why not just put them both in the same folder?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Ray.
Thanks for the points - it's a good question. ~Ray