How to get a variable name as a string in PHP

Standard

From: http://mach13.com/how-to-get-a-variable-name-as-a-string-in-php


How to get a variable name as a string in PHP
PHP – Jean-Jacques Guegan
On rare occasions, you may need to retrieve a variable name as a string.

This handy little function retreives the name of the variable:

will give ‘my_var’.

I started trying to find a solution to this problem, because I needed such a function and because questions about it appeared on several mailing lists and forums:
[PHP] Is there a way to get a variable name as a string?

most people said it was not possible…
Re: [PHP] Is there a way to get a variable name as a string?
From: Rasmus Lerdorf

In PHP it is possible to use a variable if you have its name as a string :

But PHP does not natively include a way to get the name of a given variable.

This might be useful in situations where you design a function with a parameter passed by reference and you need to know which variable was sent as a parameter for this function.

The solution :
The following function retreives the variable name from a given variable:

This function has to be called with a second parameter always set to the result of the function “get_defined_vars()”:

How does it work ?
var_name compares the result of the function “get_defined_vars()” before and after modification of the variable whose name we want to find.
The initial set of defined variables passed as a parameter to var_name is first stored in “$aDefinedVars_0” and compared later.
Before modifying the value of the variable, its value is saved in $iVarSave.

is then changed to something different :

$aDefinedVars keeps track of this modification and can be compare to $aDefinedVars_0 (its initial value). The difference is the variable we modified and we can get its name as a string in the key value of the array record.

The value of the variable is restored to its initial value kept in $iVarSave and the name of the variable is returned.
Example :
The following PHP code shows the function var_name in action:

var_name can retreive the name of the variable in every case.

Application :
The most direct application of this function is, of course, a “dump” function.
Just call this “dump” function with the variable you want to monitor – do not forget to add the “get_defined_vars()” parameter…


‘;
}

dump($v_4, get_defined_vars());

?>
[/crayon]

Going further :
The function var_name can be enhanced by adding the parameter $bShowAllRef to show, if set to true, all the references relating to the variable passed as a parameter:

That’s it ! You can download the function here.

This code is free to use and published under the GPL license.

895 total views, 4 views today

Scripts to find and verify SSH logins to other machines by hacking back via Kippo

Standard

First we log all the connection attempts to my server(Live or new Virtual Machine) using the package called Kippo – http://code.google.com/p/kippo/.


Then we create this file I called grab_ssh_info.sh(Click for latest).


Then we can use the copy of /root/kippo_ssh_auths.log log to try and connect BACK to the door knockers machine and see if the login works.
If it does, add it to a success log(if new) and go on to the next one.
If it fails, ignore it. It will be deleted when we delete the copy of the log file at the end of the script.
I call this file test_ssh_info.sh


Small script(start_kippo.sh) for cron to make sure your Kippo is still running.
I noticed that the small VPS I was running would kill Kippo once awhile because I ran out of memory(32MB) and swap(32MB). So I tested every minute to see if needed starting again.


Set your log rotation to cycle the Kippo log every 24 hours or my scripts will be re testing a lot of ssh connections.
Set your cron to run them whenever. I recommend just before the logrotate cycle. Just make sure it is sequenced right. Do the grab script first.

1,004 total views, 1 views today