views:

105

answers:

2

i start a new session with : session_start(); then i set some session variables like this : $_SESSION['name']=$_POST['name']; and some another variables. at bottom of page i set header to diffrent page : header('location: index.php'); exit(); now in new page (index.php i can't access to my session variables, like $_SESSION['name']) what's wrong ? thanks.

+6  A: 

Are you calling session_start() in your other pages where you're trying to access your written $_SESSION variables? You will need to do that too before trying to read anything, e.g.:

session_start();
$blah = $_SESSION['blah'];
karim79
+4  A: 

This is a known problem in PHP, HTTP, or whoever you want to blame. Basically, you cannot set cookies and redirect using HTTP in the same request. When you are starting a session that has not been created yet, you are sending a cookie.

Two options:

  1. start the session elsewhere, or
  2. send either JavaScript that forces a redirect, or use a meta tag.

Example:

<?php /* set session cookies */ ?>
<script>window.location.replace("index.php");</script>
carl
SESSION vars aren't cookies, but the session id is. You can set all the session vars you want, anywhere you want as long as you've already started the session in another script.
davethegr8
Yes, exactly, I've updated my response.
carl