views:

423

answers:

5

A user will click on a link which will open a new page (code below). My problem is that when this new page is opened, it creates a NEW session ID. How do I stop this from happening?

require_once('../../config.php');    //Database connection details
require_once('../../connect.php');   //Connect to database

session_start();  <----------- HERE


if(isset($_GET['id'])) 
{

$id = $_GET['id'];
$tbl_uploads = $_SESSION['COMPANY_ID'].'_uploads';

$query = "SELECT username, type, size, content FROM $tbl_uploads WHERE id = '$id'";

$result = mysql_query($query) or die('Error, query failed');
list($username, $type, $size, $content) = mysql_fetch_array($result);

header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: inline; filename=$username");
echo $content;

mysql_close($link);
exit;
}
+1  A: 

make sure you have on the first line of the file error_reporting(E_ALL);

I think you are having some other bug here, the session_start() should not affect this.

Pentium10
I added that in, and no reportable errors. The page remains blank!
Michael
A: 

Is $_GET['id'] actually set? Slap an else clause on that initial if and spit out an error of some sort if it's not. Perhaps something's turning your click into a POST.

Marc B
It is, I've checked that. If I remove the session_start(); and change the code to type in a specific table name in place of $tbl_uploads, it works! I've tried putting a session_write_close(); function in there before the headers, but that doesn't help either!
Michael
Is this script running on a different domain (sub-domain) from the one where the original link is? Or on a different path? Perhaps your session cookie's host identifier is too restrictive and this script doesn't fall under the "real" session's bounds, so you get a new session.
Marc B
+2  A: 

Any of the required files actually send any output. If so, session_start() should go before them.

Alfabravo
You need to declare the if statement better! I would suggest using a question mark after the first sentance. ;)
Arlen Beiler
Will take it into account from now on. Thanks :)
Alfabravo
A: 

session_start() might be the problem. When you start a session it should make a session ID. That is the way it seems to me anyway. Could you include the code for session_start()?

Arlen Beiler
A: 

Why don't you try this:

$sid = session_id();
if ($sid == ""){ 
       //Session should be created
       session_start();
}//else Session already exists
pdjota