Call a simple function from a Browser

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ANN-Tech Team.
Forum Newbie
Posts: 3
Joined: Sat Jul 02, 2011 2:45 pm

Call a simple function from a Browser

Post 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.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Call a simple function from a Browser

Post 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);
    }
?>
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Call a simple function from a Browser

Post by McInfo »

Use htmlspecialchars() or htmlentities() to thwart cross-site scripting.
Post Reply