Welcome to WebmasterWorld Guest from 107.20.5.156

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Obfuscate data in PHP from SQL database

hide two octets of ip in php from sql db

     

Seedy

12:51 am on Mar 5, 2012 (gmt 0)



I'm displaying a table of IP addresses from an SQL database but wish to obfuscate the last two octets. I want to show the first two octets to confirm an IP address has been recorded but not fully reveal the users private data.

Does anyone know a way this can be done please?

Thank you in advance.

incrediBILL

2:56 am on Mar 5, 2012 (gmt 0)

WebmasterWorld Administrator incredibill is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Easier done than said:

$ip="127.0.0.1";
$a=explode(".",$ip);
$newip="$a[0].$a[1].***.***";
echo "Obscured IP = $newip";


The output would be: "Obscured IP = 127.0.***.***"

Seedy

11:35 am on Mar 5, 2012 (gmt 0)



Thanks very much Bill.

Having trouble incorporation that into my PHP.

Tried creating a function using your code but can't get it to insert the obscured IP.

 <?php
$i=0;
while ($i < $num) {
$f1=mysql_result($result,$i,"name");
$f2=mysql_result($result,$i,"score");
$f3=mysql_result($result,$i,"ip");
?>

<table>
<tr>
<td><?php echo ucfirst($f1); ?></td>
<td><?php echo $f2; ?></td>
<td><?php echo $f3; ?></td>
</tr>
</table>


What would you suggest please?
Thank you

Seedy

12:11 pm on Mar 5, 2012 (gmt 0)



Easier done than said:

hide two octets of ip in php from sql db

:-D

Got it:

 <?php
$i=0;
while ($i < $num) {
$f1=mysql_result($result,$i,"name");
$f2=mysql_result($result,$i,"score");
$f3=mysql_result($result,$i,"ip");
$ip=$f3;
$a=explode(".",$ip);
$newip="$a[0].$a[1].***.***";
?>

<table>
<tr>
<td><?php echo ucfirst($f1); ?></td>
<td><?php echo $f2; ?></td>
<td><?php echo $newip; ?></td>
</tr>
</table>


This works fine, does all seem ok to you Bill?
Many thanks

incrediBILL

7:01 pm on Mar 5, 2012 (gmt 0)

WebmasterWorld Administrator incredibill is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Looks good to me, especially if it works for you!

The reason it didn't work in a function is you lost the $a return values.

This would work as a function:
function obscure_ip($ip) {
$a=explode(".",$ip);
return($a[0] . '.' . $a[1] . '.***.***');
}

echo 'Obscured IP = ' . obscure_ip("127.0.0.1");

Seedy

7:20 pm on Mar 5, 2012 (gmt 0)



Many thanks Bill.

Seedy

7:30 pm on Mar 5, 2012 (gmt 0)



In the same page, I have a lot of repeat PHP code which I'm sure could be condensed. Perhaps with only one mysql_connect section? I've given it a go a number of times using includes() but cannot get it to work.

The basic code is below and the only values that vary are the (1) username, (2) database name, (3) table name and the URL/name in the (4) html table (which can remain repeated).

How could I condense the code so that it's not being repeated (seemingly) unnecessarily.

Many thanks in advance.

 <?php
require_once('/home/user/php/db.php');
mysql_connect($hostname,$username1,$password);
@mysql_select_db($database1) or die( "Unable to select database");
$query = ("SELECT name, score, ip FROM $table1 ORDER BY score DESC LIMIT 0, $display_max") or error_msg('Could not retrieve scores.');
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
?>
<table>
<tr>
<th><a href="link1.php">Link1</a></th>
</tr>
<tr>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
</table>
<?php
$i=0;
while ($i < $num) {
$f1=mysql_result($result,$i,"A");
$f2=mysql_result($result,$i,"B");
$f3=mysql_result($result,$i,"C");
$ip=$f3;
$a=explode(".",$ip);
$newip="$a[0].$a[1].*.*";
?>
<table>
<tr>
<td><?php echo ucfirst($f1); ?></td>
<td><?php echo $f2; ?></td>
<td><?php echo $newip; ?></td>
</tr>
</table>
<?php
$i++;
}
?>
 

Featured Threads

Hot Threads This Week

Hot Threads This Month