Jump to content

Session variable doesn't pass to a function


keley

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);
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.