"Check PHP $_SESSION empty" problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JnrJnr
    New Member
    • Oct 2009
    • 88

    "Check PHP $_SESSION empty" problem

    Hi all could someone please explain to me whats wrong with this code?...(see image for code or check bellow)

    When I run the script it should check if the session variable is empty, if it is then give it a value.

    The next time the script runs (in the same page session),
    then the session variable will ofcourse not be empty meaning that the if statement should not run.
    The problem is that is always runs no matter if the session variable has a value or not.

    Main Script:
    Code:
    <?php 
    session_start();
    require("database_connect.php");
    if(empty($_SESSION['counter']))
    {
    	$var = $_SESSION['counter'];
    	echo('<span style="color:white"> conn is ' . $var . '</span>' );
    
    	$_SESSION['counter'] = 1;
    	require_once("counter.php");
    }
    
    ?>
    Database_connec t.php
    Code:
    $connection = mysqli_connect("localhost", "root", "jnr@mysql", "metal_traders") 
    or die('<label>failed to connect</label>');
    counter.php
    Code:
    session_start();
    $_SESSION['counter'] = 1;
    $increment = mysqli_query($connection, "update Counter set Count = Count +1 where idCounter = 1") 
    or die('<label>failed to update</label>');
    I am running this code of an internet server. But running locally there seems to be no problem.
    Attached Files
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    I'd start with deleting line #1 in counter.php.

    Comment

    • JnrJnr
      New Member
      • Oct 2009
      • 88

      #3
      Thanks for reply but tried that and still no avail...
      Do you think the problem could be with Hetzner's web hosting servers?

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        Do you think the problem could be with Hetzner's web hosting servers?
        I really don't know.


        what do you get if you add var_dump($_SESS ION); ?

        Comment

        • JnrJnr
          New Member
          • Oct 2009
          • 88

          #5
          When adding var_dump($_SESS ION); before and after if statement I get
          array(0) { }
          Any ideas?

          When adding it to local server I get
          array(1) { ["counter"]=> int(1) }

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            and on various places inside the if() ?

            if that still gets an empty array, you should consult your ISP's tech support.

            Comment

            • JnrJnr
              New Member
              • Oct 2009
              • 88

              #7
              Ok if I put it inside the if and after the
              require_once("c ounter.php"); (after the $_session gets its value) I get array(1) { ["counter"]=> int(1) }
              ...thats just proof that the session variable has a value and therefore should not run the if statement, but yet it still does...?

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                the different behaviour of your local server vs. the ISP's server indicate a configuration problem. you need to discuss it with the ISP's tech support as this is out of the realm of what we can change.

                Comment

                • JnrJnr
                  New Member
                  • Oct 2009
                  • 88

                  #9
                  Thanks Dormilich. I will see what I can get out of them

                  Comment

                  • JnrJnr
                    New Member
                    • Oct 2009
                    • 88

                    #10
                    Hey Dormilich, I put php_flag display_errors on and this is what I get:
                    Warning: session_start() : Cannot send session cookie - headers already sent by (output started at /usr/www/users/metalukszb/test_session.ph p:9) in /usr/www/users/metalukszb/test_session.ph p on line 10
                    It seems that metalukszb is part of my database name.
                    Any ideas?

                    Comment

                    • JnrJnr
                      New Member
                      • Oct 2009
                      • 88

                      #11
                      SOLVED!

                      Ok it looks like I got it working. Seems like you have to put session_start() right at the beginning of your code before any code is sent.

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        Seems like you have to put session_start() right at the beginning of your code before any code is sent.
                        of course. how else are you supposed to send HTTP headers? and to set the session cookie, you need to send a header.

                        you should have mentioned that there is output before that. from what you were giving, it seemed that these were the complete scripts (up to that point)

                        Comment

                        • JnrJnr
                          New Member
                          • Oct 2009
                          • 88

                          #13
                          I use to overlook that when developing on local server and seemed to work. Its my first time I work on a hosted server and Im pretty much still a junior dev at this. Thanks

                          Comment

                          • Dormilich
                            Recognized Expert Expert
                            • Aug 2008
                            • 8694

                            #14
                            it might also be that your local server used output buffering ...

                            Comment

                            Working...