0

I have the following PHP code:

if(!isset($_SESSION)) {
    session_destroy();
    header('Location: register.php'); //redirect to the orginal form
} else {
    $businessOwnerID = $_SESSION['business_owner_id']; //The Business owner ID
    $mobileValidation = $_SESSION['mobile_validation'];
}

I am not unserstanding why it isn't redirecting to register.php

I tried destroying all sessions by doing session_destroy(); but it did not work either

3
  • Have you started the session on the first line of your file? Commented Aug 22, 2014 at 21:11
  • did you do a var_dump($_SESSION) to see if there's anything in it? isset($_SESSION) would only return false if the session was never started or used previously. Commented Aug 22, 2014 at 21:12
  • Please provide all code above that snippet. Even something as simple as a blank line outside of php tags could make a difference. Commented Aug 22, 2014 at 21:12

2 Answers 2

4

Since you obviously don't really want to just check if there is a session (there pretty much always will be) but if particular session attributes are set, you should be doing it this way:

if(!isset($_SESSION['business_owner_id'], $_SESSION['mobile_validation']))
{
   session_destroy();
   header('Location: register.php'); //redirect to the original form
} 
...
Sign up to request clarification or add additional context in comments.

6 Comments

In my real code I have a whole list of session values (10 or more), I was wondering whether if instead of listing them one by one I'd use !isset($_SESSION)
If you didn't call session_start() there is no $_SESSION.
@user2333968, You should always post your real code instead of confusing everyone.
session_start() is already included. @developerwjk if I paste all my code I will create more quesstions than get answers.
@GolezTrol, Assuming the code he posted is in use, he must have called session_start() or it would have redirected.
|
0

The $_SESSION super global will always be set after calling session_start().

Instead, you should check if a particular value is set:

if(!isset($_SESSION['business_owner_id'])) {
    session_destroy();
    header('Location: register.php'); //redirect to the orginal form
} else {
    $businessOwnerID = $_SESSION['business_owner_id']; //The Business owner ID
    $mobileValidation = $_SESSION['mobile_validation'];
}

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.