centerwork Posted October 26, 2008 Share Posted October 26, 2008 Here is my function. The Echo inside the Function echo out fine. But in the fallowing page where it echos out the returns they are blank. //This Function checks Logins and Privelage Level. function signin_ck($pg_priv) { if(isset($_SESSION['sessVar'])) { $username = $_SESSION['sessVar'][3]; $userpass = $_SESSION['sessVar'][4]; echo 'Function U: '.$username.'<br /> Function P: '.$userpass.'<br />'; $query = "SELECT * FROM site_users WHERE su_user = '$username' & su_pass = '$userpass' LIMIT 1"; $results = mysql_query($query)or die(mysql_error()); $rows = mysql_fetch_array($results); $su_priv = $rows['su_priv']; return $username; return $userpass; return $su_priv; if($results != true) { header("Location: ".$_SERVER['PHP_SELF']."?l=1"); } else { if($su_priv>$pg_priv) { header("Location: ".$_SERVER['PHP_SELF']."?p=2&er=1"); } } } else { header("Location: ".$_SERVER['PHP_SELF']."?l=1"); } } Here is the script snipit where it calls the function and echos out the returns: //Check's User autherization. if($p != 1) { signin_ck($pg_priv); } echo 'U:'.$username.'<br />'; echo 'P:'.$userpass.'<br />'; echo 'PRIV:'.$su_priv.'<br />'; Here are the Results: Function U: userx Function P: passx U: P: PRIV: Link to comment https://forums.phpfreaks.com/topic/130184-solved-function-not-returning-variables/ Share on other sites More sharing options...
ohdang888 Posted October 26, 2008 Share Posted October 26, 2008 it can't return more than 1 var... and you never ser what that's supposed to equal... but can you put multiple vars in an array... i'll write something up h/o Link to comment https://forums.phpfreaks.com/topic/130184-solved-function-not-returning-variables/#findComment-675102 Share on other sites More sharing options...
Prismatic Posted October 26, 2008 Share Posted October 26, 2008 First, return will only run once, which means $username is the only var being returned. If you need to return multiple variables stuff them in an array and return that. Also, once you call return the function exits, so if($results != true) and everything below it doesn't executute. also once you've returned your data you want to assign it to a varable, $data = signin_ck($pg_priv); now assuming you build an array with your data it will be accessable with $data["username"], $data["userpass"] etc. Link to comment https://forums.phpfreaks.com/topic/130184-solved-function-not-returning-variables/#findComment-675105 Share on other sites More sharing options...
kenrbnsn Posted October 26, 2008 Share Posted October 26, 2008 You should only have one return in a function, plus when you use a function you have to assign the returned value to a variable. To return multiple values, return them in an array. Try: <?php //This Function checks Logins and Privelage Level. function signin_ck($pg_priv) { if(isset($_SESSION['sessVar'])) { $username = $_SESSION['sessVar'][3]; $userpass = $_SESSION['sessVar'][4]; echo 'Function U: '.$username.'<br /> Function P: '.$userpass.'<br />'; $query = "SELECT * FROM site_users WHERE su_user = '$username' & su_pass = '$userpass' LIMIT 1"; $results = mysql_query($query)or die(mysql_error()); $rows = mysql_fetch_array($results); $su_priv = $rows['su_priv']; return array($username,$userpass,$su_priv); if($results != true) { header("Location: ".$_SERVER['PHP_SELF']."?l=1"); } else { if($su_priv>$pg_priv) { header("Location: ".$_SERVER['PHP_SELF']."?p=2&er=1"); } } } else { header("Location: ".$_SERVER['PHP_SELF']."?l=1"); } } ?> And <?php //Check's User autherization. if($p != 1) { list($username,$userpass,$su_priv) = signin_ck($pg_priv); } echo 'U:'.$username.'<br />'; echo 'P:'.$userpass.'<br />'; echo 'PRIV:'.$su_priv.'<br />'; ?> Ken Link to comment https://forums.phpfreaks.com/topic/130184-solved-function-not-returning-variables/#findComment-675110 Share on other sites More sharing options...
ohdang888 Posted October 26, 2008 Share Posted October 26, 2008 change this: if($p != 1) { signin_ck($pg_priv); } to this: if($p != 1) { $return = signin_ck($pg_priv); } change this: return $username; return $userpass; return $su_priv; to this: $return = array(); $return['username'] = $username; $return['userpass'] = $userpass; $return['su_priv'] = $su_priv; return $return; and this: echo 'U:'.$username.'<br />'; echo 'P:'.$userpass.'<br />'; echo 'PRIV:'.$su_priv.'<br />'; to this: echo 'U:'.$return['username'].'<br />'; echo 'P:'.$return['userpass'].'<br />'; echo 'PRIV:'.$return['su_priv'].'<br />'; Link to comment https://forums.phpfreaks.com/topic/130184-solved-function-not-returning-variables/#findComment-675111 Share on other sites More sharing options...
centerwork Posted October 26, 2008 Author Share Posted October 26, 2008 Adding the varialbes I need into a single array and returning the array variable worked. Thank you everyone for your help. Link to comment https://forums.phpfreaks.com/topic/130184-solved-function-not-returning-variables/#findComment-675173 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.