Page 1 of 2
Showing IP Address of the visitor
Posted: Mon Jul 11, 2011 7:46 am
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']}"); ?>
Re: Showing IP Address of the visitor
Posted: Mon Jul 11, 2011 8:51 am
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']}";
Re: Showing IP Address of the visitor
Posted: Mon Jul 11, 2011 8:54 am
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.
Re: Showing IP Address of the visitor
Posted: Mon Jul 11, 2011 9:14 am
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 ?)
Re: Showing IP Address of the visitor
Posted: Mon Jul 11, 2011 9:28 am
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?
Re: Showing IP Address of the visitor
Posted: Mon Jul 11, 2011 10:12 am
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

Re: Showing IP Address of the visitor
Posted: Mon Jul 11, 2011 10:21 am
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.
Re: Showing IP Address of the visitor
Posted: Mon Jul 11, 2011 10:30 am
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.
Re: Showing IP Address of the visitor
Posted: Mon Jul 11, 2011 10:42 am
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.
Re: Showing IP Address of the visitor
Posted: Mon Jul 11, 2011 11:19 am
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.
Re: Showing IP Address of the visitor
Posted: Mon Jul 11, 2011 11:24 am
by conradk
Could you please post the full code ?
Re: Showing IP Address of the visitor
Posted: Mon Jul 11, 2011 11:56 am
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'];
Re: Showing IP Address of the visitor
Posted: Mon Jul 11, 2011 12:06 pm
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.
Re: Showing IP Address of the visitor
Posted: Mon Jul 11, 2011 12:21 pm
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
Re: Showing IP Address of the visitor
Posted: Mon Jul 11, 2011 12:22 pm
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
?>
Re: Showing IP Address of the visitor
Posted: Mon Jul 11, 2011 12:36 pm
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?
Re: Showing IP Address of the visitor
Posted: Mon Jul 11, 2011 12:46 pm
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.
Re: Showing IP Address of the visitor
Posted: Mon Jul 11, 2011 1:27 pm
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.
Re: Showing IP Address of the visitor
Posted: Mon Jul 11, 2011 1:28 pm
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##
Re: Showing IP Address of the visitor
Posted: Tue Jul 12, 2011 10:34 am
by nyo
Ok, I found the answer on another forum. "Use iframe".
Thank you all for your ideas.
Re: Showing IP Address of the visitor
Posted: Tue Jul 12, 2011 2:12 pm
by Kamal
nailyener wrote:"Use iframe".
DONT! iframes are evil!