如何使用$ _SESSION或js计时器自动注销?

时间:2011-09-21 08:00:42

标签: php javascript symfony1

我读了这个主题,但不知道从哪里开始 第一步是什么?我有这个首先被调用的代码:rclayout.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <?php include_http_metas() ?>
    <?php include_metas() ?>
    <?php include_title() ?>
    <link rel="shortcut icon" href="/favicon.ico" />
    <?php use_stylesheet('rainbow.css');  ?>
    <?php use_javascript('rainbow.js'); ?>
    <?php include_stylesheets(); ?> 
    <?php include_javascripts(); ?>
</head>
<body onload='ax_startup();'>   
<center>
    <?php
       echo "<div id='div_main_container_rc'>"; 
    ?>
<div id='div_header_container_rc'>
   <?php include_component('profile','header'); ?>
</div>
    <?php       
            echo "<div id='div_content_container_rc'>";
            echo $sf_content;
            echo "</div>";
            echo "<div id='div_footer'>";
    ?>
   //show a footer menu here
</div>  
</div> 
</center>
 </body>
 </html>

然后_header.php检查用户是否登录:

<?php
$USR_IS_ADMIN = false;
$USR_AUTH     = false;

if($sf_user->hasAttribute('ADMIN'))
{
    $USR_IS_ADMIN = true;
}
    $id = $sf_user->getAttribute('profile_id');

    if($sf_user->hasAttribute('profile_id') > 0)
{   
      $profile = RcProfileTablePeer::getById($id);
      $activated = $profile->getActivated();
       if($activated == 1)
       {
        //echo "activated".$activated;
        $USR_AUTH = true;
       }
       else
       {
        //echo "NOT activated".$activated;
    $USR_AUTH = false;
        }
}
   ?>
   <?php if(!$USR_AUTH) : ?>
       //show a specific menu here   
   <?php endif;?>

  <?php if($USR_AUTH):?>
      //show a different menu here pertaining to logged in user
  <?php endif;?>

我的UPDATED factories.yml文件:

prod:
  logger:
  class:   sfNoLogger
  param:
    level:   err
    loggers: ~

test:
  storage:
  class: sfSessionTestStorage
  param:
    session_path: %SF_TEST_CACHE_DIR%/sessions

response: 
  class: sfWebResponse
  param:
    send_http_headers: false

mailer:
  param:
    delivery_strategy: none

dev:
  mailer:
  param:
    delivery_strategy: none

all:
  routing:
  class: sfPatternRouting
  param:
    generate_shortest_url:            true
    extra_parameters_as_query_string: true

  view_cache_manager:
    class: sfViewCacheManager
    param:
      cache_key_use_vary_headers: true
      cache_key_use_host_name:    true

user:
  param:
     timeout: 300

我必须从哪里开始如何做到这一点?我没有在任何地方看到会话 我配置php.ini文件,如果是这样如何?或者我是否通过会话这样做?

请帮帮忙? 谢谢

2 个答案:

答案 0 :(得分:1)

只需在session_destroy()时删除您的会话变量即可。如果您不知道设置了哪些会话变量,您可以使用类似的东西将它们打印出来

 <?php 
 session_start(); 
 Print_r ($_SESSION);
 ?>

如果您要注销用户,您需要unset()用户ID也可以查看php手册

http://php.net/manual/en/function.session-destroy.php(阅读说明)

答案 1 :(得分:0)

默认情况下,PHP使用PHP会话机制。此会话通过factories.yml进行配置。默认配置如下:

  user:
    class: myUser
    param:
      timeout:         1800
      logging:         %SF_LOGGING_ENABLED%
      use_flash:       true
      default_culture: %SF_DEFAULT_CULTURE%

因此,默认情况下,会话将在1800秒(= 30分钟)后自动超时。

您自己的factories.yml会覆盖Symfony中的默认factories.yml(可在/lib/vendor/symfony/lib/config中找到)。在那factories。yml the user factory is defined like above. So if that configuration is sufficient for you, you don't have to anything. If you want to change the timeout, you can override the appropriate lines in your own factories.yml . In that case you can add to following lines to your own factories.yml`:

  user:
    param:
      timeout:         900  # log out after 15 minutes

哦,我非常强烈地建议您将逻辑排除在_header.php的视图之外。所有带有if / else结构的PHP代码都应该在components.class.php中,te视图(_header.php)应该只是查看数据。

这样的事情:

控制器:

// components.class.php
public function executeHeader() {

    // code here...
    $this->isAuthenticated = true/false;
} 

查看:

 <?php if ($isAuthenticated): ?>
 ...
 <?php enif; ?>
 <?php if (!$isAuthenticated): ?>
 ...
 <?php enif; ?>

更清洁,它从逻辑中分离出来......: - )