Solved

passing parameter into function

Posted on 2015-02-20
7
68 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 30

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 30

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 42

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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 42

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 30

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

759 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

20 Experts available now in Live!

Get 1:1 Help Now