I am using a session parameter to count the number of searched parameters (as I will be using them later).
I want to set this parameter to 0 when there is no search,

so I used this:

(this is done in an included file that runs before anything else and is responsible for sessions, initializing and such)

if(!$_GET["s"])

{

$_SESSION["search_parameters"] = 0;

}

I encountered a problem that happens in IE but doesn’t happen in Opera.

When I refresh the search-results page, for some reason the $_SESSION["search_parameters"] was set to 0,

although it shouldn’t have.

Trying to find the problem I changed the code a bit

if(!$_GET["s"])

{

$_SESSION["search_parameters"] = 0;

?>

<script language="JavaScript" type="text/javascript">

alert('something');

</script>

<?

}

There where 2 interesting results to this test

1) there was no alert where there shouldn’t be an alert, so I still don’t know what set the session var to 0.

2) At other pages I got 2 alerts (!) instead of the expected 1, and it happened in IE only!

Anyone has any idea?

    Hi

    so basically every time that "s" doesn't appear in the url, you want to reset the $_SESSION["search_parameters"] = 0; ?

    I've had problems where the javascript alert doesn't show because of the point its executed in the html, you might want to change that to

     print 'something'; 

    just to ensure its not a java error.

    It almost sounds like you are recreating the session each time the page loads, try adding a var_dump($_session) before and after the offending code that sets the value to 0

    Hope this helps

    Regards
    Tim

      I have printed the content of the $_SESSION at the top of the page, and the field was set to 0 by then

      way before this bit of code.

      I have two points to add.

      When I remove the line "<meta http-equiv="content-type" content="text/html; charset=UTF-8">"

      there was only one alert, and not two, and to be more accurate, it was enough to remove the "chareset=UTF-8" part,

      any possible explanation?

      I created a page which doesn’t do anything but print the $_SESSION

      and used the "open in new window" to open it, so now I have the troublesome page and the test page both opened

      and both with the same session

      and here is the weird thing, in the subject page I print the $_SESSION at the top and at the bottom of the page,

      both are ok

      when I refresh the test-page, the search-parameters field is set to 0

      where I now refresh the subject page the search-parameter is set to 0 as well

      when could it be set to 0 if the last action was displaying a correct session?

        Hey,

        Dont know why you'd get an error with that character set, but try this one.

        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

        If session value is being set before that bit of code, could it be to do with the point thats incrementing ur session value?

        Whatever is setting it to 0 is also in ur test page, right? are you registering the session variable? how are you storing your session_id? Cookie?

        I'd alter ur test page to try and print the session before the function that creates it and at every point thereafter - so you can see at what point in the code the session is changed. It will probably give you an error if no session has been created but it shouldn't stop the whole script from running (with a bit of luck) 🙂

         if (session_id() == '')
        {
        	session_start();
        }

        might help to prevents sessions from being recreated

        Reagrds

        Tim

          I got the alert twice with "<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">"

          as well.

          Here's he test page code:

          static $session_loaded = false;

          if(!$session_loaded)

          {

                  session_cache_limiter('private, must-revalidate');
          
                  session_name("WebsiteID");
          
                  session_start();
          
                  $session_loaded = true;

          }

          print_r($_SESSION);

          as you can see, nothing happens before the printing of the $_SESSION.

          I added the

          session_cache_limiter('private, must-revalidate');

          due to something I've read at php.net.

          it didn’t change a thing

            Hey

            I think I see the problem

            Its in your use of STATIC

            From what I've just read, the variable will remain valid even when you leave the function that you declared it in. However what you are actually doing when you refresh that page is redeclaring and reinitialising the value in that variable.

            Try creating a test.php file with the following

            static $session_loaded = false;
            
            if(!$session_loaded)
            {	
            	print 'test';
            	$session_loaded = true;	
            }
            

            Every time you hit refresh it still prints "test", meaning the variable isn't holding its new value

            try

            if (session_id() == '')
            {
            session_start();
            print 'starting session';
            }
            else
            {
            print 'Session started - ' . session_id();
            }
            

            and see if that makes a difference (you may need to tinker with that) or try

            ini_set('session.auto_start',1);
            

            This turns starts a new session automatically.

            Regards

            Tim

              You were right about the static variable (bad post at php.net I suppose)

              Though your code didn’t work as expected as well

              if (session_id() == '')

              {

                      session_start(); 
              
                      print 'starting session<br>'; 

              }

              else

              {

                      print 'Session started - ' . session_id() . '<br>'; 

              }

              print session_id();

              ALWAYS printed:

              starting session
              e591e1f69bcbf58ac996a93f89688f8a

              probably because you don’t have a session_id before you do session_start..

              either way, I don’t think that’s the problem, since restarting the session more than once did produce the same session_id

              (hence the same session)

              and indeed all the other session variables remained the same as if it’s the same session,

              besides the variables in question.

                There's a know issue with caching related to IE6.
                Maybe this helps (first line on page):

                <?php
                session_start();
                header ("Cache-control: private"); / IE6 fix /
                ?>

                cheers,

                Hans

                  it is not the caching prooblem....

                  tested with header ("Cache-control: private"); / IE6 fix /

                    Write a Reply...