keley Posted April 6, 2006 Share Posted April 6, 2006 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 herefunction 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 https://forums.phpfreaks.com/topic/6704-session-variable-doesnt-pass-to-a-function/ Share on other sites More sharing options...
DrDre Posted April 6, 2006 Share Posted April 6, 2006 [code]function readcalendardata($month,$year){ global $ctuser;[/code]Gotta make it global. Link to comment https://forums.phpfreaks.com/topic/6704-session-variable-doesnt-pass-to-a-function/#findComment-24355 Share on other sites More sharing options...
toplay Posted April 6, 2006 Share Posted April 6, 2006 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 useSee:[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 https://forums.phpfreaks.com/topic/6704-session-variable-doesnt-pass-to-a-function/#findComment-24357 Share on other sites More sharing options...
keley Posted April 6, 2006 Author Share Posted April 6, 2006 Thanks for the help. $_Sessions worked great. Link to comment https://forums.phpfreaks.com/topic/6704-session-variable-doesnt-pass-to-a-function/#findComment-24614 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.