[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP session_start

Posted on 2006-05-10
20
Medium Priority
?
239 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:neonlights
  • 11
  • 8
20 Comments
 

Author Comment

by:neonlights
ID: 16650922
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
 
LVL 49

Expert Comment

by:Roonaan
ID: 16650934
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
 

Author Comment

by:neonlights
ID: 16651148

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:neonlights
ID: 16651163
just in case, in mysite.com

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

thanks
0
 

Author Comment

by:neonlights
ID: 16651182
Thank you for helping me......:-)
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 16651299
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
 

Author Comment

by:neonlights
ID: 16651335
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
 
LVL 49

Expert Comment

by:Roonaan
ID: 16651352
Did you launch test2.php from clicking the link, or by entering it into the location bar?

-r-
0
 

Author Comment

by:neonlights
ID: 16651390
clicking the link for test2.php
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 16651425
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
 

Author Comment

by:neonlights
ID: 16651475
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
 
LVL 49

Expert Comment

by:Roonaan
ID: 16651530
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
 

Author Comment

by:neonlights
ID: 16651623
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
 
LVL 49

Expert Comment

by:Roonaan
ID: 16651708
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
 

Author Comment

by:neonlights
ID: 16651728
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
 
LVL 49

Expert Comment

by:Roonaan
ID: 16651753
You can do

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

Then reload the page a couple of times.

-r-
0
 

Author Comment

by:neonlights
ID: 16651821
hi...

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

and the page - after many reloads:

array (
)
0
 
LVL 49

Accepted Solution

by:
Roonaan earned 2000 total points
ID: 16651906
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
 
LVL 13

Expert Comment

by:srikanthmadishetti
ID: 16652203
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
 

Author Comment

by:neonlights
ID: 16652952
thanks srikanthmadishetti...

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

very newbie here..

thanks
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses four methods for overlaying images in a container on a web page
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month18 days, 14 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question