Solved

How to check if custom Wordpress session is set

Posted on 2016-09-08
7
76 Views
Last Modified: 2016-09-08
I'm trying to figure out how to check if a custom Wordpress session is set.

I set it like this:
$session->set_userdata( 'loggedIn', $loginGood );

Open in new window


Then when looking it up on other pages I use this:
$session->userdata('loggedIn')

Open in new window


I tried checking to see if set like:
if(isset($session->userdata('loggedIn')))

Open in new window


But the above is incorrect syntax.
0
Comment
Question by:N R
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41789674
Are you using this?
https://wordpress.org/plugins/wp-session-manager/

The syntax looks correct (except for missing semi-colons at the end).  What's the issue?  Can you assign the value of $session->userdata() to a variable?
0
 
LVL 11

Author Comment

by:N R
ID: 41789681
Yes so assigning the session is working.  

But when trying to see if the session is set like this I get a syntax error in my editor and the page is just a white page and does not load.

if(isset($session->userdata('loggedIn'))){
	echo 'working';
}else{
	echo 'not working';
}

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41789699
OK, I think this may be because isset() is looking for a variable, not a function call.  Maybe try it like this:
$x = $session->userdata('loggedIn');

if(!empty($x)){
    echo 'working';
}else{
    echo 'not working';
}

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Author Comment

by:N R
ID: 41789713
Then I get the problem where the variable isn't set.
Fatal error: Call to a member function userdata() on a non-object

Open in new window

0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 41789736
You may need to use global $session in this context.
0
 
LVL 11

Author Comment

by:N R
ID: 41789776
All for that then as well, anything special with those ans WP?
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41790071
Yeah, I don't know why WP does not just use the PHP session - so much easier!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
The viewer will learn how to count occurrences of each item in an array.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question