Jump to content

Showing Visitor's IP Address


ConfigureWEB

Recommended Posts

Hi,

 

I have a blog and I store posts in a MySQL database. I want to show the IP address of the visitor on one single post. If I didn't store posts in the database and had separate files for each post, the following code would do the job:

 

<?php echo 'Your IP is: ' . $_SERVER['REMOTE_ADRR']; ?>

 

But since the post is stored in the database, I guess I need to use something else. I heard about the eval() function and tried the following code but it didn't work:

 

<?php eval( '?><?php echo $_SERVER['REMOTE_ADDR']; ?><?' ); ?>

 

Do you have any idea how can I show the IP?

Link to comment
Share on other sites

Your better bet would be to use a place holder and then do a str_replace on the text. As I highly discourage the user of eval.

 

$text = 'Something Your IP Is: %userip% something else';
$text = str_replace('%userip%', $_SERVER['REMOTE_ADDR'], $text);
echo $text;

 

Should do the trick.

Link to comment
Share on other sites

Your better bet would be to use a place holder and then do a str_replace on the text. As I highly discourage the user of eval.

 

$text = 'Something Your IP Is: %userip% something else';
$text = str_replace('%userip%', $_SERVER['REMOTE_ADDR'], $text);
echo $text;

 

Should do the trick.

 

So, how can I add your code into the post which is stored in the database?

Link to comment
Share on other sites

You are making this harder than it is.

 

You would store the value that is in $_SERVER['REMOTE_ADDR'] with the saved blog post record in the database, say in a column named ip.

 

To display the IP address when you display the blog post, you would use the following php code, assuming that $row is the associative array of data that was fetched from the database table -

 

<?php echo 'Your IP is: ' . $row['ip']; ?>

Link to comment
Share on other sites

You are making this harder than it is.

 

You would store the value that is in $_SERVER['REMOTE_ADDR'] with the saved blog post record in the database, say in a column named ip.

 

To display the IP address when you display the blog post, you would use the following php code, assuming that $row is the associative array of data that was fetched from the database table -

 

<?php echo 'Your IP is: ' . $row['ip']; ?>

 

I don't understand why I need to store IP address in the database. I have a single post about "how to find your IP address" and in that post I want to show the IP address of the visitor. Here is an example: http://www.wallpaperama.com/forums/how-to-display-ip-address-php-script-code-function-from-visitor-user-t399.html

Link to comment
Share on other sites

I'm guessing you want a generic add on solution that doesn't require modifying your blog software?  (the link you posted is for creating a web page that displays the visitor's ip address, not for a way of directly embedding the ability to display the visitor's ip address into a blog post.)

 

To generically do this, you would either use an iframe or dynamically produce an image that displays the ip address. You would then insert either an <iframe src="URL"></iframe> tag or an <img src="URL" alt=""> tag into the blog post using bbcode tags, since most blog software would not permit direct insertion of html tags.

 

For the iframe method, the URL would be to a .php script that does exactly what the link you posted is doing. For the image method, the URL would be to a .php script that dynamically produces and outputs an image using the GD image functions that writes the IP address onto the image.

Link to comment
Share on other sites

I'm guessing you want a generic add on solution that doesn't require modifying your blog software?  (the link you posted is for creating a web page that displays the visitor's ip address, not for a way of directly embedding the ability to display the visitor's ip address into a blog post.)

 

To generically do this, you would either use an iframe or dynamically produce an image that displays the ip address. You would then insert either an <iframe src="URL"></iframe> tag or an <img src="URL" alt=""> tag into the blog post using bbcode tags, since most blog software would not permit direct insertion of html tags.

 

For the iframe method, the URL would be to a .php script that does exactly what the link you posted is doing. For the image method, the URL would be to a .php script that dynamically produces and outputs an image using the GD image functions that writes the IP address onto the image.

 

I never thought about iframes, I guess that was the answer I was looking for. I tried it and it works just as I wanted. Thank you very much.

 

By the way, I don't use a blog software, it is a plain simple PHP-MySQL blog that I created from scratch and it allows direct insertion of html.

Link to comment
Share on other sites

If the blog software is your own and you are willing to modify the php code or if you already have bbcode tags in use or your own tag/placeholder system in it, you would use the method that per1os posted.

 

The %userip% is just some made up unique placeholder/tag that would be put into the text of a blog post at the point where you want to display the visitor's ip address.

 

The str_replace() statement would be something that you add to your blog software at some point after you retrieve the content from the database and before you output the content of the blog post to the browser.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.