Page 1 of 1

Session issues

Posted: Fri May 28, 2010 1:33 am
by Zigson
Hi,

I'm new with sessions and would need some help - if somebody can point me out to the solution, i'd be more than happy.

Well, here is the issue.

I have two files:

number1.php

Code: Select all

 <?php 
 // starting the session 
 session_start(); 
  // setting up variable in the session 
 $_SESSION['variable']="data"; 
 print_r($_SESSION);
 ?> 
print_r outputs an array - so far, so good.

number2.php

Code: Select all

 <?php 
 // starting the session again 
 session_start(); 
 
 // echo variable from the session that i set up on number1.php 
 echo 'My variable is ' . $_SESSION['variable']; 
 
 print_r($_SESSION);
 ?> 
Now, it looks like $_SESSION is empty.
Also i get "Warning: session_start() [function.session-start]: Cannot send session cookie..." error, eventhough i put php code before html tag. If i put the @ sign before session_start, i don't get warnings but i'm not sure if this is right thing to do.

What I'm i doing wrong?

Re: Session issues

Posted: Fri May 28, 2010 2:21 am
by Brenden
This can sometimes happen if you accidentally put a space or a blank new line before <?php - it will output information to the browser, causing the "headers already sent" message.

Re: Session issues

Posted: Fri May 28, 2010 3:50 am
by Zigson
a) Any idea why session variable is empty on second page? Isn't that a "global" variable?
b) I've deleted all spaces, no positive result.

Re: Session issues

Posted: Fri May 28, 2010 10:48 am
by katierosy
I have tested it, it works well. My variable is dataArray ( [variable] => data )
Just copy these two files text in new files, and please see if it works.

number1.php

Code: Select all

<?php
 // starting the session
 session_start();
  // setting up variable in the session
 $_SESSION['variable']="data"; 
?>

number2.php
<?php
 // starting the session again
 session_start();
 
 // echo variable from the session that i set up on number1.php
 echo 'My variable is ' . $_SESSION['variable'];
 
 print_r($_SESSION);
 ?>

Re: Session issues

Posted: Fri May 28, 2010 6:05 pm
by Brenden
I know this is unlikely, but I once discovered that my session issues were caused by me using a local hostname with an underscore in it. http://l_testsite/dosession.php

Re: Session issues

Posted: Mon May 31, 2010 12:26 am
by Zigson
katierosy funny, it really works.

Thank you for testing it. I'm not sure where the problem was, I'll report it if I find it.

Thank you all again.