RE: [PHP] how to get var name and value from function?

2007-04-19 Thread Ford, Mike
On 19 April 2007 04:36, Richard Lynch wrote:

 On Wed, April 18, 2007 4:57 am, Ford, Mike wrote:
  On 17 April 2007 01:18, Richard Lynch wrote:
   Or is it explicitly stated in the manual somewhere I'm not seeing
   that one can put things in $GLOBALS directly? [shrug]
  
 http://uk2.php.net/manual/en/language.variables.predefined.php
 #language.variables.superglobals
 
 When I read that section of the manual, along with similar variables,
 such as: $_SERVER, $_GET, $_POST, $_COOKIE, $_FILES, $_ENV, $_REQUEST
 
 I do not, in my mind, see anything indicating that it is a documented
 feature that cramming some value into $_GLOBALS['foo'] is
 specifically supported...

Well the definition of $GLOBALS says:

  Contains a reference to every variable which is currently available
   within the global scope of the script.

I take that to mean reference as in the  operator, so that
$GLOBALS['foo'] is a reference to $foo, and when you assign to a
reference you also assign to

But, notwithstanding that, how about Example 12.3 at 
http://php.net/global#language.variables.scope.global?

Cheers!

Mike

-
Mike Ford,  Electronic Information Services Adviser,
JG125, The Headingley Library,
James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 812 4730  Fax:  +44 113 812 3211 


To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] how to get var name and value from function?

2007-04-19 Thread Tijnema !

On 4/19/07, Ford, Mike [EMAIL PROTECTED] wrote:

On 19 April 2007 04:36, Richard Lynch wrote:

 On Wed, April 18, 2007 4:57 am, Ford, Mike wrote:
  On 17 April 2007 01:18, Richard Lynch wrote:
   Or is it explicitly stated in the manual somewhere I'm not seeing
   that one can put things in $GLOBALS directly? [shrug]
 
 http://uk2.php.net/manual/en/language.variables.predefined.php
 #language.variables.superglobals

 When I read that section of the manual, along with similar variables,
 such as: $_SERVER, $_GET, $_POST, $_COOKIE, $_FILES, $_ENV, $_REQUEST

 I do not, in my mind, see anything indicating that it is a documented
 feature that cramming some value into $_GLOBALS['foo'] is
 specifically supported...

Well the definition of $GLOBALS says:

 Contains a reference to every variable which is currently available
  within the global scope of the script.

I take that to mean reference as in the  operator, so that
$GLOBALS['foo'] is a reference to $foo, and when you assign to a
reference you also assign to

But, notwithstanding that, how about Example 12.3 at 
http://php.net/global#language.variables.scope.global?

Cheers!

Mike


Hmm, that's quite ugly, what happens when defining a variable outside,
and inside a function. and then get the reference to it? Will the
first var be overwritten, and when the function ends, it will refer to
the old var again?

Tijnema


-
Mike Ford,  Electronic Information Services Adviser,
JG125, The Headingley Library,
James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 812 4730  Fax:  +44 113 812 3211


To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] how to get var name and value from function?

2007-04-19 Thread Richard Lynch
On Thu, April 19, 2007 3:34 am, Ford, Mike wrote:
 But, notwithstanding that, how about Example 12.3 at
 http://php.net/global#language.variables.scope.global?

That's good enough for me.

Just never noticed it before.

Thanks!

-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] how to get var name and value from function?

2007-04-19 Thread Richard Lynch
On Thu, April 19, 2007 9:21 am, Tijnema ! wrote:
 Hmm, that's quite ugly, what happens when defining a variable outside,
 and inside a function. and then get the reference to it? Will the
 first var be overwritten, and when the function ends, it will refer to
 the old var again?

I can't un-twist your sentence into code, but if you can, you could
probably test it and find out faster than we could answer...

:-)

-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] how to get var name and value from function?

2007-04-18 Thread Ford, Mike
On 17 April 2007 01:18, Richard Lynch wrote:

 On Mon, April 16, 2007 12:10 pm, Ford, Mike wrote:
  No, not just another array (although I agree about the function
  being pretty useless!) -- $GLOBALS is a superglobal array that
  contains a reference to every variable defined in the global scope,
  so that accessing $GLOBALS['var'] from anywhere is the same as
  accessing $var in the global scope.  It's a way of referencing
  global variables without having to use a global $var statement.
 
 So, as far as I can tell, you're avoiding using the documented feature
 global $var and cramming things into $GLOBALS which happens to work
 but is an undocumented feature?
 
 All the ills associated with global $var are still there, of course.

Of course -- this can be a matter of personal preference, but I have seen the 
use of $GLOBALS advocated over the global keyword as part of the 
knowing-where-your-data-came-from-at-the-point-of-use frame of mind.
 
 Or is it explicitly stated in the manual somewhere I'm not seeing that
 one can put things in $GLOBALS directly? [shrug]

http://uk2.php.net/manual/en/language.variables.predefined.php#language.variables.superglobals


Cheers!

Mike

-
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning  Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 812 4730  Fax:  +44 113 812 3211 


To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] how to get var name and value from function?

2007-04-18 Thread Richard Lynch
On Wed, April 18, 2007 4:57 am, Ford, Mike wrote:
 On 17 April 2007 01:18, Richard Lynch wrote:
 Or is it explicitly stated in the manual somewhere I'm not seeing
 that
 one can put things in $GLOBALS directly? [shrug]
 http://uk2.php.net/manual/en/language.variables.predefined.php#language.variables.superglobals

When I read that section of the manual, along with similar variables,
such as:
$_SERVER, $_GET, $_POST, $_COOKIE, $_FILES, $_ENV, $_REQUEST

I do not, in my mind, see anything indicating that it is a documented
feature that cramming some value into $_GLOBALS['foo'] is specifically
supported...

In fact, considering the context, $_GLOBALS would be the ONLY one,
other than $_SESSION, which would be writable, really.

I mean, okay, you can cram things into $_POST, but that doesn't make
them magically have been posted from the browser, and you can cram
stuff into $_ENV, but, afaik, that isn't the same as calling SetEnv,
is it?

And you can't cram stuff into $_COOKIE to set cookies, right?

You sure can't cram a file into $_FILES easily... :-)

So, to me at least, I'm not reading that as a script-writable array to
alter global data, since there is no special notation there that one
can alter global variables just by hacking into that array.

Since there *IS* a globals syntax documented, I'm gonna stick with
that, personally, as a Documented Feature.

But this is just what *I*, a known heretic, consider Best Practice.

Y'all do whatever you want. :-)

-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] how to get var name and value from function?

2007-04-16 Thread Ford, Mike
On 14 April 2007 13:16, Afan Pasalic wrote:

 Tijnema ! wrote:
  On 4/14/07, Afan Pasalic [EMAIL PROTECTED] wrote:
   function value2var($array, $print=0)
   {
  foreach ($_POST as $key = $value)
  
  I think you should change above line to :
  
 foreach ($array as $key = $value)
 yup! it's print error. I meant $array.
  {
  ${$key} = $value;
  echo ($print ==1) ? $key.': '.$value.'br'; // to test
   results and seeing array variables and values
  }
   }
   
   value2var($_POST, 1);
   
   but, I don't know how to get info from function back to
   script?!?!? :-(
  
  Uhm, it's not even possible when you don't know the keys i believe.
 after 2 hours of testing and research I realized this too, but want
 to be sure. :-(

If you really *must* do this yourself (but others have pointed out the folly of 
it), this would do it:

function value2var($array)
{
foreach ($array as $key = $value)
{
$GLOBALS['$key'] = $value;
}
}

... or, alternatively, rather than defining you own function, use extract() 
(http://php.net/extract) with one of the overwrite safety options to avoid 
blobbing existing variables.

Personally, I'd never do this in any form -- if I do it at all, I extract 
specific indices of the array with code like:

  foreach (array('name', 'address', 'email', 'setting1', 'setting2') as $key):
$GLOBALS[$key] = $array[$key];
  endforeach;

... making certain, of course, that those values get properly validated 
elsewhere.

Cheers!

Mike

-
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning  Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 812 4730  Fax:  +44 113 812 3211 


To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] how to get var name and value from function?

2007-04-16 Thread Tijnema !

On 4/16/07, Ford, Mike [EMAIL PROTECTED] wrote:

On 14 April 2007 13:16, Afan Pasalic wrote:

 Tijnema ! wrote:
  On 4/14/07, Afan Pasalic [EMAIL PROTECTED] wrote:
   function value2var($array, $print=0)
   {
  foreach ($_POST as $key = $value)
 
  I think you should change above line to :
 
 foreach ($array as $key = $value)
 yup! it's print error. I meant $array.
  {
  ${$key} = $value;
  echo ($print ==1) ? $key.': '.$value.'br'; // to test
   results and seeing array variables and values
  }
   }
  
   value2var($_POST, 1);
  
   but, I don't know how to get info from function back to
   script?!?!? :-(
 
  Uhm, it's not even possible when you don't know the keys i believe.
 after 2 hours of testing and research I realized this too, but want
 to be sure. :-(

If you really *must* do this yourself (but others have pointed out the folly of 
it), this would do it:

function value2var($array)
{
   foreach ($array as $key = $value)
   {
   $GLOBALS['$key'] = $value;
   }
}


What's the sense in above function? you're putting the variables from
1 array in another...
you could use array_merge for this.
But even then it's quite useless...



... or, alternatively, rather than defining you own function, use extract() 
(http://php.net/extract) with one of the overwrite safety options to avoid 
blobbing existing variables.


That's a better idea. :)


Personally, I'd never do this in any form -- if I do it at all, I extract 
specific indices of the array with code like:

 foreach (array('name', 'address', 'email', 'setting1', 'setting2') as $key):
   $GLOBALS[$key] = $array[$key];
 endforeach;


endforeach? never heard of that statement before, does it really exist in PHP?



... making certain, of course, that those values get properly validated 
elsewhere.

Cheers!

Mike


Sure, you should always validate your variables, but i would recommend
to only get the variables from $_GET/$_POST that you actually gonna
need, and not just everything.

Tijnema




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] how to get var name and value from function?

2007-04-16 Thread Ford, Mike
On 16 April 2007 16:18, Tijnema ! wrote:

 On 4/16/07, Ford, Mike [EMAIL PROTECTED] wrote:
  On 14 April 2007 13:16, Afan Pasalic wrote:
  
   Tijnema ! wrote:
On 4/14/07, Afan Pasalic [EMAIL PROTECTED] wrote:
 function value2var($array, $print=0)
 {
foreach ($_POST as $key = $value)

I think you should change above line to :

   foreach ($array as $key = $value)
   yup! it's print error. I meant $array.
{
${$key} = $value;
echo ($print ==1) ? $key.': '.$value.'br';
   // to test
 results and seeing array variables and values
}
 }
 
 value2var($_POST, 1);
 
 but, I don't know how to get info from function back to
 script?!?!? :-(

Uhm, it's not even possible when you don't know the
 keys i believe.
   after 2 hours of testing and research I realized this too, but
   want to be sure. :-(
  
  If you really *must* do this yourself (but others have
 pointed out the folly of it), this would do it:
  
  function value2var($array)
  {
 foreach ($array as $key = $value)
 {
 $GLOBALS['$key'] = $value;
 }
  }
 
 What's the sense in above function? you're putting the variables from
 1 array in another... you could use array_merge for this.
 But even then it's quite useless...

No, not just another array (although I agree about the function being pretty 
useless!) -- $GLOBALS is a superglobal array that contains a reference to every 
variable defined in the global scope, so that accessing $GLOBALS['var'] from 
anywhere is the same as accessing $var in the global scope.  It's a way of 
referencing global variables without having to use a global $var statement.

I was simply pointing out how you can to get info from function back to 
script when you don't know the keys, which you'd just said you believed was 
impossible! ;) ;)

Having done which, I proceeded to point out that:

  ... or, alternatively, rather than defining you own
 function, use extract() (http://php.net/extract) with one of
 the overwrite safety options to avoid blobbing existing variables.
 
 That's a better idea. :)

... Precisely ;)

   foreach (array('name', 'address', 'email', 'setting1', 'setting2')
 as $key): $GLOBALS[$key] = $array[$key];
   endforeach;
 
 endforeach? never heard of that statement before, does it
 really exist in PHP?

Of course -- would I give you non-working code? (Well, on purpose, anyway! ;) 
See http://php.net/manual/en/control-structures.alternative-syntax.php

Cheers!

Mike

-
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning  Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 812 4730  Fax:  +44 113 812 3211 


To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] how to get var name and value from function?

2007-04-16 Thread Tijnema !

On 4/16/07, Ford, Mike [EMAIL PROTECTED] wrote:

On 16 April 2007 16:18, Tijnema ! wrote:

 On 4/16/07, Ford, Mike [EMAIL PROTECTED] wrote:
  On 14 April 2007 13:16, Afan Pasalic wrote:
 
   Tijnema ! wrote:
On 4/14/07, Afan Pasalic [EMAIL PROTECTED] wrote:
 function value2var($array, $print=0)
 {
foreach ($_POST as $key = $value)
   
I think you should change above line to :
   
   foreach ($array as $key = $value)
   yup! it's print error. I meant $array.
{
${$key} = $value;
echo ($print ==1) ? $key.': '.$value.'br';
   // to test
 results and seeing array variables and values
}
 }

 value2var($_POST, 1);

 but, I don't know how to get info from function back to
 script?!?!? :-(
   
Uhm, it's not even possible when you don't know the
 keys i believe.
   after 2 hours of testing and research I realized this too, but
   want to be sure. :-(
 
  If you really *must* do this yourself (but others have
 pointed out the folly of it), this would do it:
 
  function value2var($array)
  {
 foreach ($array as $key = $value)
 {
 $GLOBALS['$key'] = $value;
 }
  }

 What's the sense in above function? you're putting the variables from
 1 array in another... you could use array_merge for this.
 But even then it's quite useless...

No, not just another array (although I agree about the function being pretty useless!) 
-- $GLOBALS is a superglobal array that contains a reference to every variable defined in the 
global scope, so that accessing $GLOBALS['var'] from anywhere is the same as accessing $var in the 
global scope.  It's a way of referencing global variables without having to use a global 
$var statement.


but $_GET and $_POST are also global variables, so you transfer
variables from one global variable to another :)



I was simply pointing out how you can to get info from function back to script 
when you don't know the keys, which you'd just said you believed was impossible! ;) ;)


You can also return an array ;)
I mean to say returning multiple variables (not arrays)



Having done which, I proceeded to point out that:

  ... or, alternatively, rather than defining you own
 function, use extract() (http://php.net/extract) with one of
 the overwrite safety options to avoid blobbing existing variables.

 That's a better idea. :)

... Precisely ;)

   foreach (array('name', 'address', 'email', 'setting1', 'setting2')
 as $key): $GLOBALS[$key] = $array[$key];
   endforeach;

 endforeach? never heard of that statement before, does it
 really exist in PHP?

Of course -- would I give you non-working code? (Well, on purpose, anyway! ;) 
See http://php.net/manual/en/control-structures.alternative-syntax.php

Cheers!

Mike


Never knew there was an alternative syntax... (in all these years...)

Of course you would (try to) give working code, but you could've be
confused by other programming languages :)

Tijnema

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] how to get var name and value from function?

2007-04-16 Thread Jim Lucas

Afan Pasalic wrote:

hi,
this one I can't figure out:

I have to assign value of an array to variable named after key of the
array several times in my project to , e.g. after I submit a form with
personal info I have
$_POST['name'] = 'john doe';
$_POST['address'] = '123 main st.';
$_POST['city'] = 'urbandale';
$_POST['zip'] = '12345';
$_POST['phone'] = '123-456-7980';
etc.

Then I assign value to the var name:
foreach ($_POST as $key = $value)
{
${$key} = $value;
}
and then validate submitted.

Though, to avoid writing all over again the same lines (even it's only 3
lines) I was thinking to create a function something like:

function value2var($array, $print=0)
{
foreach ($_POST as $key = $value)
{
${$key} = $value;
echo ($print ==1) ? $key.': '.$value.'br'; // to test
results and seeing array variables and values
}
}

value2var($_POST, 1);

but, I don't know how to get info from function back to script?!?!?
:-(

any help appreciated.

-afan


well, I would do something like this.

function value2var($in, $wanted=array(), $print=false) {
if ( ! is_array($wanted) ) {
$tmp = array($wanted);
} else {
if ( count($wanted)  0 ) {
$tmp = $wanted;
} else {
$tmp = array_keys($in);
}
}
$myArr = array();
foreach ($tmp as $key) {
if ( isset($in[$key])  !empty($in[$key]) ) {
$value = validate_data($in[$key], $key);
}
$value = '';
}
$myArr[$key] = $value;
// to test results and seeing array variables and values
if ( $print )
echo $key.': '.$value.'br';
}
}
return $myArr;
}


// Say $_POST looked like this
$_POST['first_name'] = 'John';
$_POST['last_name'] = 'Doe';
$_POST['address1'] = '123 Someplace Dr.';
$_POST['something_else'] = 'Some Value';

$wanted = array('first_name', 'last_name', 'address1');

extract(value2var($_POST, $wanted, TRUE), EXTR_SKIP, 'CLEAN');

//  The EXTR_SKIP option will force extract not to create the variable if it 
already exists.
//  Check out http://us2.php.net/extract for other options that could replace 
EXTR_SKIP

//  After running the above line, you should end of with the variables that 
follow

echo $CLEAN_first_name;  // This should echo John
echo $CLEAN_last_name;  // This should echo Doe
echo $CLEAN_address1;  // This should echo 123 Someplace Dr.

// and the something_else variable will not have been created as $CLEAN_something_else, because it 
did not exist in the $wanted array


--
Enjoy,

Jim Lucas

Different eyes see different things. Different hearts beat on different strings. But there are times 
for you and me when all such things agree.


- Rush

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] how to get var name and value from function?

2007-04-16 Thread Richard Lynch
On Fri, April 13, 2007 7:49 pm, Afan Pasalic wrote:
 I have to assign value of an array to variable named after key of the
 array several times in my project to , e.g. after I submit a form with
 personal info I have
 $_POST['name'] = 'john doe';
 $_POST['address'] = '123 main st.';
 $_POST['city'] = 'urbandale';
 $_POST['zip'] = '12345';
 $_POST['phone'] = '123-456-7980';
 etc.

 Then I assign value to the var name:
 foreach ($_POST as $key = $value)
 {
 ${$key} = $value;
 }
 and then validate submitted.

 Though, to avoid writing all over again the same lines (even it's only
 3
 lines) I was thinking to create a function something like:

 function value2var($array, $print=0)
 {
 foreach ($_POST as $key = $value)
 {
 ${$key} = $value;
 echo ($print ==1) ? $key.': '.$value.'br'; // to test
 results and seeing array variables and values

//make it available to the rest of the script
global ${$key};

 }
 }

 value2var($_POST, 1);

 but, I don't know how to get info from function back to script?!?!?
 :-(

 any help appreciated.

Allow me to save you some pain...

What you are doing *SEEMS* like a Good Idea to most PHP
not-quite-beginners.

It's not a Good Idea, however, because you end up with too many times
where you end up adding some other include file that ends up using
$name as a variable, and suddenly some other seemingly random portion
of your script stops working -- and you usually don't connect the two,
as the code you added to use $name worked fine for some time until
you went to use that one FORM that used this value2var() function that
makes $name global...

Only $name as a global is problematic because you've used it elsewhere.

Don't do what you are doing, because it's going to hurt you a lot
eventually.

Leave the stuff in $_POST, and deal with it early on, and put it
explicitly in variables that you want to use for that script.

Trying to generalize this and write a function and having globals all
over the place will only cause you grief long-term.

-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] how to get var name and value from function?

2007-04-16 Thread Richard Lynch
On Mon, April 16, 2007 12:10 pm, Ford, Mike wrote:
 No, not just another array (although I agree about the function
 being pretty useless!) -- $GLOBALS is a superglobal array that
 contains a reference to every variable defined in the global scope, so
 that accessing $GLOBALS['var'] from anywhere is the same as accessing
 $var in the global scope.  It's a way of referencing global variables
 without having to use a global $var statement.

So, as far as I can tell, you're avoiding using the documented feature
global $var and cramming things into $GLOBALS which happens to work
but is an undocumented feature?

All the ills associated with global $var are still there, of course.

Or is it explicitly stated in the manual somewhere I'm not seeing that
one can put things in $GLOBALS directly? [shrug]

I'd just use global $$var personally, if I was gonna do this at all...

As a middle ground one could have a SINGLE global variable, say,
$DATA, and use the var names as keys in that...

Still not something I'd really recommend doing, but at least it's less
problematic than having a zillion global vars with generic names like
$name running all through your application.

-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] how to get var name and value from function?

2007-04-15 Thread Zoltán Németh
2007. 04. 14, szombat keltezéssel 08.15-kor Afan Pasalic ezt írta:
 Tijnema ! wrote:
  On 4/14/07, Afan Pasalic [EMAIL PROTECTED] wrote:
  hi,
  this one I can't figure out:
 
  I have to assign value of an array to variable named after key of the
  array several times in my project to , e.g. after I submit a form with
  personal info I have
  $_POST['name'] = 'john doe';
  $_POST['address'] = '123 main st.';
  $_POST['city'] = 'urbandale';
  $_POST['zip'] = '12345';
  $_POST['phone'] = '123-456-7980';
  etc.
 
  Then I assign value to the var name:
  foreach ($_POST as $key = $value)
  {
 ${$key} = $value;
  }
  and then validate submitted.
 
  Are you sure you want to do this? You never know what a hacker inserts
  to your POST data, so he could easily define variables inside your
  script, especially when you're using more dangerous functions like
  system().
 I do validation after this step. :)

you should validate before this step, not after. let's say you have an
important variable called $system_setting
then someone sends you a POST with 'system_setting' in it. then you're
writing that POST value to your important variable with that foreach
stuff, and trying to validate after it - but your system_setting value
is corrupted still!

greets
Zoltán Németh

 
 
  Though, to avoid writing all over again the same lines (even it's only 3
  lines) I was thinking to create a function something like:
 
  function value2var($array, $print=0)
  {
 foreach ($_POST as $key = $value)
 
  I think you should change above line to :
 
 foreach ($array as $key = $value)
 yup! it's print error. I meant $array.
 {
 ${$key} = $value;
 echo ($print ==1) ? $key.': '.$value.'br'; // to test
  results and seeing array variables and values
 }
  }
 
  value2var($_POST, 1);
 
  but, I don't know how to get info from function back to script?!?!?
  :-(
 
  Uhm, it's not even possible when you don't know the keys i believe.
 after 2 hours of testing and research I realized this too, but want to
 be sure.
 :-(
 
 thanks.
 
 -afan
 
 
 
  Tijnema
 
  any help appreciated.
 
  -afan
 
 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] how to get var name and value from function?

2007-04-14 Thread Tijnema !

On 4/14/07, Afan Pasalic [EMAIL PROTECTED] wrote:

hi,
this one I can't figure out:

I have to assign value of an array to variable named after key of the
array several times in my project to , e.g. after I submit a form with
personal info I have
$_POST['name'] = 'john doe';
$_POST['address'] = '123 main st.';
$_POST['city'] = 'urbandale';
$_POST['zip'] = '12345';
$_POST['phone'] = '123-456-7980';
etc.

Then I assign value to the var name:
foreach ($_POST as $key = $value)
{
   ${$key} = $value;
}
and then validate submitted.


Are you sure you want to do this? You never know what a hacker inserts
to your POST data, so he could easily define variables inside your
script, especially when you're using more dangerous functions like
system().



Though, to avoid writing all over again the same lines (even it's only 3
lines) I was thinking to create a function something like:

function value2var($array, $print=0)
{
   foreach ($_POST as $key = $value)


I think you should change above line to :

   foreach ($array as $key = $value)

   {
   ${$key} = $value;
   echo ($print ==1) ? $key.': '.$value.'br'; // to test
results and seeing array variables and values
   }
}

value2var($_POST, 1);

but, I don't know how to get info from function back to script?!?!?
:-(


Uhm, it's not even possible when you don't know the keys i believe.

Tijnema


any help appreciated.

-afan


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] how to get var name and value from function?

2007-04-14 Thread Afan Pasalic
Tijnema ! wrote:
 On 4/14/07, Afan Pasalic [EMAIL PROTECTED] wrote:
 hi,
 this one I can't figure out:

 I have to assign value of an array to variable named after key of the
 array several times in my project to , e.g. after I submit a form with
 personal info I have
 $_POST['name'] = 'john doe';
 $_POST['address'] = '123 main st.';
 $_POST['city'] = 'urbandale';
 $_POST['zip'] = '12345';
 $_POST['phone'] = '123-456-7980';
 etc.

 Then I assign value to the var name:
 foreach ($_POST as $key = $value)
 {
${$key} = $value;
 }
 and then validate submitted.

 Are you sure you want to do this? You never know what a hacker inserts
 to your POST data, so he could easily define variables inside your
 script, especially when you're using more dangerous functions like
 system().
I do validation after this step. :)


 Though, to avoid writing all over again the same lines (even it's only 3
 lines) I was thinking to create a function something like:

 function value2var($array, $print=0)
 {
foreach ($_POST as $key = $value)

 I think you should change above line to :

foreach ($array as $key = $value)
yup! it's print error. I meant $array.
{
${$key} = $value;
echo ($print ==1) ? $key.': '.$value.'br'; // to test
 results and seeing array variables and values
}
 }

 value2var($_POST, 1);

 but, I don't know how to get info from function back to script?!?!?
 :-(

 Uhm, it's not even possible when you don't know the keys i believe.
after 2 hours of testing and research I realized this too, but want to
be sure.
:-(

thanks.

-afan



 Tijnema

 any help appreciated.

 -afan


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php