【问题标题】:how to auto logout using $_SESSION or js timer?如何使用 $_SESSION 或 js 计时器自动注销?
【发布时间】:2011-09-21 08:00:42
【问题描述】:

我阅读了该主题,但不知道从哪里开始 第一步是什么?我有这个首先被调用的代码: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;?>

我更新的 factory.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 文件吗?如果是,如何配置?还是我在会话中这样做?

请帮忙? 谢谢

【问题讨论】:

    标签: php javascript symfony1


    【解决方案1】:

    只要你想用session_destroy() 销毁你的会话变量。如果您不知道设置了哪些会话变量,您可以使用类似的方法将它们打印出来

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

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

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

    【讨论】:

      【解决方案2】:

      默认情况下,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.ymlthe 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 ownfactories.yml. In that case you can add to following lines to your ownfactories.yml`:

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

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

      所以是这样的:

      控制器:

      // components.class.php
      public function executeHeader() {
      
          // code here...
          $this->isAuthenticated = true/false;
      } 
      

      查看:

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

      更简洁,它将视图与逻辑分开...... :-)

      【讨论】:

      • 非常感谢,请在顶部查看我的工厂文件。我可以添加您的代码还是如何配置我的代码?据我了解,用户是工厂,所以我怎么知道我必须给班级起什么名字???谢谢
      • 谢谢,请查看我更新的工厂文件。我将它设为 300,所以不必等待 30/15 分钟来测试它...你能解释一下超时后会发生什么吗?我打开了我的网站并且用户登录但没有任何反应??我仍然可以在网站上继续,因为我仍然登录??谢谢
      • 如果在超时时间内没有向网络服务器发出请求,则会话过期。之后的第一个请求将获得一个新会话,没有设置身份验证属性。 (这只是默认的 PHP 会话行为,所以您可能只想 Google 一下)...
      猜你喜欢
      • 2013-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-11
      • 2023-04-03
      • 2012-04-24
      • 2014-09-28
      相关资源
      最近更新 更多