Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to check if custom Wordpress session is set

Posted on 2016-09-08
7
Medium Priority
?
204 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:Nathan Riley
  • 4
  • 3
7 Comments
 
LVL 111

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 12

Author Comment

by:Nathan Riley
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 111

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 12

Author Comment

by:Nathan Riley
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 111

Accepted Solution

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

Author Comment

by:Nathan Riley
ID: 41789776
All for that then as well, anything special with those ans WP?
0
 
LVL 111

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

885 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