Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Benjiii's avatar

Session disappears after a few image-Requests

Hello @ all,

Unfortunately I have a problem by loading images in a sessioned shopping-cart by request like this:

Route:

Route::get('/images/{size}/{id}', 'ImageController@getImage');

And a little ImageController:

class ImageController extends Controller
{

    public function getImage($size, $id)
    {

        $pixelate = 1;
        $filepath = 'img/products/front/' . $id;

        if (!file_exists($filepath)) {

            $filepath = 'img/noimage.jpg';
            $pixelate = 0;

        }

        switch ($size) {
            case 'small':
                $size = [70, 100];
                break;
            default:
                $size = [140, 200];
        }

        $img = Image::make($filepath)->resize($size[0], $size[1]);

        if($pixelate){
            $img->pixelate(5);
        }

        return $img->response('jpg');

    }

}

When I add a few products to the shopping-cart and reload the page a few times, the session-data will be lost completely. When I load the images without request directly from file, all works fine.

The issue appears locally (xampp) and on the server (forge). So I suspect that there is not a server problem.

The config sessions.php file, is unaffected.

I've tried different solutions with the middleware, but unfortunately without any success. It looks like, the session were deleted after some requests.

Is there any experience?

Thanks!

Laravel Framework version 5.2.43

0 likes
2 replies
gently69's avatar

I think I've the same issue. The case can be simplified as follows:

Define this routes:

Route::get('dummyimage/{i}', function($i) {
    return '';
});

Route::group(['middleware' => 'auth'], function () {
    Route::get('killmysession', function() {
        print_r(Session::all());
        for ($i=0; $i < 500; $i++) {
            echo '<img src="' . url('dummyimage/' . $i) . '"/>';
        }
    });
});

If you call the url "./killmysession", sometimes your session will be killed.

My system:

  • SunOS 5.10 Generic_142901-13 i86pc
  • PHP 5.6.25, Not Thread Safety
  • Laravel: 5.3.10
  • Session Driver: file

Workaround: I've written "session_start();" on the top of the route file. I know, this is not a good solution, but it works for me.

elbakly's avatar

I had the same error and got it resolved

it was all in the middleware kernel, I had the Start Session and Share error session copied to the protected $middleware array before the Language Middleware everything worked fine with I removed it from $middleware array and left it in the $middlewareGroups web with the language and everthing was working fine for me.

hint: check php artisan route:list and make sure there is no middleware applied twice on your routes,

Credit goes to some guy here saying he used session_start(); in the begining of the route file which when i tried worked by was slow

        \App\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
        \App\Http\Middleware\TrustProxies::class,
//        \Illuminate\Session\Middleware\StartSession::class,
//        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\MailConfigChange::class,
    ];

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
//             \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
 \App\Http\Middleware\Language::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
            \BaklySystems\Hydrogen\Http\Middleware\AdminPanelMenu::class,
        ], ```

Please or to participate in this conversation.