DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Michael has posted 27 posts at DZone. View Full User Profile

Are Sessions Working?

  • submit to reddit
You can use this piece of code to check if it is possible to use sessions for the visitor.

This can come handy for login pages. If you have cookies disabled and try to login, no login session can be used. In such cases it is nice to inform your visitors. 


  session_start ();
  //Check GET request
  if (isset ($_GET['cookie_check'])){
    //Check session
    if (isset ($_SESSION['session_set'])){
      //Display working message
      echo "working...";
      //Or redirect
      //header ("Location: cookie_working.php");
    } else {
      //Display failure message
      echo "failure...";
      //Or redirect
      //header ("Location: cookie_fail.php");
  session_register ("session_set");
  $_SESSION['session_set'] = true;
  header ("Location: cookie_check.php?cookie_check=1");