Solved

passing parameter into function

Posted on 2015-02-20
7
73 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
LVL 110

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This article discusses how to implement server side field validation and display customized error messages to the client.
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 …
The viewer will learn how to count occurrences of each item in an array.

726 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