I started to use memcached for storing session files, but Session doesn't work and apache sends response too slow. How can I solve the problem?
Modifications in php.in are:
;session.save_handler = files
session.save_handler = memcached
session.save_path = "tcp://127.0.0.1:11211?persistent=1&weight=1&timeout=1&retry_interval=15"
I solved the problem, Solution is connection url without tcp://
,so session.save_path should be
session.save_path = "127.0.0.1:11211?persistent=1&weight=1&timeout=1&retry_interval=15"
read this page:
http://www.php.net/manual/en/memcached.sessions.php
you should modify php.ini like that
session.save_handler = memcached
Change memcached to memcache
Maybe Helpful for you
Else
That Check For Server Permissions And Check For Charset of Your PHP script And
Put this code:
in being:
<?php
ob_start ();
?>
at the end:
<?php
ob_flush ();
?>
Related
I have an issue with PHP when I use session with save_handler redis or memcached.
I use the ProcessWire CMS and both session handler method working generally fine but I found one strange behaviour: I can't upload multiple images in parallel. The image which finishes at least are overriding all other uploaded images in the database.
Since yet, nobody used really ProcessWire with this session handlers and I guess it isn't a bug in ProcessWire itself. Maybe it is a bug in phpredis, memcached extension or PHP itself or I have some wrong configuration.
I used following in php.ini:
For Redis:
session.save_handler = redis
session.save_path = "tcp://127.0.0.1:6379"
For memcached:
session.save_handler = memcached
session.save_path = "127.0.0.1:11211"
Could it depend on something like session locking?
Deactivating/activating it doesn't help me.
The issue in the processwire github repo:
https://github.com/processwire/processwire-issues/issues/798
I found a solution. I added following to my php.ini
redis.session.locking_enabled = 1
redis.session.lock_expire = 60
redis.session.lock_retries = -1
A basic page page with just session_start(); loads just fine, but once I've set something, for example $_SESSION['pet']="dog";, the page load time is around 5 seconds.
I'm using AWS's memcached server and the connection time to it from the EC2 instance is really fast. I'm not sure where the slow down is coming from.
The session.save_handler is set to memcached and session.save_path is set to xxx.cfg.use1.cache.amazonaws.com:11211
phpinfo also displays Registered save handlers as files user memcache memcached
EDIT :
I uploaded test files to demonstrate the issue. The first file is simply session_start(); print_r($_SESSION); (http://rr915webapi.us-east-1.elasticbeanstalk.com/session.php). The second file is session_start();$_SESSION['pet']="dog";$_SESSION['name']="bob";(http://rr915webapi.us-east-1.elasticbeanstalk.com/session-set.php). After you load the second file, you can see the first takes a while longer to load than initially did.
By setting the following in the PHP ini file, the response time was reduced down to milliseconds.
session.lazy_write = 0
memcached.sess_locking = Off
Some possibilities :
if your PHP server running your PHP code and your memcached server / cfg.use1.cache.amazonaws.com are hosted on different regions, it can explain all this time...
there seems to be a a bug in libmemcached 1.0.16...if you update to 1.0.18, will fix the problem, see https://github.com/iuscommunity/wishlist/issues/143 comments and https://bugs.launchpad.net/libmemcached/+bug/1589344
<?php
session_start();
if(isset($_SESSION["counter"])){
echo session_id()." ".$_SESSION["counter"];
$_SESSION["counter"]++;
}
else{
$_SESSION["counter"]=0;
echo "start counter";
}
?>
It's just a basic example code for session. It working find of my PC using XAMPP. But it doesn't work at all when I put it in to my vps webserver. The out put only include"start counter" and never changed whatever I refresh the page. I checked php.ini both on XAMPP and vps. variables_order = "GPCS" request_order = "GP" register_globals = Off session.save_handler = files Above configurations are same on XAMPP and vps.
It can be due to the reason that your session expires too fast on another server. Start by making sure you're setting the session variable correctly. It's possible that sessions either aren't enabled or aren't configured correctly in the php.ini file on your server.
You can try putting this in front of the file to see any errors. When you see the error you can figure out where you have gone wrong.
error_reporting(E_ALL);
ini_set('display_errors', 1);
I'm busting my brains over this issue (it should be straightforward), but can't seem to find a solution so hopefully one of you can help me. I'm trying to store sessions using php's memcache extension.
I'm running MAMP and have installed the extension correctly (I think...it shows up when I do phpinfo), am running the daemon and can connect to it through php, using something like this:
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
So I KNOW that php can communicate with the daemon. The problem is when I enable memcache session storage in php.ini and try to do a session_start() it gives me an error that it "Cannot find save handler memcache". This is what is in my php.ini:
session.save_handler = memcache
session.save_path = "tcp://127.0.0.1:11211"
Any help would be much appreciated =(
Some additional info:
Installed using the directions here:
http://lullabot.com/articles/setup-memcached-mamp-sandbox-environment
on OSX 1.6.4 and i put the .so file in the proper directory
Memcached, not Memcache. Same as the name of the class.
session.save_handler = memcached
EDIT since the OP indicated that it was just a typo.
There are some similar stories here:
http://www.dotdeb.org/2008/08/25/storing-your-php-sessions-using-memcached/
http://phpslacker.com/2009/03/02/php-session-clustering-with-memcache/
Did you say "yes" when the installer asked you "Enable memcache session handler support?"
If it doesn't work, try the new Memcached extension (with the D). It's supposed to be better somehow.
If you want to use memcached instead of memcache, make sure you don't have tcp:// in your session.save_path. So you should fix your session.save_path from
session.save_path = "tcp://127.0.0.1:11211"
to
session.save_path = "127.0.0.1:11211"
You might just still need to install the PHP memcached extension, for example yum install php55-pecl-memcached
You will need to enable the memcache session handler support then installing php5-memcache. Which OS are you using and how have you installed it?
I have a very simple test page for PHP session.
<?php
session_start();
if (isset($_SESSIONS['views']))
{
$_SESSIONS['views'] = $_SESSIONS['pv'] + 1;
}
else
{
$_SESSIONS['views'] = 0;
}
var_dump($_SESSIONS);
?>
After refreshing the page, it always show
array
'views' => int 0
The environment is XAMPP 1.7.3. I checked phpInfo(). The session is enabled.
Session Support enabled
Registered save handlers files user sqlite
Registered serializer handlers php php_binary wddx
Directive Local Value Master Value
session.auto_start Off Off
session.bug_compat_42 On On
When the page is accessed, there is session file sess_lnrk7ttpai8187v9q6iok74p20 created in my "D:\xampp\tmp" folder. But the content is empty.
With Firebug, I can see cookies about the session.
Cookie PHPSESSID=lnrk7ttpai8187v9q6iok74p20
It seems session data is not flushed to files.
Is there any way or direction to trouble shoot this issue?
Thanks.
BTW, it is $_SESSION not $_SESSIONS.
Hence why it isn't saving the data.
The variable you need to set is called $_SESSION not $_SESSIONS
Heres what I did ...
Install sqlite for php if older than php5 (installed and enabled by default since php5).
In your php.ini (for the location see phpinfo() or run>php -i) change the line ..
session.save_handler = files
to
session.save_handler = sqlite
Then in the same file (php.ini) make sure NOTICES are turned on
error_reporting = E_ALL
restart apache if you changed php.ini
/etc/init.d/apache2 restart
Now you will be getting a sensible error message most likely revealing you dont have correct permissions to your session.save_path so ...
Find out your web services user and group unless known.
There are several ways to do so but I placed this line temporarily in my php code ...
print_r(posix_getpwuid(posix_getuid()));
And I found that user www-data and group www-data were set for this web user by my ispconfig3.
Make a session directory on your web path (probably /var/www) for the web user.
sudo mkdir /var/www/whatever;
sudo chown <user_from_last_step>:<group_from_last_step> /var/www/whatever
eg>sudo chown www-data:www-data /var/www/whatever
Again in your php.ini file make sure session.save_path is commented out, something like ...
;session.save_path = /var/lib/php5
but not
session.save_path = /var/lib/php5
restart apache if you edited php.ini
Now go to your php code (Many have a SetEnv.php ish type script which does some global stuff and is imported by all files) and add the following line BEFORE you call session_start() ...
ini_set('session.save_path', '0;0750;/var/www/whatever');
Note that we set session.save_path in your code rather than in php.ini to help keep it secure, so to replace 'whatever' with something inventive would be useful.
By now it should appear to be working, but not quite, it doesent work accross ajax requests for some reason that I dont care about related to sqlite ... so ...
Go back to your php.ini file and change the line ...
session.save_handler = sqlite
back to
session.save_handler = files.
restart apache
Fixed :) Hopefully :|
use $_SESSION and check it by print_r($_SESSION);
<?php
session_start();
if (isset($_SESSION['views']))
{
$_SESSION['views'] = $_SESSION['pv'] + 1;
}
else
{
$_SESSION['views'] = 0;
}
echo "<pre>";
print_r($_SESSION);
?>