Session issues

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Zigson
Forum Newbie
Posts: 3
Joined: Fri May 28, 2010 1:13 am

Session issues

Post 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?
Brenden
Forum Newbie
Posts: 12
Joined: Fri May 28, 2010 2:12 am

Re: Session issues

Post 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.
Zigson
Forum Newbie
Posts: 3
Joined: Fri May 28, 2010 1:13 am

Re: Session issues

Post 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.
katierosy
Forum Commoner
Posts: 27
Joined: Wed Apr 07, 2010 8:39 am

Re: Session issues

Post 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);
 ?>
Last edited by Benjamin on Fri May 28, 2010 8:17 pm, edited 1 time in total.
Reason: Added [syntax=php] tags.
Brenden
Forum Newbie
Posts: 12
Joined: Fri May 28, 2010 2:12 am

Re: Session issues

Post 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
Zigson
Forum Newbie
Posts: 3
Joined: Fri May 28, 2010 1:13 am

Re: Session issues

Post 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.
Post Reply