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
Solved

How to check if custom Wordpress session is set

Posted on 2016-09-08
7
59 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
  • 4
  • 3
7 Comments
 
LVL 109

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 109

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
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.

 
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 109

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 109

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

840 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