Solved

passing parameter into function

Posted on 2015-02-20
7
69 Views
Last Modified: 2015-02-20
Dear experts,

I have a function in php that looks like this:

function getPwd ($type = 'ADMIN', $user = 'ADMINISTRTOR')
{
    -- mysql connection to Datatbase and fetch necessary info in order to return the results back
     return $result;
}


--- here is my function call:
getPwd('USER', 'ADAM');   --> returns the password for user Adam

getPwd(); --> returns the password for admin Administrator

My question is that what if I set the variables to getPwd fuction like this?

getPwd($inputType, $userName);

I wish to know how can I still get the return type of administrator's password. If I pass two variables as null, I will not get the default value as in 'ADMIN' or 'ADMINISTRATOR'. Is there a way for me to use the above function and still get the correct return from the function with the default input value as 'ADMIN' and 'ADMINISTRATOR'? Thanks
0
Comment
Question by:Kinderly Wade
7 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 40621716
function getPwd ($type = 'ADMIN', $user = 'ADMINISTRTOR')
{
     if(empty($type))
    {
         $type = 'ADMIN';
    }
     if(empty($user))
    {
         $user = 'ADMINISTRATOR';
    }
    -- mysql connection to Datatbase and fetch necessary info in order to return the results back
     return $result;
}

Open in new window

0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 40621743
But let me ask: why do you should call a function with fake parameters?
Anyway, if $userType and or $userName are empty you'll still get administrator password; if they are unset you'll get a notice about an undefined variable.
Personally, I'd do the check in the script instead of within the function:
if (!isset($userType))
{
$userType = '';
}
if (!isset($userName)
{
$userName = '';
}
getpwd($userType, $userName);

Open in new window

0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 40621969
Check if the variables are empty within the function. if you do this, then you might as well remove the default values in the arguments - they'll serve no purpose:

function getPwd ($type = '', $user = '')
{
   if(empty($type)) $type="ADMIN";
   if(empty($user)) $user="ADMINISTRATOR";
   ...
}

Open in new window


This does seem like a security risk - calling a generic function to get an admin password from the database!
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 40622349
From the sound of this question, I think you might want to step back from this issue and learn about basic PHP security and PHP client authentication.  If you write a script that can get a password, you're putting your clients at risk of having their passwords exposed!

You can provide default values for any function arguments in the function definition.
http://iconoun.com/demo/temp_kinderly_wade.php

<?php // demo/temp_kinderly_wade.php

/**
 * Show how to provide default values for a function definition
 * SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28621260.html
 */
error_reporting(E_ALL);


function show_arguments($arg1='DEFAULT VALUE', $arg2='ANOTHER DEFAULT VALUE')
{
    echo PHP_EOL . $arg1;
    echo PHP_EOL . $arg2;
}

// WITH SPECIFIED ARGUMENTS
show_arguments('Kinderly', 'Wade');
echo '<br>';

// WITH DEFAULT ARGUMENTS
show_arguments();
echo '<br>';

// WITH A MIX OF ARGUMENTS
show_arguments('Kinderly');
echo '<br>';

Open in new window

0
 

Author Comment

by:Kinderly Wade
ID: 40622388
Hi all,

Is it possible that I can pass an empty value into the function which will give me the default arguments? (Default arguments are 'ADMIN' and 'ADMINISTRATOR'). I've tried this:

$type;
$user;

function getPwd($type, $user);

and get the return pwd of 'ADMINISTRATOR' as return result?

Hi Ray, nice to see your post and thanks Chris and Marco for the posts.
0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 250 total points
ID: 40622393
Look at the code I've already posted. Set your variables to an empty string and then pass them in:

$type = '';
$user = '';

$password = getPwd($type, $user);

Open in new window

0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 40622678
Not to polemize, but what's the difference between my comment ID 40621743 end the accepted one ID 40622393? I'm missing something...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
can windows 10 wamp send mail()? 3 34
unset shopping cart session 15 32
datetime in sql 6 31
array_values - reorder after unset? 5 12
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now