By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,117 Members | 1,093 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,117 IT Pros & Developers. It's quick & easy.

"Check PHP $_SESSION empty" problem

JnrJnr
P: 88
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:
Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. session_start();
  3. require("database_connect.php");
  4. if(empty($_SESSION['counter']))
  5. {
  6.     $var = $_SESSION['counter'];
  7.     echo('<span style="color:white"> conn is ' . $var . '</span>' );
  8.  
  9.     $_SESSION['counter'] = 1;
  10.     require_once("counter.php");
  11. }
  12.  
  13. ?>
Database_connect.php
Expand|Select|Wrap|Line Numbers
  1. $connection = mysqli_connect("localhost", "root", "jnr@mysql", "metal_traders") 
  2. or die('<label>failed to connect</label>');
counter.php
Expand|Select|Wrap|Line Numbers
  1. session_start();
  2. $_SESSION['counter'] = 1;
  3. $increment = mysqli_query($connection, "update Counter set Count = Count +1 where idCounter = 1") 
  4. 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 Images
File Type: jpg Untitled-1.jpg (57.6 KB, 233 views)
Jan 18 '12 #1

✓ answered by Dormilich

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.

Share this Question
Share on Google+
13 Replies


Dormilich
Expert Mod 5K+
P: 8,639
I'd start with deleting line #1 in counter.php.
Jan 18 '12 #2

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

Dormilich
Expert Mod 5K+
P: 8,639
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($_SESSION); ?
Jan 18 '12 #4

JnrJnr
P: 88
When adding var_dump($_SESSION); before and after if statement I get
array(0) { }
Any ideas?

When adding it to local server I get
array(1) { ["counter"]=> int(1) }
Jan 19 '12 #5

Dormilich
Expert Mod 5K+
P: 8,639
and on various places inside the if() ?

if that still gets an empty array, you should consult your ISP's tech support.
Jan 19 '12 #6

JnrJnr
P: 88
Ok if I put it inside the if and after the
require_once("counter.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...?
Jan 19 '12 #7

Dormilich
Expert Mod 5K+
P: 8,639
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.
Jan 19 '12 #8

JnrJnr
P: 88
Thanks Dormilich. I will see what I can get out of them
Jan 19 '12 #9

JnrJnr
P: 88
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.php:9) in /usr/www/users/metalukszb/test_session.php on line 10
It seems that metalukszb is part of my database name.
Any ideas?
Jan 19 '12 #10

JnrJnr
P: 88
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.
Jan 19 '12 #11

Dormilich
Expert Mod 5K+
P: 8,639
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)
Jan 19 '12 #12

JnrJnr
P: 88
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
Jan 19 '12 #13

Dormilich
Expert Mod 5K+
P: 8,639
it might also be that your local server used output buffering ...
Jan 19 '12 #14

Post your reply

Sign in to post your reply or Sign up for a free account.