PHP MySQL Query IP As ****

I have a form my users fill out that write to a MySQL DB. It also captures their IP and places it in a field called IP.

When I query this information into an html page how can I display the results as ****.****.****.XXXX
I only want the last octect of the address to display on the page, but I want the MySQL to record the actual IP address.

Is there a way I can make this happen?
mimoserAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
Try this.  Replace the dummy names with your real variables.
$display_string = "****.****.****.".substr(IPstring, -4, 4);

Open in new window

0
mimoserAuthor Commented:
How would I add that in relation to

$sql = "SELECT *, DATE_FORMAT(`date`,'%a, %b %d %Y %h:%i %p') AS showdate FROM Request";
$result = mysql_query($sql);

<font color="lime"><?php  echo $row['IP']; ?></font>

Sorry, I'm still a newbi to this lol
0
Ultra_MasterCommented:
Hi,

There is a simple way to do that:

<input type="password" name="1st_byte" value="<?php echo $1st_octet; ?>">
<input type="password" name="2nd_byte" value="<?php echo $2nd__octet; ?>">
<input type="password" name="3rd_byte" value="<?php echo $3rd_octet; ?>">
<input type="text" name="last_byte" value="<?php echo $last_octet; ?>">

OR you can simply add only an input text

<input type="text" name="IP" value="****.****.****.<?php echo $1st_octet; ?>">

Good  luck,
Ultra_Master
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Ultra_MasterCommented:
$1st_octet in the last input is actually the last octet.
0
mimoserAuthor Commented:
@Ultra Master
I already have the form that works great adding the full IP to a DB feild called 'IP'

I'm trying to now show it on an html page like
<font color="lime"><?php  echo $row['IP']; ?></font>

Which works and shows all 4 octet's, but I was trying to make it only show the last octet.
0
Shinesh PremrajanEngineering ManagerCommented:
<font color="lime"><?php  echo substr($row['IP'],strrpos($row['IP'],"."),strlen($row['IP'])); ?></font>


Hope this helps
0
mimoserAuthor Commented:
@ shinuq
This is Working!!
This is very, very close, and I may end up taking it unless there is a way to make it say something like "x" or ** out the other characters leaving the last octet? such as
xxx.xxx.xxx.255  or  ***.***.***.255  or something

If not, thats ok, this comes the closest
0
Shinesh PremrajanEngineering ManagerCommented:
I think you must be looking at this.

<font color="lime"><?php  echo preg_replace("/\d/","X",substr($row['IP'],0,strrpos($row['IP'],"."))).".".substr($row['IP'],strrpos($row['IP'],"."),strlen($row['IP']));?></font>


cheers
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mimoserAuthor Commented:
Perfect!! Can you please explain this for me?
0
mimoserAuthor Commented:
This works great, can you please explain it a little more to me about whas going on?
0
Shinesh PremrajanEngineering ManagerCommented:

1) replace the occurrence of the any decimal into a X character using the preg_replace function.
2) as we need to do the replacement of the first three octets we will be extracting the first three octets using the sub string  and the strrpos reverse traversal function.

preg_replace("/\d","X",substr($row['IP'],0,strrpos($row['IP'],".")))


3) get the fourth octet from the string using the substr and the strrpos functionality.

substr($row['IP'],strrpos($row['IP'],"."),strlen($row['IP']));

Hope this clarifies
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.