ConfigureWEB Posted July 11, 2011 Share Posted July 11, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/241683-showing-visitors-ip-address/ Share on other sites More sharing options...
per1os Posted July 11, 2011 Share Posted July 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/241683-showing-visitors-ip-address/#findComment-1241324 Share on other sites More sharing options...
ConfigureWEB Posted July 12, 2011 Author Share Posted July 12, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/241683-showing-visitors-ip-address/#findComment-1241721 Share on other sites More sharing options...
PFMaBiSmAd Posted July 12, 2011 Share Posted July 12, 2011 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']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/241683-showing-visitors-ip-address/#findComment-1241723 Share on other sites More sharing options...
ConfigureWEB Posted July 12, 2011 Author Share Posted July 12, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/241683-showing-visitors-ip-address/#findComment-1241727 Share on other sites More sharing options...
PFMaBiSmAd Posted July 12, 2011 Share Posted July 12, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/241683-showing-visitors-ip-address/#findComment-1241730 Share on other sites More sharing options...
ConfigureWEB Posted July 12, 2011 Author Share Posted July 12, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/241683-showing-visitors-ip-address/#findComment-1241731 Share on other sites More sharing options...
PFMaBiSmAd Posted July 12, 2011 Share Posted July 12, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/241683-showing-visitors-ip-address/#findComment-1241735 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.