how can i call function declared in my php file from address bar? I already tried appending function name with site address but it didn't called the function..
http://myWebSite.com/myFile.php?writeMessage
myfile.php:
<?php
/* Defining a PHP Function */
function writeMessage()
{
echo "You are really a nice person, Have a nice time!";
}
/* Calling a PHP Function */
writeMessage();
?>
You can check presence of GET parameter in URL and call function accordingly:
if(isset($_GET['writeMessage'])){
writeMessage();
}
Of course it doesnt, especially not in that way. For me, it seems to be a horrible idea to call PHP Functions from the address bar, but if you insist:
append the function name to your url like this:
http://myWebSite.com/myFile.php?func=writeMessage
And in the PHP File:
<?php
$func = filter_var($_GET["func"]);
call_user_func($func);
?>
Try:
http://myWebSite.com/myFile.php?writeMessage=1
And:
<?php
/* Defining a PHP Function */
function writeMessage()
{
echo "You are really a nice person, Have a nice time!";
}
/* Calling a PHP Function */
if(isset($_GET['writeMessage']) && $_GET['writeMessage'] == 1){
writeMessage();
}
?>
A simple way that i would use given that you dont have too many functions is to use a if statement at the top of the php file and check the parameter of the url.
Lets say url:
http://myWebSite.com/myFile.php?writeMessage
Turn it into:
http://myWebSite.com/myFile.php?function=writeMessage
code:
<?php
$function = $_GET['function'];
if($function == "writeMessage") {
// writeMessage();
}
?>
You can do something like this:
<?php
function writeMessage() {
echo "You're a nice person";
}
$writeMessageArg = $_GET['writeMessage']; /* grabs the argument from the URL */
if (isset($writeMessageArg)) {
writeMessage();
}
?>
For a generic approach
<?php
$q = $_SERVER['QUERY_STRING'];
function TestFunc() {
echo "TestFunc() called.";
}
$q();
?>
You can call the TestFunc() by entering the following url in the address bar: