loading...

Trolling Twitter followers with .htaccess & PHP

adamgreenough profile image Adam Greenough Originally published at webwide.io ・1 min read

Thought this was pretty entertaining! David Sandberg trolls his Twitter followers by using .htaccess to spoof an image file to return a PHP script that programmatically returns an image based on the visitor IP address.

Here's the code from the Tweet for anybody wanting to try anything similar:

.htaccess

RewriteEngine on
Redirect /whatisthis.jpg /whatisthis.php

whatisthis.php

<?php
function getIMG() {
    // Get last digit of IP address
    $numb = substr($_SERVER['REMOTE_ADDR'], -1);

    // Return a different image based on IP
    if($numb == 1 || $numb == 2) {
        header("Location: 1.jpg");
    } elseif($numb == 3 || $numb == 4) {
        header("Location: 2.jpg");
    } elseif($numb == 5 || $numb == 6) {
        header("Location: 3.jpg");
    } elseif($numb == 7 || $numb == 8) {
        header("Location: 4.jpg");
    } elseif($numb == 9 || $numb == 0) {
        header("Location: 5.jpg");
    }
}

getIMG();
?>

Posted on by:

adamgreenough profile

Adam Greenough

@adamgreenough

Freelance digital designer & web developer from the United Kingdom with over 10 years of experience. Admin at the webwide.io forums. Curates all the best web design & development tools at hoard.fyi.

Discussion

pic
Editor guide
 

For a better joke, don’t rederict them.

Use something like

header(“content-type: image/jpeg”);
if(...) {
echo file_get_contents(“imagename.jpg”);
}

Then the output is different per IP range, but they will not know/see it.

P.s. typed on mobile so indentation and example are not perfect.