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
Related
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
Please note that I have started session in both of the pages, but I am still having the problem.
I have saved some value in a session variable, and then echo it on another page, but it always shows empty value, even I have tried the same thing on a different server, where it runs perfectly, it is just creating on client's server. I have searched about this, someone says the values is not storing in tmp folder. I don't know what to do.
Here is my code:
<?php
include_once("config.php");
if(isset($_POST['book'])):
$_SESSION['tripcode']="hello"; //Test value
forceRedirectAdmin("login.php");
endif;
?>
I echo the session value on login.php page, but it shows empty, even I have included config.php file in login page also.
My PHP sessions are being created but $_SESSION variables are not being written to the session file.
I have three test PHP pages here that I'm using to diagnose this problem:
test_a.html:
<html>
<body>
<form action='test_b.php' method='post'>
Put something here:
<input type='text' name='saveme' />
<input type='submit' value='Go!' />
</form>
</body>
</html>
test_b.php:
<?php
session_start();
$_SESSION['saveme']=$_POST['saveme'];
echo "Here's what you wrote:<br>".$_SESSION['saveme']."<br>".
"<a href='test_c.php'>Take me to the final check</a><br>";
echo "And here's your session_id: ".session_id();
session_write_close();
?>
test_c.php:
<?php
session_start();
echo "Here's what you wrote (maybe?):<br>".$_SESSION['saveme']."<br>".
"You should also see saveme below:<br>";
foreach ($_SESSION as $key=>$val)
echo $key." ".$val."<br>";
echo "And here's your session_id: ".session_id();
?>
When opening test_a.html and typing in anything to the textbox and hitting Go!, it will show up correctly on test_b.php (when it is set and recalled from memory) but it is not shown on test_c.php.
The session_id is set and shows to be the same. The cookie is stored correctly. The session file is created correctly in the filesystem, but is not written to and remains a zero-byte file.
Sessions are now not being written on any pages of my site (despite working for well over a year up to this point) so typos in these code snippets (if any) are likely irrelevant.
Things I've checked:
The session storage directory is writeable and the disk has plenty of storage
session_start is always successful (and there is nothing before it)
The session_id is created correctly and is the same on both pages
The session cookie is created with the correct session_id
session.use_cookies and session.use_only_cookies are on
Register globals is off
Cookies are enabled
Cleared the /tmp directory to reduce possibility of filesystem issues
Tried storing the session data in memory (didn't work)
Tried changing the session file location (didn't work)
Tested on Chrome & Firefox
Ideas?
Your session storage directory may be writable but the file is not. Try setting the rights to 0666 on the session file and check if this fixes the problem. It's something like one process is creating the file and the very next is not able to write it. What's the owner of the file at creation time? Check if it's www-data or whatever runs your php.
PHP session keys are separated by the pipe character | so | in key name will break you session. Maybe this is the problem.
Check this:
enter link description here
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 have a simple form which passes a session variable and it simply fails to load on the second page. I had it running on another server, and after moving it to a new one, it no longer works. I have same PHP version (PHP 5) on both, and it works on one and not on the other - the $_SESSION array is just completely empty.
I checked to see if the session id's were the same, and they are exactly the same on both pages of the form (NOT on both servers, these are obviously different).
session_start(); is the first line of code on all pages of the form.
First Page
session_start();
echo "session id ".session_id();
$_SESSION["gencode"] = $gencode;
Second Page
session_start();
echo "session id ".session_id();
echo $_SESSION["gencode"];
Again, I had it working exactly the same on another server, after the move this part broke, should I be looking for a setting somewhere on the server? Both are Linux, if the session id is echoing that means the same session exists, correct?
Any advice would help.
Check the php.ini on both servers and confirm the session settings are the same.
var_dump($_SESSION) to see what is in the session. You may see something interesting.
Are both pages accessed via exactly the same domain?
Like almost any other PHP function, session_start() can fail, and returns FALSE if it does. Can you check the return value?