0

I have a piece of code that is checking to see if a session ID has been set for my site, and if it hasn't, it assigns it a value from my URL. Below is the code I have:

if (!$_SESSION['test_id']){
  $_SESSION['test_id'] = strip_tags($_REQUEST["id"]);
};

My problem is not a huge one, but I am getting a notice that says:

Undefined index: test_id

Is there a better way assign this variable without setting off the notice? This piece fo code is in an Expression Engine platform (Code Igniter) so I don't have total control over the display of notices. This particular page has all error reporting turned off in my code, but it is still showing up.

Thanks so much for any help!

1
  • This question is tagged CodeIgniter. Why aren't you using the Session Class? Commented Feb 24, 2012 at 19:49

4 Answers 4

3

Test it against isset() as well. That will get rid of the notice.

if (!isset($_SESSION['test_id']) && !$_SESSION['test_id']){
  $_SESSION['test_id'] = strip_tags($_REQUEST["id"]);
};
Sign up to request clarification or add additional context in comments.

Comments

0

yes.. you can do it.. using isset()

if(!isset($_SESSION['test_id']))

This is because 'test_id' doesn't exist when you check if exists.. and with isset is not creating it...

Comments

0

You should try if(!isset($_SESSION['...'])) { }

or if(!empty($_SESSION['...')) { }

Comments

0

The equivilant code to what you are looking for is:

if (!empty($_SESSION['test_id']))

The isset($foo) && !$foo is logically equivilant to !empty(). Empty is a language construct, just like isset, but it also checks that it's a non-empty value.

So there's no need for the extract condition, just toss the !empty there, and you're golden...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.