把会话放在echo里面

时间:2015-01-27 06:39:59

标签: javascript php session

 if (isset($_SESSION['LAST_ACTIVITY1']) && (time() - $_SESSION['LAST_ACTIVITY1'] > 5)) {


         echo'
    <script type="text/javascript">

    var marks = <?php echo session_unset(); ?>
    var answer = confirm ("Your Session will expired.Click yes to continue or cancel to logout")
    if (answer){
    alert ("Woo Hoo! So am I.")

    }else{
    alert ("Darn. Well, keep trying then.")

// code need to put here is php

    }
    </script>
    ';
    }else{
    }

以上是我当前的代码..现在我想在echo中插入会话..如何做到这一点..如何将php代码放入php回声中的javascript中..

下面是其他内容的代码:

if (isset($_SESSION['member_id'])) {
      session_unset();     // unset $_SESSION variable for the run-time
        session_destroy();   // destroy session data in storage


      header('Location: '.AT_BASE_HREF.'login.php?loginexpired=true');
       exit;
  }

1 个答案:

答案 0 :(得分:1)

当您使用echo时,需要使用字符串连接,而不是<?php来执行PHP代码。所以它应该是:

echo '<script type="text/javascript">

var marks = ' . session_unset() . ';
var answer = confirm ("Your Session will expired.Click yes to continue or cancel to logout");
    if (answer){
        alert ("Woo Hoo! So am I.");

    }else{
        alert ("Darn. Well, keep trying then.")
' . $some_php_var . '
}
</script>';

或者不是使用echo,而是可以退出PHP解释:

if (isset($_SESSION['LAST_ACTIVITY1']) && (time() - $_SESSION['LAST_ACTIVITY1'] > 5)) {
    ?>
    <script type="text/javascript">

    var marks = <?php echo session_unset(); ?>;
    var answer = confirm ("Your Session will expired.Click yes to continue or cancel to logout");
    if (answer){
        alert ("Woo Hoo! So am I.");

    }else{
        alert ("Darn. Well, keep trying then.");

    <?php // code need to put here is php
    ?>

    }
    </script>
<?php 
} else{
}

但是你还有其他问题。 session_unset()没有返回任何内容,因此将其分配给某些内容毫无意义。以上将导致声明:

var marks = ;

会导致Javascript语法错误。我不确定你在那里做什么,所以我不知道该怎么做才能改变它。