Found the solution. Solution at the bottom of the post
I have some code in php using sessions (I'm just testing them out - I want to use them in a login system).
test1.php:
<?php
session_start();
$_SESSION["test"] = "works";
echo $_SESSION["test"];
?>
test2.php:
<?php
echo $_SESSION["test"];
?>
test1.php output the correct value (where I wrote echo $_SESSION["test"];), however when I switch to test2.php, there's nothing. I have checked the cookies (both websites have the same session cookie). Could the problem be a server error?
Found the solution. A simple error like that can create a big problem. At the time, I did not realize that I had to have a session_start() at the beginning of every php webpage that I used session variables in.
There must be a sesssion_start(); at the beginning of EVERY php webpage that you are using session variables in
Related
I have created a very basic script to test $_SESSION parameters, and on my server this works perfectly:
<?php
session_start();
if(!isset($_SESSION["favcolor"]))
{
$_SESSION["favcolor"] = "green";
}
?>
<html>
<head>
</head>
<body>
<?php
echo "<pre>";
print_r($_SESSION);
echo "</pre>";
?>
<br /><br />
Go To Next Page
<br />
Delete Session
</body>
</html>
Page 2 is the exact same, only using a second Session variable, and the kill link does a session_unset() and a session_destroy(), then redirects to test1.php. Again, on my server this code works perfectly.
However, the exact same code (simply copied and pasted without any changes) on the server of my client does not work. Between test.php and test2.php the session variable set in test1.php gets lost. Similarly, going back, the variable set in page 2 is getting lost.
I have been looking at the phpinfo() stuff to see if I see clear differences, but I am at a loss here...
This can be caused by a lot of things, but just to start troubleshooting:
This function returns True or False, depending if it could start the session or not, so maybe you can print this returned value, to check wether it is actually saving the value for you or not.
If not, you surely ran into a php.ini configuration issue and you'll have to check these values on your client's server.
You can check the documentation for session_start() at php's official site
This was due to a mis-configured session.save_path.
After pointing it to a proper folder, things are working properly
I have two pages which I need to pass session information between them. Here's page one.
<?php
session_start();
echo session_id()."<br>";
echo $_SESSION['test']."<br>";
Page two.
<?php
session_start();
echo session_id()."<br>";
$_SESSION['test'] = 'test';
echo $_SESSION['test'];
Page two is in a different directory (same domain), and has Windows Authentication on. Page one is using anonymous authentication. From the output I can see the session ID is the same, but page one doesn't echo the test variable set from page two.
I'm running PHP in IIS 8.5 on Server 2012 R2.
Any help is appreciated.
To clarify, I am calling page two first, and page one will not show the variable.
You need to initialize the variable with a value first before using it.
First run page2.php so, that value of $_SESSION['test'] is set to test
using $_SESSION['test'] = 'test';
Now, run page1.php there you can see the value of $_SESSION['test']
using echo $_SESSION['test']."<br>";
you can also follow these steps to make use of session in a simple way so that you don't have to do session_start() again and again
1. config.php having session_start();
2. page1 having include_once('config.php');
echo session_id()."<br>";
echo $_SESSION['test']."<br>";
3. page2.php having include_once('config.php');
echo session_id()."<br>";
$_SESSION['test'] = 'test';
echo $_SESSION['test'];
Try closing your session in the first page, it's possible that your session file is still open when the second page is called, so you're not seeing the data. Try adding session_write_close() to the first page called.
http://php.net/manual/en/function.session-write-close.php
The issue was with permissions on the session save path. I changed the path to where both directories could write and it works.
My session does fails to resume when opening another file
I have 2 pages, test.php and test2.php. Each of my files are listed below.
test.php:
<?php
session_save_path('sessBin');
session_id('mySessionID');
session_start();
$_SESSION[1]="yo";
echo $_SESSION[1];
?>
go to test 2
test2.php:
<?php
session_save_path('sessBin');
session_id('mySessionID');
session_start();
echo $_SESSION[1];
echo "bleh";
?>
The first page returns:
"yo" go to test 2
however, when clicking on the link to go to test2.php, only this returns:
"bleh"
I did some research to find that cookies must be enabled. Well, they are... So what is wrong?
NOTE: (A discovery from afterwards)
I did notice that there were several files(including sess_mySessionID) in sessBin so the session is stored. However, the files are empty and the session does not seem to resume
test1.php
// session_save_path('sessBin');
// session_id('mySessionID');
session_start();
$_SESSION['test'] ="yo";
echo $_SESSION['test'];
go to test 2
test2.php
// session_save_path('sessBin');
// session_id('mySessionID');
session_start();
echo $_SESSION['test'];
echo "bleh";
Worked for me !
session_start needs to be defined at the top of every page, right after the opening php tag, on every page where the session data is expected to exist. You must do this before addressing any other session function or variable.
** Update to Answer Based On Your Comments **
check for white space before the opening php tag in test2.php. Also, enable error reporting. You might be sending something out before the session call that is causing session_start() to fail.
Try this:
config.php
<?php
session_start();
?>
In all your other files at the top add this:
include'config.php';
That way you will always have a file where you can put important stuff you might need on other pages as well, so you don't have to retype it every time.
I want to use session in PHP. But its showing some problems in my scenario.
I want to share same session in 3 different PHP files.
./sessionTest/testing1.php
./testing2.php
./testing3.php
if i store some information in $_SESSION in testing1.php, i cant access the same information in other 2 files
what should i do to make these 3 files share the same session instance?
Is there any other(except cookie) to make this possible?
P.S. These 3 files are executed by different calls, cant include one file into another using include() or require() functions.
Added session_start() at the top but still doesnt share the same session.
Like so :-)
//<-- testing1.php -->
<?php
session_start();
$_SESSION['value'] = "Text!";
?>
//<-- testing2.php -->
<?php
session_start();
echo $_SESSION['value']; //Text!
?>
See this tutorial about: PHP Sessions
Maybe it can help you to understand the working with sessions
Get hold of iehttpheaders for MSIE or web developer toolbar/firebug for Firefox and check to see if a cookie is being dropped by your PHP code / presented. Also check the path and flags on the setcookie header.
Are PHP errors disabled? If so, you could have a problem in code and just not seeing it? I've had this happen where I had some white-space in the output stream before starting the session, meaning that the session broke because the session header wasn't sent first. Of course not having php errors displaying it wasn't obvious as to why the variables were null.
Just an update as I was having some issues here. Using session_name() can be helpful if the site has multiple sessions/cookies. Look in your browser preferences to see what cookies the browser is storing. I've found you have to flush these cookies a lot to see what is going on if you are using different sessions.
<?php
session_name('mySession');
session_start();
$_SESSION['value'] = "Text!";
?>
<?php
session_name('mySession');
session_start();
echo $_SESSION['value'];
?>
I'm writing a user login system, and I (like so many others) am having a problem with my sessions.
Here's the pointer from the login script when the inputs are validated:
session_start();
$_SESSION['id']=$id;
header('location: memberhome.php');
Here's the first thing on memberhome.php:
<?php
session_start();
$id=$_SESSION['id'];
?>
And later in memberhome.php:
You are logged in as: <?php echo $id; ?>
The problem is $_SESSION['id'] is apparently empty so the echo $id prints nothing.
An alternate that also does NOT work:
//removed session_start and $_SESSION bit from the top
You are logged in as: <?php session_start(); echo $_SESSION['id']; ?>
NOW, here's the weird part. This method DOES work:
You are logged in as: <?php echo session_start();$_SESSION['id']; ?>
You can see the session_start() is moved AFTER the echo. This works when the page loads from the login script. However, upon refresh, it does NOT work once again.
I've tried a bunch of alternatives and spent a few hours searching for answers in previous questions. I also looked at my phpinfo() for something fishy and found nothing. This is entirely what my progress is hinging on. Thanks!
First of all, please enable debugging:
error_reporting(E_ALL);
ini_set('display_errors', '1');
Second, session_start() needs to be at the top of the page. So the line you wrote;
You are logged in as: <?php echo session_start();$_SESSION['id']; ?>
will never work.
The following line needs to be on top of the page, before any HTML etc.
<?php
session_start();
$id=$_SESSION['id'];
?>
Have you tried:
print_r($_SESSION);
to examine the contents of the session?
Make sure you're calling session_start() before you output anything on the page. The standard cookie-based sessions require some header information to be exchanged, which must be done before you send any content.
You're most likely running into output buffering, which is why it sometimes works and other times it does not. Generally speaking, stick to starting the session before any output is generated, you'll find your code works better.
use
ob_start(); #session_start();
on the top of the both page