PHP session_start

Hi experts, I am learning php .. and testing the page on a web server, not on my pc - since I am trying to fix my apache to work. anyhow,

right now, I am learning session_start.. (following a book)

first page - I have:

<?php

session_start();

$_SESSION['username'] = 'deathsbane';
$_SESSION['role'] = 'admin';
echo $_SESSION['username'];
?>
<a href="test2.php">test2.php</a></body>
</html>

and second page I have:

<?php

session_start();

echo $_SESSION['username'];
?>
</body>

First page, it is writtien down deathsbane and but, nothing written on the second page. I am not quite sure..can someone please explain to me.. what is the problem here..

Thanks all
neonlightsAsked:
Who is Participating?
 
RoonaanConnect With a Mentor Commented:
Then your browser is not accepting cookies and hence your sessionid is not stored.

You can do two things:
1) enable trans_sid and have phpsessid appended to all your urls:
<?php
  ini_set('session.use_trans_sid','1');
  session_start();

2) enable cookies for this website and trust other people use cookies as well.

-r-
0
 
neonlightsAuthor Commented:
in my book, it says that "On windows, you typically need to edit the php cofiguration file, php,ini and edit the session.save_path.. " 

but, I am running this code in bellhosting.net - not directly from my pc.

I hope I am clear.

thanks all
0
 
RoonaanCommented:
At first sight I see no problem.

Can you try having:
<?php
  error_reporting(E_ALL);
  ini_set('display_errors','1');

  session_start();

This will output any error (if any) and might help out debugging.

-r-
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
neonlightsAuthor Commented:

Notice: Undefined index: username in /services/webpages/a/r/mysite.com/public/newfolder/test2.php on line 16

line 16 is echo $_SESSION['username'];

today is my first day with php..
0
 
neonlightsAuthor Commented:
just in case, in mysite.com

I created folder in the public -:(newfolder) and placed my test1.php and test2.php

thanks
0
 
neonlightsAuthor Commented:
Thank you for helping me......:-)
0
 
RoonaanCommented:
Can you do:

echo '<pre>'.var_export($_SESSION,true).'</pre>';

This will output all session keys.

You then can test it by running test1.php and see which keys that file says it has, and then run test2.php

-r-
0
 
neonlightsAuthor Commented:
when I did that on my test1.php - I am getting:

array (
  'username' => 'deathsbane',
  'role' => 'admin',
)

and when I put the same code in the test2.php - I am getting:

array (
)
0
 
RoonaanCommented:
Did you launch test2.php from clicking the link, or by entering it into the location bar?

-r-
0
 
neonlightsAuthor Commented:
clicking the link for test2.php
0
 
RoonaanCommented:
Try running these for me:
<?php

  $keys = array('session.name','session.use_cookies', 'session.use_only_cookies','session.use_trans_sid');

  foreach($keys as $key) {
    echo '<div>'.$key.':'.ini_get($key).'</div>';
  }
?>

alternatively you can look and see if the session_ids match in the two pages:

<?php
  session_start();
  echo '<div>Session id:'.session_id().'</div>';
?>

-r-
0
 
neonlightsAuthor Commented:
Thanks for your message.

I pasted in the test1.php:
<?php

  $keys = array('session.name','session.use_cookies', 'session.use_only_cookies','session.use_trans_sid');

  foreach($keys as $key) {
    echo '<div>'.$key.':'.ini_get($key).'</div>';
  }
?>

I got :

session.name:PHPSESSID
session.use_cookies:1
session.use_only_cookies:0
session.use_trans_sid:0

and for the test2.php - I pasted the same code: and got

session.name:PHPSESSID
session.use_cookies:1
session.use_only_cookies:0
session.use_trans_sid:0

Since this is my first time with php - I am not quite sure what is happening.. but, it will help finding the problem.. thanks again.
0
 
RoonaanCommented:
With ini_get you can retrieve the configuration values as they are set in php.ini.

These settings influence the overal php behaviour.

With these settings you now know that your session works by using cookies, and does not use trans_sid.
Trans_sid is a system in which php dynamically adds the session id to internal urls to your site.
When visiting sites you sometimes see: http://mysite.com/page.php?PHPSESSID=12313adf3d3efcbd
That is the trans_sid.

For debugging purposes, do you know if cookies are enabled on your webbrowser?

-r-
0
 
neonlightsAuthor Commented:
ok, ,,,,

just to confirm something - I was trying install apache, mysql and php yesterday. and I have not finished my php yet.. So, this php.ini you are talking about.. - which one? at www.mysite.com??? 

and my cookies are disabled - but, I enabled it for www.mysite.com in zonelab.

thanks again for your help.
0
 
RoonaanCommented:
php.ini is a configuration file which is different for every php installation. Like you have httpd.conf for apache, you have php.ini for php.

-r-
0
 
neonlightsAuthor Commented:
Hi.. question: maybe, this is my browser problem???

I tried this code:

$ret = setcookie('username' , 'admin' , mktime() + 86400, '/');
if (!$ret)
{
      echo "unable to set cookie";
}

and the display is "unable to set cookie"

what does it means?
0
 
RoonaanCommented:
You can do

echo '<pre>'.var_export($_COOKIE,true).'</pre>';
setcookie('username','admin');

Then reload the page a couple of times.

-r-
0
 
neonlightsAuthor Commented:
hi...

I have
<?php
echo '<pre>'.var_export($_COOKIE,true).'</pre>';
setcookie('username','admin');
?>

and the page - after many reloads:

array (
)
0
 
srikanthmadishettiCommented:
session_start();
          $_SESSION['user']=0;  ( in some cases u need to initialize session variable then u need to update in the session then only session variable will be available after redirecting also or in another pages
          $_SESSION['user']=$username;
          session_write_close();
          header("Location:1.php");
          exit();
i think this will help u out
0
 
neonlightsAuthor Commented:
thanks srikanthmadishetti...

just to makesure, this code goes on my test1.php or test2.php?

very newbie here..

thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.