Jump to content

Session variables not working


devadok

Recommended Posts

Hi,

 

I'm developing an addon and inside the hooks.php, I have the following code:

 

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

function my_addon_example() {
   $_SESSION['test_var'] = 'test_val';
}

 

When a refresh the page and try to read the session var that I have set, the variable doesn't exist.

Doesn't anybody knows what am I doing wrong?

 

Thanks

Link to comment
Share on other sites

Hi,

 

sorry for didn't putting this on the code above, but this is being done:

 

add_hook('AdminAreaHeaderOutput', 1, 'my_addon_example');

 

The function is being called in every refresh, as expetced. But I'm not able to read the $_SESSION values defined previously.

Link to comment
Share on other sites

Hi,

 

basically I wanna do some "checking" code every x minutes. Example: Check if there is any new To-do Item. But instead of doing it on every refresh, I wanna check it every 10 minutes.

 

add_hook('AdminAreaHeaderOutput', 1, 'check_something');
function check_something($vars) {
$check = true;
if (isset($_SESSION['last_checked'])) {
	$last_checked = $_SESSION['last_checked'];
	$now = time();

	// cache 10 minutes - no need to check again
	if (($now - $last_checked) < (10 * 60)) {
		$check = false;
	}
}

        if ($check) {
            // do some checking code
            // ....
            // update last checked time
            $_SESSION['last_checked'] = time();
        }
}

 

Thanks

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated