Jump to content

Problem With $_SESSION Values


Werezwolf

Recommended Posts

I am trying to create a history of requested URI's (upto 3) for two purposes Login page and Error Logging. My problem is the Session is written and the values are set yet i can not retrieve the values in it upon a refresh. The use of an Array is to manage the quantity of max values (not written in yet).

    <?php
    session_start();
    //printing $_SESSION['REQUEST_URI'] here will result in nothing when it should contain something.
    $REQUEST_URI = time().','.$_SERVER['REQUEST_URI'];
    if(is_array($_SESSION['REQUEST_URI'])){array_push($_SESSION['REQUEST_URI'],array($REQUEST_URI));}
    else{$_SESSION['REQUEST_URI'] = array($REQUEST_URI);Print('never set? ');}//if dosent exist create the array
    if(isset($_SESSION['REQUEST_URI'])){print_r($_SESSION['REQUEST_URI']);}
    ?>

Session contains

    REQUEST_URI|a:1:{i:0;s:19:"1422925783,/~Debug/";}

After a refresh i expect

    REQUEST_URI|a:2:{i:0;s:19:"1422925783,/~Debug/";i:1;s:28:"1422925784,/~Debug/index.php";}

Yet it only contains
 

    REQUEST_URI|a:1:{i:0;s:28:"1422925784,/~Debug/index.php";}
Link to comment
https://forums.phpfreaks.com/topic/294317-problem-with-_session-values/
Share on other sites

Your script mostly worked for me. I changed two things:

if (is_array($_SESSION['REQUEST_URI'])) {
This gave an "Undefined index" notice on the first go. So I changed to:
if (!empty ($_SESSION['REQUEST_URI']) && is_array($_SESSION['REQUEST_URI'])) {
And:
array_push($_SESSION['REQUEST_URI'], array($REQUEST_URI));
This was creating (what I think is) undesired nesting, so I changed to:
array_push($_SESSION['REQUEST_URI'], $REQUEST_URI);
I get this result:
Array ( [0] => 1422932734,/index.php [1] => 1422932737,/page.php [2] => 1422932741,/index.php )
Note that it doesn't stop at 3, as you have no logic for that. It will just keep racking up.

 

Did you make sure to put session_start() at the top of all your pages?

Thanks for the Quick Reply

 

I threw it into everysingle page including any Require or Include pages aswell i still endup with the same i can write to session but it never retrives the data.

 

I thought it might be a permission issue but if i can write to it i should be able to read from it right?

i5naf6udl3gl4dotnjatqm23b0

i5naf6udl3gl4dotnjatqm23b0

never set?

Array ( [0] => 1422934817,/~Debug/index.php )

 

i refreshed it 5 times and switched between /~debug/ and /~debug/index.php

i even deleted all sessions and started a new one

results in the same

 

No new sessions are created either.

 

I went one step futher and added

 

session_start();
echo session_id().'
';
print_r($_SESSION);

 

it printed

 

i5naf6udl3gl4dotnjatqm23b0

i5naf6udl3gl4dotnjatqm23b0

Array ( [HTTPS] => 1 [DateModified] => Tue 03 Feb 2015 02:02:32 [REQUEST_URI] => Array ( [0] => 1422935072,/~Debug/index.php ) )

never set?

Array ( [0] => 1422935073,/~Debug/index.php )

Do this simple test for me please. Create two files: page1.php and page2.php, with the following:

page1.php

<?php

session_start();
echo '<pre>';
echo '------- PAGE1 OUTPUT -------<br />';
echo 'session_id: ' . session_id() . '<br />';
$_SESSION['foo'] = 'bar';
echo 'session data:<br />';
var_export($_SESSION);
echo '<br />----------------------------<br />';
echo '</pre>';
page2.php

<?php

session_start();
echo '<pre>';
echo '------- PAGE2 OUTPUT -------<br />';
echo 'session_id: ' . session_id() . '<br />';
echo 'session data:<br />';
var_export($_SESSION);
echo '<br />----------------------------<br />';
echo '</pre>';
Put these files in the same directory. View page1.php, and then view page2.php. Copy/paste the output from each page in your reply.

pre------- PAGE1 OUTPUT -------br session_id i5naf6udl3gl4dotnjatqm23b0br session databr array ( 'HTTPS' => 1, 'DateModified' => 'Tue 03 Feb 2015 02:02:43', 'REQUEST_URI' => array ( 0 => '1422935923,/~Debug/', ), 'foo' => 'bar', )br ----------------------------br pre

 

------- PAGE2 OUTPUT -------
session_id: i5naf6udl3gl4dotnjatqm23b0
session data:
array (
  'HTTPS' => 1,
  'DateModified' => 'Tue 03 Feb 2015 02:02:43',
  'REQUEST_URI' =>
  array (
    0 => '1422935923,/~Debug/',
  ),
  'foo' => 'bar',
)
----------------------------

 

I do have all this in a function but http://php.net/manual/en/language.variables.superglobals.php assures me that $_SESSION is a superglobal yet its not behaving thatway as i do have it in a function

Okay, so we have just confirmed that your sessions are working properly between pages. This is good.

 

That's a bit strange then however, as like I said your script works perfectly fine for me.

 

Let's try making the code a little smaller with less moving parts. Give this a whirl, and let's see what happens:

<?php

session_start();

if (!isset($_SESSION['REQUEST_URI'])) {
    $_SESSION['REQUEST_URI'] = array();
}

$_SESSION['REQUEST_URI'][] = time() . ',' . $_SERVER['REQUEST_URI'];

var_export($_SESSION['REQUEST_URI']);
Also, as stated in the manual $_SESSION is a superglobal and is not affected by scope. It does not matter if it is in a function or not.

array ( 0 => '1422937575,/~Debug/index2.php', 1 => '1422937581,/~Debug/index2.php', 2 => '1422937582,/~Debug/index2.php', )

 

im watching my Log file aswell

 

[03-Feb-2015 15:26:15 Australia/Melbourne] PHP Notice:  A session had already been started - ignoring session_start() in C:\inetpub\wwwroot\~debug\index2.php on line 3

[03-Feb-2015 15:26:21 Australia/Melbourne] PHP Notice:  A session had already been started - ignoring session_start() in C:\inetpub\wwwroot\~debug\index2.php on line 3

[03-Feb-2015 15:26:22 Australia/Melbourne] PHP Notice:  A session had already been started - ignoring session_start() in C:\inetpub\wwwroot\~debug\index2.php on line 3
 It works atleast

Nither do i. the problem seems to be with the file itself? i just copyed my code section by section into another file and referenced that and it works now.

 

This is ofcoruse a bigger project ive posted somthing in Misc about Active Directory Authentication in Misc

I do have all this in a function

 

 

you would need to post your actual code that produces/reproduces the problem. i suspect you have a line like - global $_SESSION; in your function, that actual breaks the connection with the actual super-global $_SESSION variable.

 

 

I threw it into everysingle page including any Require or Include pages

 

 

require/include files are not pages. web pages are only the main files that get requested via their url.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.