Log in

View Full Version : Stored Sessions Data Not Found



marain
01-04-2014, 03:11 AM
Happy New Years, Folks,

My first 2014 puzzlement (may they be few): I'm attempting to use sessions. My efforts produce error messages. I am hoping someone here can explain what I am doing wrong.

The visitor is presented with a menu that consists of pages s/he can read. One of the menu items is q0.php. In q0.php I define $_SESSION["code"]. Then q0.php causes quotations.txt to load:


<?php
$_SESSION["code"] = "0";
header( 'Location: page.php?here=quotations' );
?>

Note: I have no session_start(); statements because I set session.auto_start to "1" in my php.ini file.


Here is quotations.txt (a grossly abbreviated excerpt), where I attempt to use $_SESSION["code"], defined above in q0.php:



<?php
$lastUpdate = date( "j F Y", filemtime( "pageContent/quotations.txt" ) );

$quotations = array(

"We, as criminal defense lawyers, are forced to deal with <a href='http://en.wikipedia.org/wiki/Torture_memos' target='_blank'>some of the lowest people on earth</a>. People who have no sense of right and wrong. People who will lie in court to get what they want. People who do not care who gets hurt in the process. It is our job, our sworn duty as criminal defense lawyers, to protect our clients from those people.",

"Cynthia Rosenberry",

"For to him that is joined to all the living there is hope: for a living dog is better than a dead lion.
<br />
<br />
For the living know that they shall die: but the dead know not anything, neither have they any more a reward; for the memory of them is forgotten.
<br />
<br />
Also their love, and their hatred, and their envy, is not perished; neither have they any more a portion for ever in any thing that is done under the sun.",

"Ecclesiastes 8:4-6",

);


if ( $_SESSION["code"] == "0"
|| $_SESSION["code"] == "1") {

$bigIndex = rand(0, (count($quotations) -1)) & 65534; // select entry, force even
$_SESSION["quoteIndex"] = $bigIndex;
}

else {
$bigIndex = $_SESSION["quoteIndex"];
}


echo "<br /><br /><div class='bigquote'> {$quotations [$bigIndex]} <br /><br /><div align='right'>"; // display

$noSource = "&nbsp;"; // To test whether we need, or can skip, Part Two

if ($quotations [$bigIndex + 1] != $noSource) { // ...then we have an attribution to display for Part Two,

echo $quotations [$bigIndex + 1] . "<br /><br />"; // so display it already
}


if ( $_SESSION["code"] == "0"
|| $_SESSION["code"] == "2") {

$universe = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 0"; // establish Part Three (partial) universe

$characters = explode(" ", $universe); // make array of it

shuffle ($characters); // go to Buffalo

$characters = array_slice($characters, 20, 3); // trim. The "20" is somewhat arbitrary

$decorations = array( // "Mommy, see all the pretty decorations."
"&spades;",
"<span style='color : #ff4040; '>&hearts;</span>",
"<span style='color : #ff4040; '>&diams;</span>",
"&int;",
"&omega;",
"&pi;",
"&sum;",
"&clubs;"
);

shuffle ($decorations); // to Buffalo, too, the decorations

$decorations = array_slice($decorations, 0, 3); // pare decorations

$decorations = array_merge($decorations, $characters); // meld tree onto decorations

shuffle ($decorations); // again to Buffalo we go, to complete Part Three

$_SESSION["decorations"] = $decorations;
}

else {
$decorations = $_SESSION["decorations"];
}


echo "Your Access Code is " . implode("&nbsp;", $decorations) . ".<br /><font size='-2'>Access code broken? Click <a href='http://www.MarainLaw.com/quotations'>*** here ***</a> to replace it.</font><br /><br /><br /></div></div>";?>

Here are the generated error messages (technically, I suppose, "Notices"):



Notice: Undefined index: code in /var/www/html/pageContent/quotations.txt on line 2956

Notice: Undefined index: code in /var/www/html/pageContent/quotations.txt on line 2957


[Echo'd Quotation, followed by Attribution]


Notice: Undefined index: code in /var/www/html/pageContent/quotations.txt on line 2984

Notice: Undefined index: code in /var/www/html/pageContent/quotations.txt on line 2985
Your Access Code is π j G ♦ ♠ z.
Access code broken? Click *** here *** to replace it.




quotations.txt is a very large file that, as I indicated above, I've heavily excerpted. A version that works is at http://www.marainlaw.com/page.php?here=quotations.

A.

Nile
01-04-2014, 03:31 AM
A few things:


Are you positive that the auto start functionality is working? Remove the header redirect and see if you can get any errors. Even if you're positive it's working, debugging to see if there are errors (make sure you have error reporting on) may point out anything you've missed.
It's unclear what "page.php" is or how it relates to "quotation.txt". Please explain your file setup and why you're executing a text file as a php file.
I see you're using other session arrays in your code. Are those working out fine?
One thing you may want to try is dumping the session array and seeing if everything checks out.

Also, one last thing: reproduce this error. You can't expect to give us a hundredth of what I'd assume to be a huge PHP script and let us diagnose the issue. Reproducing the error with ~10 lines of code (and nothing irrelevant in between) will allow us to see exactly what's going on and how to solve the issue.

traq
01-04-2014, 05:57 AM
Notice: Undefined index: code in /var/www/html/pageContent/quotations.txt on line 2956

Look at the error message: On line 2956 of your quotations.txt file, you try to use the (I'm assuming $_SESSION) index code. Problem is, that index does not exist.

At the most basic level, the solution is to check before you try to use a variable/index that may or may not exist:
<?php

// No
print $_SESSION["index"];

// Yes
if( isset( $_SESSION['index'] ) ){
print $_SESSION['index'];
}

Of course, doing this every time you try to use a variable would be completely impractical - you only need to do this if you are not certain that the variable exists. On the other hand, since you're talking about a session index, I'd guess that it was defined on an earlier page view? If so, then you need to check. You can't assume that it exists.

If the variable/index should exist, then you need to do some troubleshooting to determine why it doesn't. Where do you define this variable? Look at that part of the code and make sure it is working correctly. See if there are any logical mistakes in your code structure (for example, defining a variable inside an if block and then trying to use it in situations where the if was false).

marain
01-04-2014, 01:44 PM
A few things:


Are you positive that the auto start functionality is working? Remove the header redirect and see if you can get any errors. Even if you're positive it's working, debugging to see if there are errors (make sure you have error reporting on) may point out anything you've missed.
It's unclear what "page.php" is or how it relates to "quotation.txt". Please explain your file setup and why you're executing a text file as a php file.
I see you're using other session arrays in your code. Are those working out fine?
One thing you may want to try is dumping the session array and seeing if everything checks out.

Also, one last thing: reproduce this error. You can't expect to give us a hundredth of what I'd assume to be a huge PHP script and let us diagnose the issue. Reproducing the error with ~10 lines of code (and nothing irrelevant in between) will allow us to see exactly what's going on and how to solve the issue.

1. I'm not sure what you're suggesting here. The heading redirect is where I (attempt to) establish $_SESSION["code"].

2. Well that's a really good question. By way of background, I've been maintaining this site for several years, but I did not originally create it. Here is the code for page.php:


<?php
// session_start should be the first line in the file
//session_start();

date_default_timezone_set( 'America/New_York');

// save the get variable (?here=filename) which defines the text content to show
$here = $_GET['here'];

// default to home page if this file has been invoked without a get variable
if ( ! isset( $_GET['here'] ) ) $here = 'index';
// you could instead force the error page in this case (although that would be illogical), like this:
// if ( ! isset( $_GET['here'] ) ) header( 'Location: 404.html' );

// show the error page if this file has been invoked with an invalid get variable
elseif ( ! is_file( 'pageContent/' . $_GET['here'] . '.txt' ) ) header( 'Location: 404.html' );
// you could instead force the home page in this case, like this:
// if ( ! is_file( 'pageContent/' . $_GET['here'] . '.txt' ) ) $here = 'index';

// we can load this now that we are sure that we have a valid get variable
require 'includes/errorCheck.inc.php';

require 'includes/page.class.php';
$page = new page;
$page -> here = $here;
$page -> showPage();

Bottom line: I lack the expertise to explain (or understand) how it functions.

3. I use session arrays in four places. All give the error message.

4. I either don't have the tools, or don't know how to do that.

I've provided the entire script, more or less. What "more or less" means is that $quotations = array( contains several hundred entries. For simplification purposes, I provided only a few. So the only material excised is additional entries in the $quotations = array(.

marain
01-04-2014, 01:53 PM
Traq,

I'll see if I can set up a test file to make those inquiries.

A.

marain
01-04-2014, 02:42 PM
I seem to be digging a bigger and bigger hole for myself: I made a test file, and eliminated all but a few entries in the quotations array. I then tried to incorporate Traq's diagnostics suggestion, i.e.
if ( isset( $_SESSION['code'] ) ){
print $_SESSION['code'];
}

else {
print "$_SESSION['code'] not found";
} and get parse error. I comment out that section and get numerous additional error messages.

Here is the test file (with diagnostics commented out), in its entirety:
<?php

$quotations = array(

"We, as criminal defense lawyers, are forced to deal with <a

href='http://en.wikipedia.org/wiki/Torture_memos' target='_blank'>some of the lowest people on

earth</a>. People who have no sense of right and wrong. People who will lie in court to get what

they want. People who do not care who gets hurt in the process. It is our job, our sworn duty as

criminal defense lawyers, to protect our clients from those people.",

"Cynthia Rosenberry",

"An advocate by the sacred duty which he owes his client knows, in the discharge of that office, but

one person in the world--that client and none other.",

"Henry Lord Brougham",

"It is wrong to expect a reward for your struggles. The reward is the act of struggle itself, not

what you win.&nbsp; Even though you can&#39;t expect to defeat the absurdity of the world, you must

make that attempt.&nbsp;That&#39;s morality.&nbsp; That&#39;s religion. That&#39;s art.&nbsp;

That&#39;s life.",

"Phil Ochs",


"For to him that is joined to all the living there is hope: for a living dog is better than a dead

lion.
<br />
<br />
For the living know that they shall die: but the dead know not anything, neither have they any more

a reward; for the memory of them is forgotten.
<br />
<br />
Also their love, and their hatred, and their envy, is not perished; neither have they any more a

portion for ever in any thing that is done under the sun.",

"Ecclesiastes 8:4-6",

);

/***********************************************************************

if ( isset( $_SESSION['code'] ) ){
print $_SESSION['code'];
}

else {
print "$_SESSION['code'] not found";
}

***********************************************************************/


if ( $_SESSION["code"] == "0"
|| $_SESSION["code"] == "1") {

$bigIndex = rand(0, (count($quotations) -1)) & 65534; // select entry, force even
$_SESSION["quoteIndex"] = $bigIndex;
}

else {
$bigIndex = $_SESSION["quoteIndex"];
}


echo "<br /><br /><div class='bigquote'> {$quotations [$bigIndex]} <br /><br /><div

align='right'>"; // display

$noSource = "&nbsp;"; // To test whether we need, or can skip, Part Two

if ($quotations [$bigIndex + 1] != $noSource) { // ...then we have an attribution to display for

Part Two,

echo $quotations [$bigIndex + 1] . "<br /><br />"; // so display it already
}

if ( $_SESSION["code"] == "0"
|| $_SESSION["code"] == "2") {

$universe = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n

o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 0"; // establish Part Three (partial) universe

$characters = explode(" ", $universe); // make array of it

shuffle ($characters); // go to Buffalo

$characters = array_slice($characters, 20, 3); // trim. The "20" is somewhat arbitrary

$decorations = array( // "Mommy, see all the pretty decorations."
"&spades;",
"<span style='color : #ff4040; '>&hearts;</span>",
"<span style='color : #ff4040; '>&diams;</span>",
"&int;",
"&omega;",
"&pi;",
"&sum;",
"&clubs;"
);

shuffle ($decorations); // to Buffalo, too, the decorations

$decorations = array_slice($decorations, 0, 3); // pare decorations

$decorations = array_merge($decorations, $characters); // meld tree onto decorations

shuffle ($decorations); // again to Buffalo we go, to complete Part Three

$_SESSION["decorations"] = $decorations;
}

else {
$decorations = $_SESSION["decorations"];
}


echo "Your Access Code is " . implode("&nbsp;", $decorations) . ".<br /><font size='-2'>Access code

broken? Click <a href='http://www.MarainLaw.com/test'>*** here ***</a> to replace it.</font><br

/><br /><br /></div></div>";?>

ALSO, to address the issue of whether the php.ini parameter was set correctly, I inserted session_start(); into the calling module, t0.pho. Here is the t0.php code:
<?php
session_start();
$_SESSION["code"] = "0";
header( 'Location: page.php?here=test' );
?>

The error messages now are much increased:

Notice: Undefined index: code in /var/www/html/pageContent/test.txt on line 46

Notice: Undefined index: code in /var/www/html/pageContent/test.txt on line 47

Notice: Undefined index: quoteIndex in /var/www/html/pageContent/test.txt on line 54

Notice: Undefined index: in /var/www/html/pageContent/test.txt on line 58






Cynthia Rosenberry


Notice: Undefined index: code in /var/www/html/pageContent/test.txt on line 67

Notice: Undefined index: code in /var/www/html/pageContent/test.txt on line 68

Notice: Undefined index: decorations in /var/www/html/pageContent/test.txt on line 101

Warning: implode() [function.implode]: Invalid arguments passed in /var/www/html/pageContent/test.txt on line 105
Your Access Code is .
Access code broken? Click *** here *** to replace it.

The test page is http://www.marainlaw.com/page.php?here=test .

traq
01-04-2014, 04:05 PM
All I can suggest is that a) your session indexes are not being set as you expect, or b) your sessions are not being carried over as you expect. Have you checked your cookies to see if you have the session cookie for this domain (unless you've changed it, the cookie name will probably be "PHPSESSID")?

After setting your session variables, try echo'ing them back to see if they were set successfully and that they contain the value(s) you expect.

On your quotations page, you can do print_r (http://php.net/print_r)( $_SESSION ) at the beginning of the file to see what data the session contains.

You can use session_id (http://php.net/session_id) to check if your sessions are, indeed, being started automatically (you will get a value if a session exists, or an empty string if not).

Nile
01-04-2014, 05:33 PM
The thing is we really can't help you until we know what the issue is, and it's rather hard to identify the issue when we only have a sliver of code. However, we're also not going to look through 3k+ lines of code just to decipher the issue. In this situation, there's really only a few options. You can either debug and try to find out what's not working with the sessions, or, again, reproduce the error in 10 lines or less, upload the code, and we can take a look to see what the issue is.

One thing that could be the issue is the session is expiring right after it's being set. To know if this is a case, create a test page and paste the following into it, then execute:

<?php
echo 'session_cache_expire(): '.session_cache_expire().'<br>';
echo 'session_cache_limiter(): '.session_cache_limiter();
Give us the output. This will let us look deeper into the php ini session settings and see if anything is messed up in there. An ideal output for this should be what you see on this example page (http://eg.jfein.net/session-demo/session-cache.php) I've set up.

On thing you haven't told us is whether or not sessions usually work on your server. To test that, run something like the following. Once the page loads, reload it again.

<?php
session_start(); //just for precaution

if( isset( $_SESSION[ 'visited' ] ))
{
echo "Sessions appear to be working!";
session_unset();
exit;
}

$_SESSION[ 'visited' ] = true;
echo "Session set attempted. Refresh.";

You can also see a sample execution here (http://eg.jfein.net/session-demo/session-test.php). As you refresh, the page should alternate between messages (the session unsets when it is detected, then you refresh and the if returns false, so it sets the session again... and so on).

Running both of these should give us a little bit of insight. However, nothing would be better then reproducing the error and sending us the minimal code. @traq also had a good idea to use session_id() and see if the session even has an ID. If nothing works, one more thing you can try is disabling cookies and give the second script above one more try. Sessions work without cookies, so if the session is appended to the URL properly, something is wrong when the session is being set within a cookie.

Also, when I said to dump the session information, I essentially meant to use a function like var_dump() or print_r() (as @traq requested above).

marain
01-04-2014, 05:43 PM
All I can suggest is that a) your session indexes are not being set as you expect, or b) your sessions are not being carried over as you expect. Have you checked your cookies to see if you have the session cookie for this domain (unless you've changed it, the cookie name will probably be "PHPSESSID")?

After setting your session variables, try echo'ing them back to see if they were set successfully and that they contain the value(s) you expect.

On your quotations page, you can do print_r (http://php.net/print_r)( $_SESSION ) at the beginning of the file to see what data the session contains.

You can use session_id (http://php.net/session_id) to check if your sessions are, indeed, being started automatically (you will get a value if a session exists, or an empty string if not).

Traq,

Your suspicion that either session indices are not being set, or that sessions are not being carried over, is entirely correct. That tends to beg the question, of course, as to "why?"

Checking, I found PHPSESSID=78e370fb4564290a1f8b88cdd9969004. The significance of that is beyond my pay grade ;-)

I sense that session_id (http://php.net/session_id) would tell me nothing since, to deal with that possibility, I inserted a session_start(). The only thing that that changes is that I get the additional (expected) message, "Notice: A session had already been started - ignoring session_start() in /var/www/html/pageContent/test.txt on line 2."

Nile
01-04-2014, 06:38 PM
Working backwards through your post here.

Checking, I found PHPSESSID=78e370fb4564290a1f8b88cdd9969004. The significance of that is beyond my pay grade ;-)

The significance of the ID is that there is indeed a session being initiated. If there were no session you'd likely get an empty string. As for using session_start(), you wouldn't need to put that at the top of your code if you're automatically starting sessions, so don't worry about that. You may want to do a session_regenerate_id() just to make sure nothing is caching/messing up anywhere.


Your suspicion that either session indices are not being set, or that sessions are not being carried over, is entirely correct. That tends to beg the question, of course, as to "why?"
Since it does seem that the session is working properly, the issue may be that the session isn't lasting long enough or isn't being properly set. See my above post and follow the instructions so we can further diagnose what's going on here.

marain
01-04-2014, 08:52 PM
Nile,

This is just an update. I have more work to do on your much appreciated suggestions. I missed your earlier post. Thank you for bring it to my attention. And thank you for your continued interest in this problem.

I use sessions on this server regularly. Additionally, the duration from setting the session index to trying to retrieve it on this site is typically just a second or so, so timing out I think we can rule out.

No, I would not expect anyone here to examine thousands of lines of code. So I do try to distill the problem. That is why I excerpted the page code. What I deleted were simply many many entries of the array. The PHP logic and syntax were preserved. It is a bit more than a sliver. Let me see how much more I can pare it down and still create the problem.

I'll be back with more info, though possibly not today.

A.

Nile
01-04-2014, 08:56 PM
Reproducing the code isn't cutting it down. It's making another scenario completely that I can use lightweight and test on my own. What you provided is untestable. With that though, I'll be awaiting your next reply with the information -- maybe that'll shed light on what's going on here.

Edit: After searching Google for setting sessions then redirecting, I'm finding other users that have had the same problems as you (I've tried to reproduce but it all works okay). Below I'll list a few solutions I've seen.


Use session_write_close() before or after the header redirect. I've see it both ways online, and I'm not sure which way is best/ideal, but try the following (uncomment and see which one works... if both works, choose your pick):


$_SESSION[ 'foo' ] = $bar;

// session_write_close();
header( 'Location: ...' );
// session_write_close();

I've seen people using exit(); and claim that putting it after the redirect fixes this issue:


$_SESSION[ 'foo' ] = $bar;

header( 'Location: ...' );
exit();

I've also seen someone try and set the PHPSESSID cookie in the header, but it looks like your PHPSESSID cookie is definitely setting. Anyways, give it a go:


$_SESSION[ 'foo' ] = $bar;

header( 'Set-Cookie: PHPSESSID=' . session_id() . '; path=/' );
header( 'Location: ...' );



If none of these work, like I previously mentioned, I did try and reproduce this issue on another example page here (http://eg.jfein.net/session-demo/reproduce/redirect.php), though it works like expected. You can see if you can reproduce the error on your server using the following code and telling me the output (session worked is what you want):


<?php
session_start();

if( isset( $_GET[ 'destination' ] ))
{
echo isset( $_SESSION[ 'test' ] ) ? "Session worked" : "Session failed";
session_unset();
exit();
}

$_SESSION[ 'test' ] = true;

header( 'Location: redirect.php?destination' );

Also, to give us some information about what PHP version you're using (if none of the above works), run phpversion() and report the output.

marain
01-04-2014, 09:18 PM
Nile,

Thank you for your continuing efforts.


<?php
session_start(); //just for precaution

if(isset( $_SESSION['visited'] ))
{
echo "Sessions appear to be working!";
session_unset();
exit;
}

$_SESSION['visited'] = true;
echo "Session set attempted. Refresh.";
?> works, just as you describe.

The output from
<?php
echo 'session_cache_expire(): '.session_cache_expire().'<br>';
echo 'session_cache_limiter(): '.session_cache_limiter();
?> is

session_cache_expire(): 180
session_cache_limiter(): nocache

It will take me some time to try to distill the code to the ten or so lines that you seek.

Nile
01-04-2014, 09:22 PM
Interesting. So now we know that 1) the session is being set and retrieved properly, 2) your cache is configured properly, and 3) the error must have to do with something specifically in the code, and there isn't a problem with your server/php config.

Before you try and reproduce the error yourself, I've already tried. While you were posting your new post, I went ahead and modified my previous post (#12, link here (http://www.dynamicdrive.com/forums/showthread.php?76193-Stored-Sessions-Data-Not-Found&p=304501#post304501)). Take a look at that one and run through the options I provide. If anything works, it'll save you from reproducing the error. Also - remember that reproducing isn't distilling. Distilling takes forever. Reproducing should take around 10 minutes. You aren't removing anything, you're starting over using a clean slate, and making the error occur again to make sure you weren't crazy the first time. In this process, you only are focused on what creates the error, and not all the in-betweens.

traq
01-05-2014, 12:31 AM
Your suspicion that either session indices are not being set, or that sessions are not being carried over, is entirely correct. That tends to beg the question, of course, as to "why?"
Have you tried using print_r( $_SESSION ); at the top of your quotations page, to see what values are being carried over?



After searching Google for setting sessions then redirecting, I'm finding other users that have had the same problems as you (I've tried to reproduce but it all works okay). Below I'll list a few solutions I've seen.

Use session_write_close() before or after the header redirect …
I've seen people using exit(); and claim that putting it after the redirect fixes this issue …
I've also seen someone try and set the PHPSESSID cookie in the header …
Try session_write_close before the redirect. I've **never** run into this problem (I haven't even seen mention of it more recently than 6 years ago), but if you have an odd configuration or a slow hard drive I suppose it might be a problem.

And using exit after redirecting is good practice anyway.

As for setting a new session cookie manually, I'd avoid trying that unless nothing else works (too much opportunity for things to get mixed up even more).

Nile
01-05-2014, 12:38 AM
I've **never** run into this problem (I haven't even seen mention of it more recently than 6 years ago), but if you have an odd configuration or a slow hard drive I suppose it might be a problem.
I've never had this issue either, but searching Stackoverflow seems to bring up many (sadly recent) results. Here is a nice checklist (http://stackoverflow.com/questions/17242346/php-session-lost-after-redirect) to follow for the OP that I just found while searching.


As for setting a new session cookie manually, I'd avoid trying that unless nothing else works (too much opportunity for things to get mixed up even more).
I agree. I would be worried about users who aren't using cookies at all (i.e., have them disabled). If sessions are only working when cookies are enabled because you have to set the session manually it could get rather confusing.

marain
01-05-2014, 08:03 PM
Nile,

None of the suggested additions solve the problem. I've not yet had a chance to create the "reproduce" file. I use PHP version 5.2.9.

A.

Nile
01-05-2014, 08:18 PM
Did you try my own reproduction of the issue (pasted below):


<?php
session_start();

if( isset( $_GET[ 'destination' ] ))
{
echo isset( $_SESSION[ 'test' ] ) ? "Session worked" : "Session failed";
session_unset();
exit();
}

$_SESSION[ 'test' ] = true;

header( 'Location: redirect.php?destination' );


If it says session failed then we've potentially reproduced this issue.

marain
01-05-2014, 08:23 PM
Nile,

I'll respond to your post #18 shortly.

I was not familiar with stackoverflow. Just went there, saw the relevant dialogue. One of the items on the checklist is "8.Make sure your file extension is .php (it happens!)". My site sets defines the session index in a file with a .php extension, but it redirects to a .txt extension! My domain where sessions works uneventfully involves redirects to files with .php extensions. That strikes me as something to try. I think that setting that up, for me, won't be easy, on account of the present file navigation scheme.

A.

marain
01-05-2014, 08:33 PM
Did you try my own reproduction of the issue (pasted below):


<?php
session_start();

if( isset( $_GET[ 'destination' ] ))
{
echo isset( $_SESSION[ 'test' ] ) ? "Session worked" : "Session failed";
session_unset();
exit();
}

$_SESSION[ 'test' ] = true;

header( 'Location: redirect.php?destination' );


If it says session failed then we've potentially reproduced this issue.

I attempted to try it, got 404-type error.

Nile
01-05-2014, 08:34 PM
One of the items on the checklist is "8.Make sure your file extension is .php (it happens!)". My site sets defines the session index in a file with a .php extension, but it redirects to a .txt extension!

The takeaway from that is to make sure that the file is actually being executed as PHP. As long as you're including it it and/or executing it as a PHP file, everything should work out fine. However, if you do want to test and make sure this isn't the issue, here's another case you can run:

index.php


<?php
session_start();

if( isset( $_GET[ 'session-set' ] ))
{
include( 'detect.txt' );
session_unset();
exit;
}

$_SESSION[ 'foo' ] = true;
header( 'Location: index.php?session-set' );


detect.txt


<?php

echo isset( $_SESSION[ 'foo' ] ) ? "Session worked." : "Session failed.";


If this returns session failed, the text file is not necessarily the issue and we'd have to do further debugging to verify.


I attempted to try it, got 404-type error.

Sorry, make sure that the PHP file is called "redirect.php", otherwise the script redirects to another page. (Or just modify the script, whatever you feel is easier).

traq
01-05-2014, 08:53 PM
I use PHP version 5.2.9
I highly recommend upgrading. 5.2 reached its end-of-life in 2011 and is no longer supported (receives no security patches, either).


One of the items on the checklist is "8.Make sure your file extension is .php (it happens!)". My site sets defines the session index in a file with a .php extension, but it redirects to a .txt extension! My domain where sessions works uneventfully involves redirects to files with .php extensions. That strikes me as something to try. I think that setting that up, for me, won't be easy, on account of the present file navigation scheme.
Notice: Undefined index: code in /var/www/html/pageContent/quotations.txt on line 2956
Because you are getting PHP errors from quotations.txt, we know that the file is indeed being executed by PHP. If your session start automatically, they will be started in any file PHP runs, regardless of the file extension.

Did you check your session contents on the quotations.php page?
Have you tried using print_r( $_SESSION ); at the top of your quotations page, to see what values are being carried over?

marain
01-05-2014, 09:39 PM
Traq,

Thanks for the heads up re PHP 5.2. If my host supports a more recent version (I suspect it does), I'll update.

print_r( $_SESSION ); yields Array ()

Niles,

I made the calling script (not the index):
<?php
session_start();

if( isset( $_GET[ 'test' ] ))
{
echo isset( $_SESSION[ 'test' ] ) ? "Session worked" : "Session failed";
session_unset();
exit();
}

$_SESSION[ 'test' ] = true;

header( 'Location: page.php?here=test' ); and again got the 404. I suspect I'm doing it incorrectly. You may have to spoon feed that to me if I'm preparing it incorrectly.

Both:

You've put huge amounts of time into this, this weekend, which I greatly appreciate. I'm inclined to look at it more, but this is, for me, a hobby and, unfortunately, real life beckons. I will return to this, but it may be a few days. I am very apologetic that your valued suggestions have not enabled me to provide you the satisfaction of saying "Problem solved." I also suspect strongly that the problem may arise from the particular way my site was set up. That makes the problem very difficult to reproduce.

I'll check for response to this thread, but will be likely unable to implement any further suggestions quickly.

A.

Nile
01-05-2014, 09:42 PM
Make sure that the location in the header redirect ("page.php") matches the files name it's in. Or just change that line entirely to:


header( 'Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . '?test' );

traq
01-06-2014, 01:47 AM
print_r( $_SESSION ); yields Array ()
Meaning, the session values are not being carried between pages. What you have is a new, empty session.



header( 'Location: page.php?here=test' ); and again got the 404.
Do you have a page named "page.php"?

Also, note that Location headers require a fully qualified URL, not only a path. You need to include the scheme and domain name as well:
Location: http://example.com/page.php?here=test
Many times, you get the same results when you omit them, but it won't work in all browsers under all circumstances.

(correct me if I'm wrong,) Looking back through this thread, I don't see that you have checked to make sure your browser is accepting the session cookie or not (for example, in Chrome, click on the menu icon -> Settings -> show advanced settings -> [Content Settings] -> [All cookies and site data])?

Speaking of, is PHP configured to use cookies? I don't know why it wouldn't be (and it should not be), but if it is, then the session id might not be being transferred across pages.

Further, you can test if the sessions on each page are the same session or not by using print session_id(); on both pages (if they are different, then it's not the same session).

marain
01-07-2014, 12:47 AM
Folks,

I'm going to try a new approach. Answering Traq, I DO have a page called page.php. I was going to upload that page for everyone's review. My new approach, however, will be to bypass that page completely for the one page on this domain that tries to use sessions.

Traq, I followed your suggestion. I'm now using PHP 5.3.8.

A.

traq
01-07-2014, 05:20 AM
good for you :)

(just in case (http://us1.php.net/migration53))