Showing IP Address of the visitor

Ask about a PHP problem here.
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Showing IP Address of the visitor

Post by nyo »

Hi,

I am trying to show the IP address of the visitor on one post. The posts are stored in MySQL database. I have the following code and it seems not to work:
<?php eval(echo "Your IP is {$_SERVER['REMOTE_ADDR']}"); ?>
conradk
Posts: 117
Joined: Tue Jul 05, 2011 10:41 pm

Re: Showing IP Address of the visitor

Post by conradk »

nailyener wrote:Hi,

I am trying to show the IP address of the visitor on one post. The posts are stored in MySQL database. I have the following code and it seems not to work:
<?php eval(echo "Your IP is {$_SERVER['REMOTE_ADDR']}"); ?>
Why use eval() ? I don't really have much experience with the eval function. However, just removing it from your code above works :)

just:
echo "Your IP is {$_SERVER['REMOTE_ADDR']}";
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Re: Showing IP Address of the visitor

Post by nyo »

conradk wrote:Why use eval() ? I don't really have much experience with the eval function. However, just removing it from your code above works :)
Well, the post is stored in mysql database, that's why.
conradk
Posts: 117
Joined: Tue Jul 05, 2011 10:41 pm

Re: Showing IP Address of the visitor

Post by conradk »

nailyener wrote:
conradk wrote:Why use eval() ? I don't really have much experience with the eval function. However, just removing it from your code above works :)
Well, the post is stored in mysql database, that's why.
Yeah, so ? You don't need eval() to echo the IP either way xD (maybe I misunderstood your question ?)
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Re: Showing IP Address of the visitor

Post by nyo »

conradk wrote:Yeah, so ? You don't need eval() to echo the IP either way xD (maybe I misunderstood your question ?)
Then, how do I echo the IP?
conradk
Posts: 117
Joined: Tue Jul 05, 2011 10:41 pm

Re: Showing IP Address of the visitor

Post by conradk »

nailyener wrote:
conradk wrote:Yeah, so ? You don't need eval() to echo the IP either way xD (maybe I misunderstood your question ?)
Then, how do I echo the IP?
echo 'Your IP is: ' . $_SERVER['REMOTE_ADDR'];
Nothing more. Just copy that in your PHP file without any sort of additional function :)
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Re: Showing IP Address of the visitor

Post by nyo »

conradk wrote:
echo 'Your IP is: ' . $_SERVER['REMOTE_ADDR'];
Nothing more. Just copy that in your PHP file without any sort of additional function :)
Thank you, I know you are trying to help but if you've read the post carefully we wouldn't have wasted our time here. The post is stored in the database and MySQL doesn't evaluate PHP code that is stored in a field. I am trying the eval() function because simply echoing it doesn't work.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Showing IP Address of the visitor

Post by jacek »

Can you post the full code ?

Also, if you have to resort to using eval() then it's probably time to rethink the design of your site.
Image
conradk
Posts: 117
Joined: Tue Jul 05, 2011 10:41 pm

Re: Showing IP Address of the visitor

Post by conradk »

nailyener wrote: Thank you, I know you are trying to help but if you've read the post carefully we wouldn't have wasted our time here. The post is stored in the database and MySQL doesn't evaluate PHP code that is stored in a field. I am trying the eval() function because simply echoing it doesn't work.
I'm not wasting any time. There is no need to store the IP inside of a post. You can store the IP in a seperate table, fetch it and echo out something like $row['user_ip']. This way you don't need eval() and your database will probably be better organised. But as Jacek said, maybe posting the whole code here would help.

Regards,
CK

PS: It would probably not hurt to be a little more polite when you are in trouble because of using the wrong methods, or at least what seems to be wrong.
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Re: Showing IP Address of the visitor

Post by nyo »

jacek wrote:Can you post the full code ?

Also, if you have to resort to using eval() then it's probably time to rethink the design of your site.
Honestly, I don't know if I have to resort to eval() or not. I am just trying to learn how to show the user IP. I have modified the design of the site a couple of times and I am sure I will modify it many times more because I am just learning this stuff.

Here is the codes that I tried in the post_body field:
<p>Your IP Address is: <?php echo "Your IP is {$_SERVER['REMOTE_ADDR']}"; ?></p>
and
<p>Your IP Address is: <?php eval(echo "Your IP is {$_SERVER['REMOTE_ADDR']}"); ?></p>
and neither worked.
conradk wrote:I'm not wasting any time. There is no need to store the IP inside of a post. You can store the IP in a seperate table, fetch it and echo out something like $row['user_ip']. This way you don't need eval() and your database will probably be better organised. But as Jacek said, maybe posting the whole code here would help.

Regards,
CK

PS: It would probably not hurt to be a little more polite when you are in trouble because of using the wrong methods, or at least what seems to be wrong.
CK, I really didn't mean to be rude. I really appreciate your help. Ok, let's say I stored IP in another table, if I want to fetch it and show on a single post, I will need to use PHP code in my post body again which brings us to the same point.
conradk
Posts: 117
Joined: Tue Jul 05, 2011 10:41 pm

Re: Showing IP Address of the visitor

Post by conradk »

Could you please post the full code ?
Dominion
Posts: 32
Joined: Thu May 05, 2011 11:32 pm

Re: Showing IP Address of the visitor

Post by Dominion »

Sorry, but why do you need eval to show something like an ip from the database? Just grab it with a query, and show it.
$sql = mysql_query("SELECT `user_ip` FROM `somewhere` WHERE...");
$user = mysql_fetch_assoc($sql);
echo 'your ip is '. $user['ip_name'];
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Re: Showing IP Address of the visitor

Post by nyo »

conradk wrote:Could you please post the full code ?
Sorry, here it is (post.php):
<?php
include 'inc/header.php';
$post = get_post($_GET['p']);
?>
<div id="main">
	<div id="content">
	<h1><a href="<?php echo $post['post_name']; ?>"><?php echo $post['post_title']; ?></a></h1>
	<p>Posted by <strong><?php echo $post['post_author']; ?></strong> on <?php echo date('j M, Y h:i', strtotime($post['post_date'])); ?>
	   in <strong><a href="category.php?p=<?php echo $post['cat_name']; ?>"><?php echo $post['cat_title']; ?></a></strong></p>
	<div><?php echo $post['post_body']; ?></div>
	</div>
</div>
<div id="sidebar"><?php include 'inc/sidebar.php'; ?></div>
<?php include 'inc/footer.php'; ?>
Dominion wrote:Sorry, but why do you need eval to show something like an ip from the database? Just grab it with a query, and show it.
$sql = mysql_query("SELECT `user_ip` FROM `somewhere` WHERE...");
$user = mysql_fetch_assoc($sql);
echo 'your ip is '. $user['ip_name'];
I don't know if I need eval or not, it was what I found on the web. I am not storing IP's in the database. This is the only post I will ever show IP of the visitor. Think about sites like whatismyip.com etc.
conradk
Posts: 117
Joined: Tue Jul 05, 2011 10:41 pm

Re: Showing IP Address of the visitor

Post by conradk »

nailyener wrote: I don't know if I need eval or not, it was what I found on the web. I am not storing IP's in the database. This is the only post I will ever show IP of the visitor. Think about sites like whatismyip.com etc.
Whatismyip.com most certainly uses this:
echo $_SERVER['REMOTE_ADDR];
Believe it or not, this is what they use. Here's a demo of what this code will do:
http://conradk.com/ip.php
Dominion
Posts: 32
Joined: Thu May 05, 2011 11:32 pm

Re: Showing IP Address of the visitor

Post by Dominion »

nailyener wrote: I don't know if I need eval or not, it was what I found on the web. I am not storing IP's in the database. This is the only post I will ever show IP of the visitor. Think about sites like whatismyip.com etc.
More or less what has already been said then...
<?php 
// some of the file
echo 'Your IP is '.$_SERVER['REMOTE_ADDR']; 
// rest of file
?>
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Re: Showing IP Address of the visitor

Post by nyo »

Dominion wrote:More or less what has already been said then...
<?php 
// some of the file
echo 'Your IP is '.$_SERVER['REMOTE_ADDR']; 
// rest of file
?>
conradk wrote:Whatismyip.com most certainly uses this:
echo $_SERVER['REMOTE_ADDR];
Believe it or not, this is what they use. Here's a demo of what this code will do:
http://conradk.com/ip.php
I know it works if you have a separate page such as ip.php. So, are you saying that I will need to create a separate file whenever I need to have some PHP script in the post body?
conradk
Posts: 117
Joined: Tue Jul 05, 2011 10:41 pm

Re: Showing IP Address of the visitor

Post by conradk »

nailyener wrote:
I know it works if you have a separate page such as ip.php. So, are you saying that I will need to create a separate file whenever I need to have some PHP script in the post body?
No. But you will need to add some php code. For instance:
<?php echo $post['post_body'] . 'Your IP is: ' . $_SERVER['REMOTE_ADRR']; ?>
I get the feeling you are not really trying to find a solution. Or maybe you're refusing the solutions we give you, even though they might work just fine. I'm done here.
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Re: Showing IP Address of the visitor

Post by nyo »

conradk wrote:No. But you will need to add some php code. For instance:
<?php echo $post['post_body'] . 'Your IP is: ' . $_SERVER['REMOTE_ADRR']; ?>
Certainly I need to add some php code, but up to now I couldn't find an answer to how to add that php code into a post that is stored in the database.
conradk wrote:I get the feeling you are not really trying to find a solution. Or maybe you're refusing the solutions we give you, even though they might work just fine. I'm done here.
Why should I refuse your solutions? Your answer is not the answer to my question that's all.

This guy explains this on his site here, but I couldn't make it work. That's why I asked about eval() function here.
User avatar
Kamal
Posts: 123
Joined: Fri May 06, 2011 10:45 am
Contact:

Re: Showing IP Address of the visitor

Post by Kamal »

Well what I would do is instead of the PHP in there, I put ##IP## or something like that, then do:
$post_body = str_replace('##IP##', $_SERVER['REMOTE_ADDR'], $post_body);
Then the IP will be in place of ##IP##
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Re: Showing IP Address of the visitor

Post by nyo »

Ok, I found the answer on another forum. "Use iframe".

Thank you all for your ideas.
User avatar
Kamal
Posts: 123
Joined: Fri May 06, 2011 10:45 am
Contact:

Re: Showing IP Address of the visitor

Post by Kamal »

nailyener wrote:"Use iframe".
DONT! iframes are evil!
Post Reply