-1

I know this question was asked a number of times but none of the solutions worked for me.

I am able to set sessions but the problem is that, for some reason, when my session is not set it keeps saying your session is set.

This is my piece of code:

session_start();    
if($_SESSION["GOALOLD"]=="GOALOLDVALS") {
        //echo $_SESSION['GOALOLD']; 
        $prog_descs     = "";
        $sel_prog_descs = "SELECT * FROM prog_description WHERE cons_id = '" . $_SESSION['behaviour_user'] . "' AND prog_id = '" . $_SESSION['prog_id'] . "' AND student_id = '" . $_SESSION['current_student'] . "' ORDER BY prog_desc_id DESC LIMIT 1";
        $sel_prog_dessc = mysql_query($sel_prog_descs) or die(mysql_error());
        if (mysql_num_rows($sel_prog_dessc) > 0) {
            $rs_prog_dessc = mysql_fetch_array($sel_prog_dessc);
            $prog_descs    = $rs_prog_dessc['prog_goal_con'];
        }echo $prog_descs;
        }else if($_SESSION["GOALNEW"]=="GOALNEWVALS") {
            //echo $_SESSION['GOALNEW'];
            $prog_descs     = "";
            $sel_prog_descs = "SELECT * FROM prog_description WHERE cons_id = '" . $_SESSION['behaviour_user'] . "' AND prog_id = '" . $_SESSION['prog_id'] . "' AND student_id = '" . $_SESSION['current_student'] . "' ORDER BY prog_desc_id DESC LIMIT 1";
            $sel_prog_dessc = mysql_query($sel_prog_descs) or die(mysql_error());
            if (mysql_num_rows($sel_prog_dessc) > 0) {
                $rs_prog_dessc = mysql_fetch_array($sel_prog_dessc);
                $prog_descs    = $rs_prog_dessc['prog_goal_con'];
            }
            echo $prog_descs;
        }else{
    }

Here is how I am unsetting my sessions:

unset($_SESSION['GOALOLD']);
unset($_SESSION['GOALNEW']);

Is there a reliable and proper way to check the session, whether it is set or not, or has some xyz value in it?

For me, they are not working.

I have even tried in another browser but still get the same problem where it keeps saying it is set. Possibly my if else is buggy or has technical issues; I am not sure.

Keep Coding
  • 636
  • 9
  • 26
  • possible duplicate of [PHP session variables wont work](http://stackoverflow.com/questions/12063647/php-session-variables-wont-work) – Glorfindel Jul 11 '15 at 10:36
  • well that is not the case anyways.. @Glorfindel – Keep Coding Jul 11 '15 at 10:37
  • Can you please check that code provided in this article works or not. [PHP Sessions](http://www.sitepoint.com/php-sessions/). If it doesn't work then there is some type of 'setup' issue. – Ryan Vincent Jul 11 '15 at 10:42
  • 1
    You need to add more code, particularly the parts you refer to in your question. Your code is just an empty if/else which does nothing. – Phil Jul 11 '15 at 10:44
  • I dont understand this is question that should be downvoted. well.. Since it very common question to ask here. – Keep Coding Jul 11 '15 at 10:46
  • 1
    @KeepCoding please include more information in your set up of the sessions so that the quality of support can reflect better what you want to know. Do you start with session_start() ? Is this standalone? Is this the first piece of code in the test file? – MyStream Jul 11 '15 at 10:51
  • well I have added more information to the question now. 2nd Yeah I have started sessions on the top of the page as well. On first call there is not session is set but it still keep saying your session is set. I am sure it is not set. Moreover, how to easily unset the sessions as well? @MyStream – Keep Coding Jul 11 '15 at 10:54
  • When you unset your session, you should also use session_destroy() and session_regenerate_id(). – MyStream Jul 11 '15 at 10:58
  • For testing you could clear your browsers cookies. Your next request will always start a fresh session. – Phil Jul 11 '15 at 11:00
  • @Phil_1984_ i have tried that too now and it keeep saying ` ["GOALOLD"]=> string(11) "GOALOLDVALS"` is set. see var_dump – Keep Coding Jul 11 '15 at 11:03
  • Then something is setting it between your **first** `session_start()` and your `var_dump`. Nowhere in your code sets any session variables, so either you didn't clear your cookies properly, or the code you have provided to us is still incomplete. – Phil Jul 11 '15 at 17:01
  • **Edit:** There is now an answer which has appeared below which would cause this behaviour. Make sure you use 2 equal characters when checking for equality `if($_SESSION["GOALOLD"] == "GOALOLDVALS")` – Phil Jul 11 '15 at 17:08

3 Answers3

0

There is not a lot here to be 'buggy'. $_SESSION is just an array..
If you var_dump($_SESSION) before your test, this may help you spot an error.

MaggsWeb
  • 3,018
  • 1
  • 13
  • 23
  • Please improve the formatting of your answer or your answer will consider downvote. – Keep Coding Jul 11 '15 at 10:38
  • 1
    @ChrisMaggs This doesn't feel like an answer so much as a comment. Possibly, improve it with information about checking if `session_id()` is blank and how to check if a session has already been started? – MyStream Jul 11 '15 at 10:44
  • @MyStream noted - Thankyou – MaggsWeb Jul 11 '15 at 10:45
0

First of all you must have to start SESSION

<?php
session_start(); // MUST write this line

if(isset($_SESSION['GOALOLD']) && $_SESSION['GOALOLD']=='GOALOLDVALS') {

}else if(isset($_SESSION['GOALNEW']) && $_SESSION['GOALNEW']=='GOALNEWVALS') {

    }else{

    }
?>
Umair Ayub
  • 19,358
  • 14
  • 72
  • 146
  • As a suggestion on coding practice, consider `function isGoalOld()` and use a format such as `if(isGoalOld()) { ... } else if(isGoalNew()) { ... } else { ... }` and handle sessions in a more compact and modular fashion, which will also help you debug it more effectively. – MyStream Jul 11 '15 at 10:47
0

You can check session variable like this:

session_start();
if(isset($_SESSION['GOALOLD']) && !empty($_SESSION['GOALOLD'])) {
   //Put here here want you want when If session of GOALOLD is set
}

Make sure you're calling session_start before reading from or writing to the session array.

Imran
  • 4,582
  • 2
  • 18
  • 37