2

The goal is to create a repository of a custom session files. When I use this code to write data to a session file, php creates two files with different names but with the same content. Why is this happening?

$uniqFileName = sha1(uniqid('', true)) . '.sess';
$path = '/storage/sessions/';
$sessionContent = ['name' => 'SomeName', 'age' => 'SomeAge'];
file_put_contents($path . $uniqFileName, serialize($sessionContent));

This creates two files at a time with the same content:

0b2399001549e543d067ea28c6561a1b752f58a9.sess
685fcb86fc7310d58e1154ca6b6d029630bb6d56.sess

The contents of both files:

a:2:{s:4:"name";s:8:"SomeName";s:3:"age";s:7:"SomeAge";}

It is understood that this will be a class, but the code works the same way in the context of the class and in index.php.

  • this code is not enough to track the error. show when and which condition it is running? – urfusion Feb 09 '16 at 08:06
  • Where are you doing this? Is this in the index.php in the document root? – codisfy Feb 09 '16 at 08:07
  • It is understood that this will be a class, but the code works the same way in the context of the class and in index.php – Anton Kalmykov Feb 09 '16 at 08:10
  • Check your access_log(if you are using apache and have access to it, many shared hosting providers will also give you access to the logs). Check there if you see two requests being made. – codisfy Feb 09 '16 at 08:14
  • 1
    @codeHeart Thank you. You're absolutely right. The problem was in .htaccess file which is twice addressed index.php. – Anton Kalmykov Feb 09 '16 at 08:24

1 Answers1

4

Since both those names are different using uniq(), that means your code is being called twice for sure. Look for that. There must be a loop or 2 calls to this code section. Can't happen by itself.

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95