Jump to content

Archived

This topic is now archived and is closed to further replies.

keley

Session variable doesn't pass to a function

Recommended Posts

I have a user-defined function that needs to have a Session variable used in the function. The Session variable (MM_Username) exist can be output before the function is called, but if I try to echo it inside the function, nothing is printed. I am certain that it is a coding issue, but I can not find it. Any help would be greatly appreciated.

<?php
$ctuser=$HTTP_SESSION_VARS['MM_Username'];
echo "0. ".$ctuser."<br>"; //it outputs the correct value fine here
function readcalendardata($month,$year){
$dbHost = "localhost";
$dbUser = "xxxx";
$dbPass = "xxxxx";
$dbName = "xxxxx";

echo "1. ".$ctuser."<br>"; // no value is output here

// Open Database Connection
$dbLink = mysql_connect($dbHost, $dbUser, $dbPass);
if (!$dbLink){
die ("Database: Couldn`t connect to mySQL Server");
}
mysql_select_db($dbName, $dbLink)
or die ("Database: Couldn`t open Database");

// Create timestamps to search with
$firstDayTimestamp = mktime(0,0,0,$month,1,$year);
$daysInMonth = date("t",$firstDayTimestamp);
$lastDayTimestamp = mktime(23,59,59,$month,$daysInMonth,$year);

// Create SQL
$sql = "SELECT calendar.id, calendar.title, UNIX_TIMESTAMP(calendar.dateField) AS timestampField, users.siteid, users.username ";
$sql .= "FROM calendar, users ";
$sql .= "WHERE UNIX_TIMESTAMP(dateField) >= " . $firstDayTimestamp . " AND UNIX_TIMESTAMP(dateField) <= " . $lastDayTimestamp . " AND users.siteid=calendar.siteid AND users.username ='" . $ctuser . "' ";
$sql .= "ORDER BY timestampField ASC";

// Read in Data
$dbResult = mysql_query($sql, $dbLink)
or die ("MySQL Error: " . mysql_error() );
$numRecords = mysql_num_rows($dbResult);
$eventsArray[] = "";
for($i=0;$i<$numRecords;$i++){
$row = mysql_fetch_assoc($dbResult);
$day = date("j",$row['timestampField']);
// Check Date isn`t already in $eventsArray
if(!in_array($day,$eventsArray)){
$eventsArray[] = $day;
}
}

// Close Database Connection
mysql_close($dbLink);

// Return eventsArray to code that called function
return $eventsArray;
}
?>
<?php
$month = date("n");
$year = date("Y");
global $_GET;
if (isset($_GET['m']) && isset($_GET['y']) ){
$month = $_GET['m'];
$year = $_GET['y'];
$dateComponents = getdate();
$day = $dateComponents['mday'];
} else {
$dateComponents = getdate();
$month = $dateComponents['mon'];
$year = $dateComponents['year'];
$day = $dateComponents['mday'];
}

$eventsArray = readcalendardata($month,$year);

Share this post


Link to post
Share on other sites
[code]
function readcalendardata($month,$year){
   global $ctuser;
[/code]


Gotta make it global.

Share this post


Link to post
Share on other sites
The $HTTP_SESSION_VARS array variable is not a superglobal variable. So, you have to specify global inside the function (see below). However, I really recommend to use $_SESSION superglobal variable if you're using PHP v4.1.0 or higher.


function readcalendardata($month,$year){

global $HTTP_SESSION_VARS, $ctuser; // Depending which one you want to use


See:

[a href=\"http://us2.php.net/manual/en/reserved.variables.php#reserved.variables.session\" target=\"_blank\"]http://us2.php.net/manual/en/reserved.vari...riables.session[/a]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.