if php session is not working correctly than solution might be following…..
There is two way to maintain session in php.
-> using cookie
-> url rewriting (means need to pass session id to each and every page and keep current session )
The session support allows you to register arbitrary numbers of variables to be preserved across requests. When a visitor accesses your site, PHP will check automatically (if session.auto_start is set to 1) or on your request (explicitly through session_start() or implicitly through session_register()) whether a specific session id has been sent with the request
1) in php.ini file…..
session.name -> PHPSESSID
/if session maintain by cookie…..
session.use_cookies -> must be 1 or on.
session.save_path -> directory must need to exist for to store session
//if session maintain by url rewriting…..
session.use_trans_sid -> must be 1
Try with simple example..
<?php
// file Name: page1.php
//ini_set(‘session.use_cookies’, true);
//ini_set(‘session.use_trans_sid’,1);
session_start();
echo ‘Welcome to page #1’;
$_SESSION[‘favcolor’] = ‘green’;
$_SESSION[‘animal’] = ‘cat’;
$_SESSION[‘time’] = time();
// Works if session cookie was accepted
echo ‘<br /><a href=”page2.php”>page 2</a>’;
$phpSessionId = session_id();
//echo $phpSessionId;
// Or maybe pass along the session id, if needed
//echo ‘<br /><a href=”page2.php?’ . SID . ‘”>page 2</a>’;
echo ‘<br /><a href=”page2.php?PHPSESSID=’ . $phpSessionId . ‘”>page 2</a>’;
?>
<?php
// page2.php
//ini_set(‘session.use_cookies’, true);
//ini_set(‘session.use_trans_sid’,1);
session_start();
$phpSessionId = session_id();
//echo $phpSessionId;
echo ‘Welcome to page #2<br />’;
echo $_SESSION[‘favcolor’]; // green
echo “<br>”;
echo $_SESSION[‘animal’]; // cat
echo “<br>”;
echo date(‘Y m d H:i:s’, $_SESSION[‘time’]);
// You may want to use SID here, like we did in page1.php
echo ‘<br /><a href=”page1.php”>page 1</a>’;
?>
Thanks brother,
This was useful to me.
By: Abu on October 13, 2008
at 5:22 pm
I have a problem with session. I am working on a simple project of just three pages. I need the values saved in session variables on page1 to be visible in page2 and page3, but i could only see the session from page1 visible only in page2 but not page3.
Please i need help on this.
Thanks in advance.
Samuel.
By: samuel on January 19, 2009
at 8:31 am
i follow the above code its also not working, i used this code at my webhost page not in localhost.
By: stalin on August 21, 2010
at 5:42 am
Thank u …………..:D
By: sunam on August 31, 2011
at 8:35 am
thanks for your writing…
this page save my nights 🙂
thanks
By: fredy kardian on November 28, 2011
at 5:46 pm
Nice work
By: fhfh on January 31, 2012
at 11:50 am