Page 1 of 1

Call a simple function from a Browser

Posted: Sat Jul 02, 2011 3:07 pm
by ANN-Tech Team.
Hi guys, I have this very simple code:

Code: Select all

<?php
    printf("John Carmack");
?>
Then, to print out the line i do this:
http://www.mydomain.com/myName.php

Now... if I have this as a function:

Code: Select all

<?php
function writeName(){
    printf("John Carmack");
}
?>

What do i have to add in the end of the link
http://www.mydomain.com/myName.php
to call the function and print out the text?

Thanks.

Re: Call a simple function from a Browser

Posted: Sat Jul 02, 2011 3:16 pm
by flying_circus
Typically, you would use the querystring.

For example: http://example.org/myName.php?name=John%20Carmack

and the php code would be something like:

Code: Select all

<?php
  # Check for variable existence
    $name = isset($_GET['name']) ? $_GET['name'] : '';
    
  # Validate $name
    if(!empty($name))
      writeName($name);
      
  # Output $name
    function writeName($name) {
      print($name);
    }
?>

Re: Call a simple function from a Browser

Posted: Sat Jul 02, 2011 5:27 pm
by McInfo
Use htmlspecialchars() or htmlentities() to thwart cross-site scripting.