Don't to this! It's evil! I'm showing you how, so you'll know what not to do.
You can try this program. It let's you send a message to your boss. Type the message, and hit the button:
The program sends the message (it doesn't, but let's pretend), and confirms what it has done.
Sometimes, the program will change every n in the message, to an m:
Oh, so you mever make mistakes? Hmmmm...
Here's the code:
- <?php
- // Don't do this! It's evil!
- $message = '';
- if (isset($_POST['message'])) {
- $message = $_POST['message'];
- }
- // Do something evil?
- if ($message != '') {
- if (rand(1, 3) == 3) {
- // Yes! Evil it is.
- // Replace ns with ms.
- $message = str_replace('n', 'm', $message);
- }
- }
- ?><!doctype html>
- ...
- <p>Send a message to your boss.</p>
- <?php
- if ($message != '') {
- print "<p>Message sent: $message</p>\n";
- }
- ?>
- <form method="post">
- <p>
- <label>Message<br>
- <textarea name="message" cols="40" rows="8"></textarea>
- </label>
- <br><span class="help-text">Please type your message.</span>
- </p>
- <p>
- <button type="submit">Send</button>
- </p>
- </form>
This is one of those pages that sends data to itself. There's no action
on the form (line 23):
- <form method="post">
When the program starts, it grabs the POST param message
, if there is one:
- $message = '';
- if (isset($_POST['message'])) {
- $message = $_POST['message'];
- }
OK. Now, if there is a message, we might do something evil with it.
- // Do something evil?
- if ($message != '') {
- if (rand(1, 3) == 3) {
- // Yes! Evil it is.
- // Replace ns with ms.
- $message = str_replace('n', 'm', $message);
- }
- }
rand(1, 3)
chooses a random whole number, between 1 and 3. If the random number chosen is 3 (which it will be one-third of the time), the program does something evil:
- // Replace ns with ms.
- $message = str_replace('n', 'm', $message);
Here's what the official str_replace() docs say:
- - CUT SCREEN HERE - -
str_replace
str_replace
— Replace all occurrences of the search string with the replacement string
Description
str_replace ( array|string $search , array|string $replace , string|array $subject , int &$count = null ) : string|array
This function returns a string or an array with all occurrences of search
in subject
replaced with the given replace
value.
- - CUT SCREEN HERE - -
So, one-third of the time, all ns are replaced by ms.
Often, the user won't notice. They know what they typed, and expect to see that. Sometimes, the boss won't notice it either. But sometimes...
There are lot of variants of this prank. For example:
$message = str_replace('boss', 'idiot', $message);
Imagine this applied to the message "Hey, boss! Nice meeting today!"
Remember, this is evil! Don't do it!