Jump to content

[SOLVED] Function Not Returning Variables


centerwork

Recommended Posts

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

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.

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

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 />';

 

 

Archived

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

×
×
  • 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.