Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to call a php function from address bar?

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();
  ?>
like image 616
warzone_fz Avatar asked Feb 06 '26 01:02

warzone_fz


2 Answers

You can check presence of GET parameter in URL and call function accordingly:

if(isset($_GET['writeMessage'])){
   writeMessage();
}
like image 193
Oskars Pakers Avatar answered Feb 07 '26 16:02

Oskars Pakers


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();
  }
?>
like image 38
user2873528 Avatar answered Feb 07 '26 15:02

user2873528