naveen kumar
naveen kumar

Reputation: 11

php session are not working wordpress

In wordpress session is not working. i want to load div on one first time of site load.

 <?php session_start();
if(!isset($_SESSION['div_loading']))
{
   $_SESSION['div_loading'] = 1;   
?>
<div class="blockUI blockOverlay" style="z-index: 1000; border: none; margin: 0px; padding: 0px; width: 100%; height: 100%; top: 0px; left: 0px; opacity: 0.8; cursor: wait; position: fixed; background-color: rgb(0, 0, 0);"></div>
<div class="blockUI blockMsg blockPage" style="z-index: 1011; position: fixed; padding: 0px; margin: 0px; width: 806px; top: 25%; left: 19.5%; text-align: center; color: rgb(0, 0, 0); border: 3px solid rgb(170, 170, 170); background-color: rgb(255, 255, 255);">
<div id="brewery-check">
  <h2>Please Select</h2>
  <?php echo $_SESSION['div_loading'] ;
  ?>
  <a href="/#" id="brewery-link"> 
link1  
  </a>
  <a href="http://google.com" id="restaurant-link">
link2
  </a>
</div>
</div>
<?php } ?>

Upvotes: 1

Views: 934

Answers (4)

Kristian Vitozev
Kristian Vitozev

Reputation: 5971

You must use cookie instead of session. In most cases (it depends of PHP settings) the session will have limited lifetime.

If you want to do this the right way, you can use init hook and setup your cookie data there. You can place that piece of code in your functions.php file.

<?php

function sw_31159775() {
    if (!isset($_COOKIE['your_cookie_name'])) {
        $expirity= time() + (10 * 365 * 24 * 60 * 60);
        setcookie('your_cookie_name', 'some value', $expirity);
    }
}

add_action('init', 'sw_31159775');

?>

And then you can access $_COOKIE['your_cookie_name'] anywhere in your code.

Upvotes: 0

vrajesh
vrajesh

Reputation: 2942

please add code in function.php Try this:

function Sessioninit() {
    if(!session_id()) {
        session_start();
    }
}

add_action('init', 'Sessioninit', 1);

Upvotes: 1

Gopal S Rathore
Gopal S Rathore

Reputation: 9995

This should be first line in your current active theme's functions.php file.

if (!session_id())
    session_start();

Upvotes: 0

NullPoiиteя
NullPoiиteя

Reputation: 57322

your session code seems fine to me, but you are accessing them just after setting them. below i am showing how session work

When you open page requested form sever you get phpsessid and with this id you store data on server by session so you need to fetch those data .. and normally you reload/make ajax request so that you can fetch data.

Session id : data {some data}

key value pair (hash table )

so try

 <?php 

 if(isset($_SESSION['div_loading'])
     echo $_SESSION['div_loading'];
 else
     echo 1;

Upvotes: 0

Related Questions