473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

"Check PHP $_SESSION empty" problem

JnrJnr
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, 305 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.

13 3001
Dormilich
8,658 Expert Mod 8TB
I'd start with deleting line #1 in counter.php.
Jan 18 '12 #2
JnrJnr
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
8,658 Expert Mod 8TB
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
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
8,658 Expert Mod 8TB
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
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
8,658 Expert Mod 8TB
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
88
Thanks Dormilich. I will see what I can get out of them
Jan 19 '12 #9
JnrJnr
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
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
8,658 Expert Mod 8TB
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
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
8,658 Expert Mod 8TB
it might also be that your local server used output buffering ...
Jan 19 '12 #14

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

Similar topics

1
by: Nick Whitelegg | last post by:
Hello, I'm having an odd problem with combining an authentication session variable with header() redirection. Basically I have an authentication script which checks a username/password. If the...
1
by: Gary | last post by:
i have a project which is include ASP, VB, VB COM, MTS, IIS. when the user enter the site the user is asked to login using UserID and Password after that i am passing those values to COM Objects...
1
by: farooqazeem | last post by:
Hi guys, I’m facing some problem can u solve it. Problem is: I’m giving user Id and password in (Login_sess.asp) and submit it to page (sess_test.asp). I am setting session variable...
2
by: Alex Fimine | last post by:
Hi, Please help me with the following: In login.aspx page: Session("accessGranted") = 1 Response.Write("<script>window.open(""userpage.aspx"", ""User"");</script>")
14
by: Venkat Chellam | last post by:
I have a peculiar problem. I have a simple web application which loads some data from the oracle table and display in the datagrid in the webpage and datagrid has page enabled which shows 10 rows...
13
by: Simon Matthews | last post by:
I am having issues with the right way to architecture the following (using c# asp.net):- The question I have is how best pass the collected data from one web page for use in another. The...
1
by: Hema S via DotNetMonster.com | last post by:
Dear All, Problem 1 ----------- I am using Sql Server for session state and setting the timeout property. But my session is not getting expired at all. Snippet <sessionState...
17
by: jensen bredal | last post by:
Hello, i'm struggling with a somehow badly understood session scenario. I provide acces to my pages based on form authentication using Session cookies. Som of my pages are supposed to be...
2
by: Rajesh.jain25 | last post by:
Hi, I am facing a problem of automatic session timeout problem and automatic session_end event fired. Case1: As I have analyzed I get to know that the default session timeout is 20 in...
3
by: stclaus | last post by:
Hi, I'm currently experiencing a problem using sessions under php 4.4.2. I store variables and objects inside session variables, and all works well under php 5.x, but when I upload those pages to...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.