2011-09-21 37 views
0

我读了这个话题,但不知道从哪里开始 第一步是什么?我有这样的代码,被称为第一: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;?> 

我更新后的文件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文件,如果是这样的话?或者我会在会话中做到这一点?

请帮忙? 谢谢

回答

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)应该仅用于查看数据。

因此,像这样:

控制器:

​​

查看:

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

干净得多,而且它的方式隔开从逻辑的观点... :-)

+0

非常感谢您在顶部看到我的工厂文件。我可以只添加你的代码,或者我该如何配置我的?从我的理解,用户是一个工厂,所以我怎么知道我要给班级什么名字?谢谢 –

+0

看到我更新的答案,我澄清了一下... –

+0

谢谢,请参阅我更新的工厂文件..我做了300,所以不必等待30/15分钟来测试这个......你能解释暂停后会发生什么吗?我有我的网站打开和用户登录,但没有任何反应?我仍然可以继续在网站上,因为我仍然登录?谢谢 –

1

当你想要与session_destroy()一起破坏你的会话变量。如果你不知道哪个会话瓦尔设置,你可以使用像这样的东西打印出来

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

如果你想退出,你需要unset()的用户的用户ID也看看PHP手册

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