关于PHP Session

I have registered a session in index.php and print it.

$_SESSION['login'] = 'ok'; //print 'ok'

then in page1.php got this session and change it.

<?$_SESSION['login'] = 'no';?>
setTimeout('window.location="index.php"', 100);

Now in index.php there is still print 'ok'. How can?

On each you script, you need to include the following line of code:

<?php session_start(); ?>

If you do not do this, PHP will not persist $_SESSION vars and it will default to whatever was originally set.

To read more about PHP $_SESSION variables, take a look here: PHP: session_start()

from your comment, you had session_start() on both right? but lets take this step by step:

  1. you go to index.php and set your session variable to 'ok'
  2. you go to page1.php and set your session to 'no'
    after this, you redirect your page back to index.php
  3. you print out the value of session variable in index.php, and returns 'ok' even after setting the value in page1.php to 'no', right? but you forgot that you changed the value of the session back to 'ok' when you were redirected to index.php

since you dont have any condition whatsoever in index.php, even if you set it on some other page, it will always print 'ok'