-1

I have a single page application based on an index.php page where I start the session and display first contents. Then I load via Ajax contents in different divs based on the links that are clicked.

If I want to access the $_SESSION array in the loaded page (eg. to display user's name) I need to put session_start also in the page that I load via Ajax. Everything works fine this way but when I see the log I see that php is throwing an error each time I load one of the pages via Ajax saying that

"session already started. Ignoring session_start()".

So: on one side I need to put session_start to access the session array but on the other the session_start command is ignored.

From index.php:

<?php
require '../session_handler.inc.php';
require 'global_functions.php';
session_start();
if(isset($_SESSION['id1'])){
    $actusr=$_SESSION['id1'];
}

A sample ajax call:

function loadContent(sourceUrl){
    $(".container").load(sourceUrl);
}

The php I need on top of the called page:

<?php 
require '../../session_handler.inc.php';
require '../global_functions.php';
session_start();

If I remove the session_start() the $_SESSION will be unavailable

How can I fix this situation? Can I access the session array from the loaded page without starting session?

Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74
  • Try removing it from the ajax as it's part of the script you're running it under. – Funk Forty Niner Apr 22 '16 at 15:18
  • @Fred-ii- then I no longer will load $_Session array – Lelio Faieta Apr 22 '16 at 15:18
  • 2
    *Hm...*, bizarre. Can you share your code? Can't see how that would break like that. You don't have 2 `session_start();`'s anywhere in the same file(s) do you? Or something else is calling it twice? like an include/require etc. a function somewhere. – Funk Forty Niner Apr 22 '16 at 15:21
  • @Fred-ii- no for sure! Will add some sample code – Lelio Faieta Apr 22 '16 at 15:22
  • It's definitely not because you call `session_start` in the first page load and then in the AJAX handlers. Those are independent requests. Most likely you are calling `session_start` twice in your AJAX handlers. – Sergiu Paraschiv Apr 22 '16 at 15:23
  • @SergiuParaschiv I have already checked it. Also checked the log live while navigating and the issue happen when I load the page. No double session_start() on the same page for sure – Lelio Faieta Apr 22 '16 at 15:27
  • 1
    Does `session_handler.inc.php` and/or `global_functions.php` contain `session_start();` or any other file calling it twice? @LelioFaieta check your console also. – Funk Forty Niner Apr 22 '16 at 15:28
  • It's most likely in something you `include` in that page where you `session_start`. – Sergiu Paraschiv Apr 22 '16 at 15:28
  • session_handler is an empty file actually and global_functions has no session_start command – Lelio Faieta Apr 22 '16 at 15:29
  • someone popped an answer below, no idea if that'll work for you @LelioFaieta – Funk Forty Niner Apr 22 '16 at 15:30
  • This doesn't really address the question, but why are you including an empty file? – Don't Panic Apr 22 '16 at 15:30
  • It certainly seems like a file called `session_handler` might start the session. – Don't Panic Apr 22 '16 at 15:31
  • @LelioFaieta which he/she might have pulled from http://stackoverflow.com/a/18542272/ and the Q&A http://stackoverflow.com/questions/6249707/check-if-php-session-has-already-started See also the manual http://php.net/manual/en/function.session-status.php – Funk Forty Niner Apr 22 '16 at 15:32
  • @Don'tPanic it's a file to manage sessions via db. Since now I have a dedicated and not shared server I am testing sessions without db. Before removing the file I just commented it all to see if it works – Lelio Faieta Apr 22 '16 at 19:42
  • @Fred-ii- if I check for session to be started on the loading page I get a not started feedback anyway i check it. Also if I don't start the session again the $_session array is not available – Lelio Faieta Apr 22 '16 at 19:45

3 Answers3

0

It's better to use this line instead of session_start(). you might have more than one session_start and using session between those two.

if(session_id() == '') {
    session_start();
}
Mojtaba
  • 4,852
  • 5
  • 21
  • 38
0

If you are testing on a local WAMP or Xamp environment, I would recommend you clear your cache, close your browser (Not a single tab) and run your app.

Try this 1:

 <?php
        if(!isset($_SESSION)) 
        { 
            session_start(); 
        } 
    ?>

Or

2: If you have php>=5.4.0, better use this:

$status = session_status();
if($status == PHP_SESSION_NONE){
    //There is no active session
    session_start();

}else
if($status == PHP_SESSION_ACTIVE){
    //Destroy current and start new one
    session_destroy();
    session_start();
}

Hope this helps..

Amar Pratap
  • 1,000
  • 7
  • 20
  • I run a webserver for this app. This solution will not solve the issue. I need to pass the session array to the new page in any other way than session _start – Lelio Faieta Apr 22 '16 at 19:40
0

move session_start(); to top of your page

<?php
session_start();
require '../session_handler.inc.php';
require 'global_functions.php';
if(isset($_SESSION['id1'])){
    $actusr=$_SESSION['id1'];
}

session_handler.inc.php or global_functions.php may already contain session_start() so move the session_start() of index.php to the top. also note that this should be the first statement in index.php

or insert the code below at page top of index.php

<?php
session_start();
?>

and remove the other session_start() in index.php

  • Index.php needs session start to check if user has logged in so I can't remove it – Lelio Faieta Apr 22 '16 at 19:37
  • 1
    @LelioFaieta dont remove it just move it to the top of the page just after –  Apr 22 '16 at 20:11
  • You know that this doesn't change anything? It's not a "headers already sent" issue. The code sequence doesn't affect the result at all (tested) – Lelio Faieta Apr 22 '16 at 20:13
  • 1
    @LelioFaieta please try doing the same in the page that you are loading in the div and the index.php page. i faced this similar problem a while back and doing this solved mine. –  Apr 22 '16 at 20:34
  • It doesn't in my case. – Lelio Faieta Apr 22 '16 at 20:35