DEV Community

ahoNerd
ahoNerd

Posted on • Originally published at ahonerd.com on

A session had already been started

Solusi sederhana untuk menangani error pada PHP script yang berbunyi: Notice: session_start(): A session had already been started - ignoring in ...

PHP >= 5.4.0 || PHP 7 || PHP 8

Metode pertama adalah dengan menggunakan function session_status()

Function ini akan menghasilkan output sebagai berikut:

  • PHP_SESSION_DISABLED if sessions are disabled.
  • PHP_SESSION_NONE if sessions are enabled, but none exists.
  • PHP_SESSION_ACTIVE if sessions are enabled, and one exists.

Untuk lebih jelasnya bisa lihat di sini.

Sehingga untuk mengatasi error Notice: session_start(): A session had already been started bisa dengan cara memulai session setelah memeriksa status session, sebagai berikut:

if (session_status() == PHP_SESSION_NONE) {
  session_start();
}
Enter fullscreen mode Exit fullscreen mode

PHP < 5.4.0

Metode berikutnya lebih kompatible untuk versi PHP yang lebih lawas, yaitu dengan menggunakan function session_id().

Untuk lebih jelasnya bisa lihat di sini.

if(session_id() == '') {
  session_start();
}
Enter fullscreen mode Exit fullscreen mode

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay