Werezwolf Posted February 3, 2015 Share Posted February 3, 2015 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";} Quote Link to comment https://forums.phpfreaks.com/topic/294317-problem-with-_session-values/ Share on other sites More sharing options...
scootstah Posted February 3, 2015 Share Posted February 3, 2015 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? Quote Link to comment https://forums.phpfreaks.com/topic/294317-problem-with-_session-values/#findComment-1504639 Share on other sites More sharing options...
Werezwolf Posted February 3, 2015 Author Share Posted February 3, 2015 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? Quote Link to comment https://forums.phpfreaks.com/topic/294317-problem-with-_session-values/#findComment-1504644 Share on other sites More sharing options...
scootstah Posted February 3, 2015 Share Posted February 3, 2015 (edited) Add this right underneath session_start(): echo session_id();Does this string change on every page load, or stay the same? If it changes, you might have the session cookie domain and/or path incorrectly configured. Edited February 3, 2015 by scootstah Quote Link to comment https://forums.phpfreaks.com/topic/294317-problem-with-_session-values/#findComment-1504647 Share on other sites More sharing options...
Werezwolf Posted February 3, 2015 Author Share Posted February 3, 2015 (edited) 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 ) Edited February 3, 2015 by Werezwolf Quote Link to comment https://forums.phpfreaks.com/topic/294317-problem-with-_session-values/#findComment-1504650 Share on other sites More sharing options...
scootstah Posted February 3, 2015 Share Posted February 3, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/294317-problem-with-_session-values/#findComment-1504651 Share on other sites More sharing options...
Werezwolf Posted February 3, 2015 Author Share Posted February 3, 2015 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: i5naf6udl3gl4dotnjatqm23b0session 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 Quote Link to comment https://forums.phpfreaks.com/topic/294317-problem-with-_session-values/#findComment-1504652 Share on other sites More sharing options...
scootstah Posted February 3, 2015 Share Posted February 3, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/294317-problem-with-_session-values/#findComment-1504654 Share on other sites More sharing options...
Werezwolf Posted February 3, 2015 Author Share Posted February 3, 2015 (edited) 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 Edited February 3, 2015 by Werezwolf Quote Link to comment https://forums.phpfreaks.com/topic/294317-problem-with-_session-values/#findComment-1504655 Share on other sites More sharing options...
Solution scootstah Posted February 3, 2015 Solution Share Posted February 3, 2015 You can get rid of that Notice with: if (!session_id()) { session_start(); }So it works now? Awesome! I'm not really sure what the problem was, without experiencing it myself and being able to step through it. Quote Link to comment https://forums.phpfreaks.com/topic/294317-problem-with-_session-values/#findComment-1504656 Share on other sites More sharing options...
Werezwolf Posted February 3, 2015 Author Share Posted February 3, 2015 (edited) 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 Edited February 3, 2015 by Werezwolf Quote Link to comment https://forums.phpfreaks.com/topic/294317-problem-with-_session-values/#findComment-1504657 Share on other sites More sharing options...
mac_gyver Posted February 3, 2015 Share Posted February 3, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/294317-problem-with-_session-values/#findComment-1504682 Share on other sites More sharing options...
Werezwolf Posted February 5, 2015 Author Share Posted February 5, 2015 Ran through the old code once more and checked it. There was an old script that was meant to be removed but it essentially set $_SESSION = ''; Now i feel stooped for not commenting it out, As many people state "We learn from our mistakes". Quote Link to comment https://forums.phpfreaks.com/topic/294317-problem-with-_session-values/#findComment-1504928 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.