Posted by: prajapatinilesh | September 14, 2007

How to solve php session problem ?

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….. -> 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..

// file Name: page1.php
//ini_set(‘session.use_cookies’, true);

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>’;

// page2.php
//ini_set(‘session.use_cookies’, true);

$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>’;



  1. Thanks brother,
    This was useful to me.

  2. 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.

  3. i follow the above code its also not working, i used this code at my webhost page not in localhost.

  4. Thank u …………..:D

  5. thanks for your writing…
    this page save my nights 🙂

  6. Nice work

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


%d bloggers like this: