在 heroku 上重新部署应用时 PHP 会话消失

When redeploying app on heroku PHP sessions disappear

我在 Heroku 上有一个 Symfony 应用程序,它使用会话(例如,让用户保持登录状态)。



我已经配置了 memcachier(heroku addons 命令的输出):

Add-on                             Plan  Price
─────────────────────────────────  ────  ─────
memcachier (memcachier-flat-XXXX)  dev   free



我还在项目的根目录中创建了文件 .user.ini,内容如下:

session.save_handler=memcached
memcached.sess_binary=1
session.save_path="PERSISTENT=myapp_session ${MEMCACHIER_SERVERS}"
memcached.sess_sasl_username=${MEMCACHIER_USERNAME}
memcached.sess_sasl_password=${MEMCACHIER_PASSWORD}



此外,我已将此要求添加到 composer.json ext-memcached:

  "require": {
        "php": ">=5.3.9",
        "ext-memcached" : "*",
        "symfony/symfony": "2.7.*",



所以当我通过 heroku run bash 登录到实例并检查是否安装了 memcahed 模块时 - 一切看起来都很好(也通过 nginx 和 php5-fpm [=66= 执行了 phpinfo() ]相同的配置值)

~ $ php -i | grep memcache
/app/.heroku/php/etc/php/conf.d/110-ext-memcached.ini
memcached
memcached support => enabled
libmemcached version => 1.0.18
memcached.compression_factor => 1.3 => 1.3
memcached.compression_threshold => 2000 => 2000
memcached.compression_type => fastlz => fastlz
memcached.serializer => php => php
memcached.sess_binary => 0 => 0
memcached.sess_connect_timeout => 1000 => 1000
memcached.sess_consistent_hash => 0 => 0
memcached.sess_lock_expire => 0 => 0
memcached.sess_lock_max_wait => 0 => 0
memcached.sess_lock_wait => 150000 => 150000
memcached.sess_locking => 1 => 1
memcached.sess_number_of_replicas => 0 => 0
memcached.sess_prefix => memc.sess.key. => memc.sess.key.
memcached.sess_randomize_replica_read => 0 => 0
memcached.sess_remove_failed => 0 => 0
memcached.sess_sasl_password => no value => no value
memcached.sess_sasl_username => no value => no value
memcached.store_retry_count => 2 => 2
memcached.use_sasl => 1 => 1
Registered save handlers => files user memcached 



然而,当通过 heroku run bash 和 运行 php -i | grep session 检查实例时,您可以看到 session.save_handler 仍然是 files 尽管在 .user.ini session.save_handler = memcached

中配置
session
session.auto_start => Off => Off
session.cache_expire => 180 => 180
session.cache_limiter => nocache => nocache
session.cookie_domain => no value => no value
session.cookie_httponly => Off => Off
session.cookie_lifetime => 0 => 0
session.cookie_path => / => /
session.cookie_secure => Off => Off
session.entropy_file => /dev/urandom => /dev/urandom
session.entropy_length => 0 => 0
session.gc_divisor => 1000 => 1000
session.gc_maxlifetime => 1440 => 1440
session.gc_probability => 1 => 1
session.hash_bits_per_character => 5 => 5
session.hash_function => 0 => 0
session.name => PHPSESSID => PHPSESSID
session.referer_check => no value => no value



session.save_handler => files => files
                        ^^^^^^^^^^^^^^    WTF ???


session.save_path => no value => no value
session.serialize_handler => php => php
session.upload_progress.cleanup => On => On
session.upload_progress.enabled => On => On
session.upload_progress.freq => 1% => 1%
session.upload_progress.min_freq => 1 => 1
session.upload_progress.name => PHP_SESSION_UPLOAD_PROGRESS => PHP_SESSION_UPLOAD_PROGRESS
session.upload_progress.prefix => upload_progress_ => upload_progress_
session.use_cookies => On => On
session.use_only_cookies => On => On
session.use_strict_mode => Off => Off
session.use_trans_sid => 0 => 0

所以我想知道为什么 .user.ini 配置没有在真正的 php.ini 配置中生效?

这是一个常见的误解。 heroku run bash 启动项目的单独实例。 ".user.ini" 当你对项目进行实际的 http 或 https 调用时,有条件地应用,因为服务器(在你的情况下是 nginx)正在访问它试图访问的文件的路径(web.php).基本上“.user.ini”不会在全球范围内应用,也永远不会。