snipt

Ctrl+h for KB shortcuts

PHP

Detect TOR requests

<?php

print isTorRequest() ? "Yep" : "Nope";

function isTorRequest() {
  $reverse_client_ip = implode('.', array_reverse(explode('.', $_SERVER['REMOTE_ADDR'])));
  $reverse_server_ip = implode('.', array_reverse(explode('.', $_SERVER['SERVER_ADDR'])));
  $hostname = $reverse_client_ip . "." . $_SERVER['SERVER_PORT'] . "." . $reverse_server_ip . ".ip-port.exitlist.torproject.org";
  return gethostbyname($hostname) == "127.0.0.2";
} 

?> 
https://snipt.net/embed/9213dfe7e8af68ef167edc4033764049/
/raw/9213dfe7e8af68ef167edc4033764049/
9213dfe7e8af68ef167edc4033764049
php
PHP
13
2019-07-18T06:47:43
True
False
False
/api/public/snipt/34366/
detect-tor-requests
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1"> 1</a> <a href="#L-2"> 2</a> <a href="#L-3"> 3</a> <a href="#L-4"> 4</a> <a href="#L-5"> 5</a> <a href="#L-6"> 6</a> <a href="#L-7"> 7</a> <a href="#L-8"> 8</a> <a href="#L-9"> 9</a> <a href="#L-10">10</a> <a href="#L-11">11</a> <a href="#L-12">12</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a><span class="k">print</span> <span class="nx">isTorRequest</span><span class="p">()</span> <span class="o">?</span> <span class="s2">&quot;Yep&quot;</span> <span class="o">:</span> <span class="s2">&quot;Nope&quot;</span><span class="p">;</span> </span><span id="L-4"><a name="L-4"></a> </span><span id="L-5"><a name="L-5"></a><span class="k">function</span> <span class="nf">isTorRequest</span><span class="p">()</span> <span class="p">{</span> </span><span id="L-6"><a name="L-6"></a> <span class="nv">$reverse_client_ip</span> <span class="o">=</span> <span class="nb">implode</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">,</span> <span class="nb">array_reverse</span><span class="p">(</span><span class="nb">explode</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">,</span> <span class="nv">$_SERVER</span><span class="p">[</span><span class="s1">&#39;REMOTE_ADDR&#39;</span><span class="p">])));</span> </span><span id="L-7"><a name="L-7"></a> <span class="nv">$reverse_server_ip</span> <span class="o">=</span> <span class="nb">implode</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">,</span> <span class="nb">array_reverse</span><span class="p">(</span><span class="nb">explode</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">,</span> <span class="nv">$_SERVER</span><span class="p">[</span><span class="s1">&#39;SERVER_ADDR&#39;</span><span class="p">])));</span> </span><span id="L-8"><a name="L-8"></a> <span class="nv">$hostname</span> <span class="o">=</span> <span class="nv">$reverse_client_ip</span> <span class="o">.</span> <span class="s2">&quot;.&quot;</span> <span class="o">.</span> <span class="nv">$_SERVER</span><span class="p">[</span><span class="s1">&#39;SERVER_PORT&#39;</span><span class="p">]</span> <span class="o">.</span> <span class="s2">&quot;.&quot;</span> <span class="o">.</span> <span class="nv">$reverse_server_ip</span> <span class="o">.</span> <span class="s2">&quot;.ip-port.exitlist.torproject.org&quot;</span><span class="p">;</span> </span><span id="L-9"><a name="L-9"></a> <span class="k">return</span> <span class="nb">gethostbyname</span><span class="p">(</span><span class="nv">$hostname</span><span class="p">)</span> <span class="o">==</span> <span class="s2">&quot;127.0.0.2&quot;</span><span class="p">;</span> </span><span id="L-10"><a name="L-10"></a><span class="p">}</span> </span><span id="L-11"><a name="L-11"></a> </span><span id="L-12"><a name="L-12"></a><span class="cp">?&gt;</span><span class="x"> </span> </span></pre></div> </td></tr></table>
php, tor