Hello, I'm having a little trouble and I'm guessing there's an easy solution... I just don't know it. I have a script that exports the contents of a table to Excel... but I need it to use a session variable to pull olny certain data. Here's my export script: <?php header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=XL_".date("m-d-Y").".xls"); header("Pragma: no-cache"); header("Expires: 0"); include("dbinfo.php"); mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die("Unable to select database"); $select = "SELECT program, hotelname, programcity, progman, pmemail, firstname, lastname FROM $usertable WHERE program='$MM_program'"; $export = mysql_query($select); $count = mysql_num_fields($export); for ($i = 0; $i < $count; $i++) { $header .= mysql_field_name($export, $i)."\t"; } while($row = mysql_fetch_row($export)) { $line = ''; foreach($row as $value) { if ((!isset($value)) OR ($value == "")) { $value = "\t"; } else { $value = str_replace('"', '""', $value); $value = '"' . $value . '"' . "\t"; } $line .= $value; } $data .= trim($line)."\n"; } $data = str_replace("\r", "", $data); if ($data == "") { $data = "\n(0) Records Found!\n"; } print "$header\n$data"; ?> PHP: You can see the "WHERE" clause is asking for program='$MM_program' Code (markup): But I'm not sure how to get that $MM_program to follow me to this page. Any help?
use session_start(); $_SESSION[MM_program]=value you want to store; PHP: it will store value in session and you can use it on any page untill you will unset the variable or destroy the session
Sorry if I'm misunderstanding... I already have it set up to pass the $MM_program info on from previous pages, but it only works with "session_start();" which doesn't work with my code, above. Is there a way to keep the session in my code, without interfering with the headers from this page? (which is what I assume is happening)
it will work <?php header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=XL_".date("m-d-Y").".xls"); header("Pragma: no-cache"); header("Expires: 0"); include("dbinfo.php"); session_start(); mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die("Unable to select database"); $select = "SELECT program, hotelname, programcity, progman, pmemail, firstname, lastname FROM $usertable WHERE program='$_SESSION[MM_program]'"; $export = mysql_query($select); $count = mysql_num_fields($export); for ($i = 0; $i < $count; $i++) { $header .= mysql_field_name($export, $i)."\t"; } while($row = mysql_fetch_row($export)) { $line = ''; foreach($row as $value) { if ((!isset($value)) OR ($value == "")) { $value = "\t"; } else { $value = str_replace('"', '""', $value); $value = '"' . $value . '"' . "\t"; } $line .= $value; } $data .= trim($line)."\n"; } $data = str_replace("\r", "", $data); if ($data == "") { $data = "\n(0) Records Found!\n"; } print "$header\n$data"; ?> PHP:
Try this: <?php # Put session_start() at the first line of the pages where you intend to use the value in $_SESSION[ 'MM_program' ] session_start(); header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=XL_".date("m-d-Y").".xls"); header("Pragma: no-cache"); header("Expires: 0"); include("dbinfo.php"); mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die("Unable to select database"); $select = "SELECT program, hotelname, programcity, progman, pmemail, firstname, lastname FROM $usertable WHERE program='" . $_SESSION[ 'MM_program' ] . "'"; $export = mysql_query($select); $count = mysql_num_fields($export); for ($i = 0; $i < $count; $i++) { $header .= mysql_field_name($export, $i)."\t"; } while($row = mysql_fetch_row($export)) { $line = ''; foreach($row as $value) { if ((!isset($value)) OR ($value == "")) { $value = "\t"; } else { $value = str_replace('"', '""', $value); $value = '"' . $value . '"' . "\t"; } $line .= $value; } $data .= trim($line)."\n"; } $data = str_replace("\r", "", $data); if ($data == "") { $data = "\n(0) Records Found!\n"; } print "$header\n$data"; ?> PHP: Put session_start(); at the beginning of all of the pages where you want to use $_SESSION[ 'MM_program' ] and replace any code that sets the variable $MM_program with $_SESSION[ 'MM_program' ] Brew
Thanks Brewster, but it still no worky. Sunnyverma, When I log in I register some session variables... all of which I can call on an any page as long as I have: <?php session_start(); ?> PHP: at the top of the code. (no big deal) But when I get to this page, which has no other code but what I've given, it doesn't like the session start code. When I add that, and try to hit the page, I get an error that the document (in a temp folder) cannot be found. On all the other pages, all I need to call on these variables is the session start code, and something like this: <? echo $MM_program;?> PHP: Does this clarify at all? I'm sorry I'm not including more code, but that's all the previous page has pertaining to session variables.
Can you post the all of the code for the page where you are setting the $_SESSION[ 'MM_program' ] session variable Brew
Ok, here it is: <?php require_once('Connections/connect.php'); ?> <?php // *** Validate request to login to this site. session_start(); $loginFormAction = $_SERVER['PHP_SELF']; if (isset($accesscheck)) { $GLOBALS['PrevUrl'] = $accesscheck; session_register('PrevUrl'); } if (isset($_POST['username'])) { $loginUsername=$_POST['username']; $password=$_POST['password']; $MM_fldUserAuthorization = "AccessLevel"; $MM_fldUserID = "ID"; $MM_redirectLoginSuccess = "admin.php"; $MM_redirectLoginFailed = "failed.php"; $MM_redirecttoReferrer = false; mysql_select_db($database_cmmreg, $cmmreg); $LoginRS__query=sprintf("SELECT ID, UserName, Password, AccessLevel, designation, program, programdate, hotelname, programcity, progman, pmemail, pmphone, hotellink FROM tbllogin WHERE UserName='%s' AND Password='%s'", get_magic_quotes_gpc() ? $loginUsername : addslashes($loginUsername), get_magic_quotes_gpc() ? $password : addslashes($password)); $LoginRS = mysql_query($LoginRS__query, $cmmreg) or die(mysql_error()); $loginFoundUser = mysql_num_rows($LoginRS); if ($loginFoundUser) { $loginStrGroup = mysql_result($LoginRS,0,'AccessLevel'); $loginID = mysql_result($LoginRS,0,'ID'); $MM_designation = mysql_result($LoginRS,0,'designation'); $MM_program = mysql_result($LoginRS,0,'program'); $MM_programdate = mysql_result($LoginRS,0,'programdate'); $MM_hotelname = mysql_result($LoginRS,0,'hotelname'); $MM_programcity = mysql_result($LoginRS,0,'programcity'); $MM_progman = mysql_result($LoginRS,0,'progman'); $MM_pmemail = mysql_result($LoginRS,0,'pmemail'); $MM_pmphone = mysql_result($LoginRS,0,'pmphone'); $MM_hotellink = mysql_result($LoginRS,0,'hotellink'); //declare session variables and assign them $GLOBALS['MM_Username'] = $loginUsername; $GLOBALS['MM_id'] = $loginID; $GLOBALS['MM_UserGroup'] = $loginStrGroup; $GLOBALS['AccessLevel'] = $accesslevel; $GLOBALS['designation'] = $MM_designation; $GLOBALS['program'] = $MM_program; $GLOBALS['programdate'] = $MM_programdate; $GLOBALS['hotelname'] = $MM_hotelname; $GLOBALS['programcity'] = $MM_programcity; $GLOBALS['progman'] = $MM_progman; $GLOBALS['pmemail'] = $MM_pmemail; $GLOBALS['pmphone'] = $MM_pmphone; $GLOBALS['hotellink'] = $MM_hotellink; //register the session variables session_register("MM_Username"); session_register("MM_id"); session_register("MM_UserGroup"); session_register("AccessLevel"); session_register("MM_designation"); session_register("MM_program"); session_register("MM_programdate"); session_register("MM_hotelname"); session_register("MM_programcity"); session_register("MM_progman"); session_register("MM_pmemail"); session_register("MM_pmphone"); session_register("MM_hotellink"); if (isset($_SESSION['PrevUrl']) && false) { $MM_redirectLoginSuccess = $_SESSION['PrevUrl']; } header("Location: " . $MM_redirectLoginSuccess ); } else { header("Location: ". $MM_redirectLoginFailed ); } } ?> PHP:
Try this code: <?php session_start(); require_once('Connections/connect.php'); // *** Validate request to login to this site. $loginFormAction = $_SERVER['PHP_SELF']; if (isset($accesscheck)) { $_SESSION['PrevUrl'] = $accesscheck; } if (isset($_POST['username'])) { $loginUsername=$_POST['username']; $password=$_POST['password']; $MM_fldUserAuthorization = "AccessLevel"; $MM_fldUserID = "ID"; $MM_redirectLoginSuccess = "admin.php"; $MM_redirectLoginFailed = "failed.php"; $MM_redirecttoReferrer = false; mysql_select_db($database_cmmreg, $cmmreg); $LoginRS__query=sprintf("SELECT ID, UserName, Password, AccessLevel, designation, program, programdate, hotelname, programcity, progman, pmemail, pmphone, hotellink FROM tbllogin WHERE UserName='%s' AND Password='%s'", get_magic_quotes_gpc() ? $loginUsername : addslashes($loginUsername), get_magic_quotes_gpc() ? $password : addslashes($password)); $LoginRS = mysql_query($LoginRS__query, $cmmreg) or die(mysql_error()); $loginFoundUser = mysql_num_rows($LoginRS); if ($loginFoundUser) { $_SESSION['MM_UserGroup'] = mysql_result($LoginRS,0,'AccessLevel'); $_SESSION['MM_id'] = mysql_result($LoginRS,0,'ID'); $_SESSION['designation'] = mysql_result($LoginRS,0,'designation'); $_SESSION['program'] = mysql_result($LoginRS,0,'program'); $_SESSION['programdate'] = mysql_result($LoginRS,0,'programdate'); $_SESSION['hotelname'] = mysql_result($LoginRS,0,'hotelname'); $_SESSION['programcity'] = mysql_result($LoginRS,0,'programcity'); $_SESSION['progman'] = mysql_result($LoginRS,0,'progman'); $_SESSION['pmemail'] = mysql_result($LoginRS,0,'pmemail'); $_SESSION['pmphone'] = mysql_result($LoginRS,0,'pmphone'); $_SESSION['hotellink'] = mysql_result($LoginRS,0,'hotellink'); //declare session variables and assign them $_SESSION['MM_Username'] = $loginUsername; $_SESSION['AccessLevel'] = $accesslevel; if (isset($_SESSION['PrevUrl']) && false) { $MM_redirectLoginSuccess = $_SESSION['PrevUrl']; } header("Location: " . $MM_redirectLoginSuccess ); } else { header("Location: ". $MM_redirectLoginFailed ); } } ?> PHP: Brew
Thanks Brewster... but you understand, I'm not having any trouble with the login code? I did try your code, then the export to excel page, just in case... but still doesn't work. I'll keep your code as you just seemed to clean it up a bit for me (thank you) but fixing code on that page doesn't seem to help me with the ability to pass session variables on to the export page. The code for that doesn't seem to like session_start(). It's very possible I'm just missing the point, but I want to reiterate, the only issue I'm having is passing code to this one page that consists only of this code: <?php header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=XL_".date("m-d-Y").".xls"); header("Pragma: no-cache"); header("Expires: 0"); include("dbinfo.php"); mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die("Unable to select database"); $select = "SELECT program, hotelname, programcity, progman, pmemail, firstname, lastname, designation, institution, title, function, funcother, site, piname, namebadge, email, phone, fax, mobile, address1, address2, city, state, zip, country, emergencyname, emergencynumber, comm, invite, vip, status, pmcomm, hotel, roompref, checkin, checkout, roomcomm, hoteldeparthour, hoteldepartmin, dietary, handicap, returnmethod, returndate, returnhour, returnmin, returnairline, returncity, returnflnumber, departmethod, seatpref, ffnumber, departdate, departhour, departmin, travelcomm, departairline, departcity, prefdeptime, departflnumber, conncity, transneeded, billstatus, dateadded, datemod FROM $usertable WHERE program='" . $_SESSION[ 'MM_program' ] . "'"; $export = mysql_query($select); $count = mysql_num_fields($export); for ($i = 0; $i < $count; $i++) { $header .= mysql_field_name($export, $i)."\t"; } while($row = mysql_fetch_row($export)) { $line = ''; foreach($row as $value) { if ((!isset($value)) OR ($value == "")) { $value = "\t"; } else { $value = str_replace('"', '""', $value); $value = '"' . $value . '"' . "\t"; } $line .= $value; } $data .= trim($line)."\n"; } $data = str_replace("\r", "", $data); if ($data == "") { $data = "\n(0) Records Found!\n"; } print "$header\n$data"; ?> PHP: I can pass any session variable to any other page that doesn't call headers. (or, at least I think that's the hang up) I appreciate your help and hope you don't give up on me.