2

I am looking to store a variable across an entire session so that when a user closes a promotional bar it stays closed and doesn't pop back up every time they navigate to a new page.

On page load I have the following:

$.ajax
({ 
  url: 'promo.php',
  type: 'post',
  data : formData,
  success: function(result)
  {
    alert(result);
  }

});

The formData isn't too important right now as it isn't used.*

promo.php:

<?php

  if (isset($_SESSION['promoBar'])) {

    echo $_SESSION['promoBar'];

  }
  else {

    $_SESSION['promoBar'] = "closed";
    echo "does not exist";

  }

?>

The idea is that on page load it will check if the $_SESSION['promoBar'] variable exists, and if it does, return it's value. Otherwise set a value.

Currently the alert always displays does not exist. I was expecting it to display does not exist the first time and then closed each time I navigate to a new page.

What have I done wrong?

1
  • Where you are setting the session $_SESSION['promoBar'] and if so where do you have session_start ? Commented Jul 13, 2015 at 8:59

1 Answer 1

3

Try this... Use "session_start" before check

session_start();

  if (isset($_SESSION['promoBar'])) {

    echo $_SESSION['promoBar'];

  }
  else {

    $_SESSION['promoBar'] = "closed";
    echo "does not exist";

  }

?>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! This worked great! Might be worth editing your code from "start_session" to "session_start"
check it and let me know

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.