homepage Welcome to WebmasterWorld Guest from 54.167.10.244
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Obfuscate data in PHP from SQL database
hide two octets of ip in php from sql db
Seedy




msg:4424854
 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




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

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




msg:4425002
 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




msg:4425011
 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




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

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




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

Many thanks Bill.

Seedy




msg:4425199
 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++;
}
?>

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved