session variable cleared when page redirected.

15 views
Skip to first unread message

sso

unread,
Jun 3, 2009, 10:10:17 AM6/3/09
to Professional PHP Developers
I have a similar problem to something that was recently posted.
I set a session var called "valid" on the login page. When the
browser is redirected to the page mem/index.php it seems like the
session variable is wiped.

I've tried this on my linux server as well as locally with the same
result.

suggestions?



login.php
---------------
if ($status == 1)
{
// initiate a session
session_start();
$_SESSION['valid'] = 1;
//redirect to protected page
header("Location: mem/index.php");
exit();
}
---------------


mem/index.php
---------------
print_r($_SESSION); // prints nothing.
---------------

Rodrigo Souza

unread,
Jun 3, 2009, 11:12:26 AM6/3/09
to Professi...@googlegroups.com
try this:

mem/index.php
---------------

session_start();
print_r($_SESSION);   //   prints nothing.
--
Sem mais para o momento,
Rodrigo Souza

--------------------------------------------------------------------------------
:: Rodrigo Souza dos Santos
:: Curitiba - Brasil
:: rodrigosou...@gmail.com
--------------------------------------------------------------------------------

Robert Gonzalez

unread,
Jun 3, 2009, 11:55:42 AM6/3/09
to Professi...@googlegroups.com
You are more than likely going to have to call session_write_close() before you redirect to make sure all the necessary headers are sent:
--
Robert A. Gonzalez

Felipe Nascimento de Moura

unread,
Jun 3, 2009, 12:33:25 PM6/3/09
to Professi...@googlegroups.com
Well, I've seen it before, and the problem was the address...
we were accessing the page by its name, but, redirecting it by its IP Address...then, once the session uses the address to be "cookied", it was returning a new and empty session.
You don't NEED to use write_close in PHP 5, but session_start is needed before using SESSION variables, and specialy, before ANYTHING be writen in the page.

Hope I have been useful.

cheers.
--
Felipe N. de Moura
Desenvolvimento Web

http://thewebmind.org
fel...@thewebmind.org
---------------------------------
Fazendo da web um lugar melhor pra se viver.
Making the web a better place to live

Ivan Filho Gasparetto

unread,
Jun 3, 2009, 10:41:39 AM6/3/09
to Professi...@googlegroups.com
Add:
session_start();
to the top of your index.php - it will work.
Another thing: session_start(); should be the first instruction, out of that IF statement in the login file.

Cheers,
Ivan

2009/6/3 sso <strongs...@gmail.com>



--
Ivan Gasparetto
Project Manager / Software Engineer / Accessibility consultant
Skype: igasparetto
MSN: igasp...@hotmail.com
Phone: +44 (0) 7871 456 562

giyassuddin amani

unread,
Jun 4, 2009, 1:16:30 AM6/4/09
to Professi...@googlegroups.com
Hi,

Put session_start(); statement as a first statement on the login page.

On index page use session_start(); statement as a first statement.
Then your session will expire on index page.
--
Thanks,
Giyassuddin
91-9811952598

Nader dasuki

unread,
Jun 4, 2009, 3:12:00 AM6/4/09
to Professi...@googlegroups.com
see dear the best solution is :
<?
session_start();

       $my_session_value=$_SESSION['valid'] ;
      echo  $my_session_value;
 
regards
--
Eng. Nader Dasuqi
Project Manager
+9665339217533

Techteam

unread,
Jul 4, 2009, 2:48:43 AM7/4/09
to Professional PHP Developers
Hi,

I have put session_start() at index page and echo my session value.

But still session is cleared.

regards

On Jun 4, 12:12 pm, Nader dasuki <nader...@gmail.com> wrote:
> see dear the best solution is :
> <?
> session_start();
>
>        $my_session_value=$_SESSION['valid'] ;
>       echo  $my_session_value;
>
> regards
> On Thu, Jun 4, 2009 at 8:16 AM, giyassuddin amani <giyassud...@gmail.com>wrote:
>
>
>
> > Hi,
>
> > Put session_start(); statement as a first statement on the login page.
>
> > On index page use session_start(); statement as a first statement.
> > Then your session will expire on index page.
>

Bobby Easland

unread,
Jul 5, 2009, 12:02:16 AM7/5/09
to Professi...@googlegroups.com
If your session is not saved upon header redirect then you need to use
the http://www.php.net/session_write_close function to manually write
the data to disk.

Try something like this:

<?php
// Start the session
session_start();

// Set some session value
$_SESSION['valid'] = 1;

// Manually write the session data - should be just before the redirect
session_write_close();

// This should be a *FULL URI* per RFC specification!!!
header("Location: mem/index.php");
exit();
?>

--
Bobby Easland
http://www.oscommerce-freelancers.com

billylee

unread,
Jul 5, 2009, 8:28:21 AM7/5/09
to Professional PHP Developers
You may want to look into the subdirectory from where you're
redirecting the page to.

E.g. A session in "/login/index.php" is different from a session in "/
mem/index.php". This could a possible cause. If it is then, you may
want to use cookies to help keep track.

suhasini

unread,
Jul 5, 2009, 12:50:48 AM7/5/09
to Professi...@googlegroups.com
hi ,
hw did u assigned the varible   $my_session_value=$_SESSION['valid'] ;??
have u assigned value to $_SESSION['valid'] ; in prevous page?
--
with regrads
Suhasini
Reply all
Reply to author
Forward
0 new messages