I have a function that uses its own params but also checks if any get/post values are avaliabe for different behavior.
I'd like to be able to do that in the home page, which url is: domain.com/
For example:
function simulate_get($name,$val){
// do it
}
And then, in code
..
simulate('foo','last_posts');
show_user_posts($user,$bla,$ble);
..
I know that I should add an extra paramerter to the function but still wondering if is actually posible in PHP to do that.
Just write to it:
function simulate_get($name, $val)
{
$_GET[$name] = $val;
}
As $_GET is a superglobal you can just do:
$_GET['foo'] = 'last_posts';
and then directly use it in your code:
$_GET['foo'] = 'last_posts';
show_user_posts($user,$bla,$ble);
or if you want to use a function to set it:
function simulate_get ($key, $value) {
$_GET[$key] = $value;
}
simulate_get('foo','last_posts');
show_user_posts($user,$bla,$ble);
Just change the contents of $_GET, that's no problem at all.
$_GET['foo'] = 'last_posts';
Related
I want to apply addslashes() to all the post elements got through
$this->input->post('my_var');
How can I do that ? Is there any feature like filters under wordpress for this ?
I think you want something global. My idea is to edit the global post function in the codeigniter to use addslashes on everything. You can find that function in:
/yourfolder/system/core/Input.php
You can escape it by setting it global.
function post($index = NULL, $xss_clean = FALSE)
{
// Check if a field has been provided
if ($index === NULL AND ! empty($_POST))
{
$post = array();
// Loop through the full _POST array and return it
foreach (array_keys($_POST) as $key)
{
$post[$key] = addslashes($this->_fetch_from_array($_POST, $key, $xss_clean));
}
return $post;
}
return addslashes($this->_fetch_from_array($_POST, $index, $xss_clean));
}
Although I don't really find it as good solution to modify the global functions this should do the trick in your case.
Edit: I see that input->post already does that and you would not need to add that function additionally.
Can anyone tell me why this works (it echos out "poo"):
$input = "wee";
$val = "poo";
${$input} = $val;
echo $wee;
But this doesn't:
function bodily($input) {
$val = "poo";
${$input} = $val;
}
bodily("wee");
echo $wee;
I want to use this sort of method to play with some $_POST vars. Please tell me if I can explain more... Cheers!
Your variable $wee gets only defined inside the scope of your function bodily(). It is not defined outside this function.
You could make it global, anyway this is not a useful pattern for a real life application:
function bodily($input) {
$val = "poo";
global ${$input}; // make your $wee defined in the global scope
${$input} = $val;
}
bodily("wee");
echo $wee;
outputs
poo
Because the variable is defined locally inside of the function. Let the function return the value and assign it to a variable outside of the function.
Because the variables inside a function are not accessible from outside unless inside the function you use "global $var" or pass it by reference like function (&$var) ...
in order for your code to work you need
<?php
function bodily($input) {
$val = "poo";
${$input} = $val;
echo $wee;
}
bodily("wee");
I am passing a parameter, "action", to another file, process.php. The value of "action" will be the name of a function that is defined within process.php. If the function exists, it should be called automatically. I tried the following:
$action = $_REQUEST['action'];
$function = {$action}();
if(function_exists($function))
$function;
else
die('No function.');
That, however, does not work. Any suggestions? Thanks!
As #Positive said you are calling the function on assignment. I just wanted to add a few things - this is a bit risky - what if the request contents were 'phpinfo' or something even more dangerous. Perhaps a better idea would be to select the function from an array of allowed functions:
$allowed_functions = array(
'my_function_one',
'my_function_two'
);
$action = $_REQUEST['action'];
if(in_array($action,$allowed_functions)){
{$action}();
}
else {
die('No function.');
}
Change the assignment to $function like below. Note that function_exists only take the function name.
$action = $_REQUEST['action'];
$function = $action;
Actually you are calling the function with this statment $result = $function();, see Variable functionsPHP-Manual.
and also sanitize the GET parameter according to your function name conventions.
i've been coding asp and was wondering if this is possible in php:
$data = getData($isEOF);
function getData($isEOF=false)
{
// fetching data
$isEOF = true;
return $data;
}
the function getData will return some data, but i'd like to know - is it possible to also set the $isEOF variable inside the function so that it can be accessed from outside the function?
thanks
It is possible, if your function expects it to be passed by reference :
function getData(& $isEOF=false) {
}
Note the & before the variable's name, in the parameters list of the function.
For more informations, here's the relevant section of the PHP manual : Making arguments be passed by reference
And, for a quick demonstration, consider the following portion of code :
$value = 'hello';
echo "Initial value : $value<br />";
test($value);
echo "New value : $value<br />";
function test(& $param) {
$param = 'plop';
}
Which will display the following result :
Initial value : hello
New value : plop
Using the global statement you can use variables in any scope.
$data = getData();
function getData()
{
global $isEOF;
// fetching data
$isEOF = true;
return $data;
}
See http://php.net/manual/en/language.variables.scope.php for more info.
Yes, you need to pass the variable by reference.
See the example here : http://www.phpbuilder.com/manual/functions.arguments.php
How to write a php function in order to pass parameters
like this
student('name=Nick&roll=1234');
If your format is URL encoded you can use parse_str to get the variables in your functions scope:
function student($args)
{
parse_str($args);
echo $name;
echo $roll;
}
Although, if this string is the scripts URL parameters you can just use the $_GET global variable.
Pass parameters like this:
student($parameter1, $parameter2){
//do stuff
return $something;
}
call function like this:
student($_GET['name'], $_GET['roll']);
Use parse_str.
function foo($paraString){
try{
$expressions = explode('&',$paraString);
$args = array();
foreach($expressions as $exoression){
$kvPair = explode('=',$exoression);
if(count($kvPair !=2))throw new Exception("format exception....");
$args[$kvPair[0]] = $kvPair[1];
}
....
Edit: that wayyou can do it manually. If you just want to get something out of a querrystring there are already functions to get the job done