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']}"); ?>
<?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 worksnailyener 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']}"); ?>
echo "Your IP is {$_SERVER['REMOTE_ADDR']}";
Well, the post is stored in mysql database, that's why.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
Yeah, so ? You don't need eval() to echo the IP either way xD (maybe I misunderstood your question ?)nailyener wrote:Well, the post is stored in mysql database, that's why.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
Then, how do I echo the IP?conradk wrote:Yeah, so ? You don't need eval() to echo the IP either way xD (maybe I misunderstood your question ?)
nailyener wrote:Then, how do I echo the IP?conradk wrote:Yeah, so ? You don't need eval() to echo the IP either way xD (maybe I misunderstood your question ?)
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.conradk wrote:echo 'Your IP is: ' . $_SERVER['REMOTE_ADDR'];Nothing more. Just copy that in your PHP file without any sort of additional function
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.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.
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.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.
<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.
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 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.
$sql = mysql_query("SELECT `user_ip` FROM `somewhere` WHERE..."); $user = mysql_fetch_assoc($sql); echo 'your ip is '. $user['ip_name'];
Sorry, here it is (post.php):conradk wrote:Could you please post the full code ?
<?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'; ?>
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.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'];
Whatismyip.com most certainly uses this: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.
echo $_SERVER['REMOTE_ADDR];Believe it or not, this is what they use. Here's a demo of what this code will do:
More or less what has already been said then...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.
<?php // some of the file echo 'Your IP is '.$_SERVER['REMOTE_ADDR']; // rest of file ?>
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 ?>
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 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
No. But you will need to add some php code. For instance: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?
<?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.
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:No. But you will need to add some php code. For instance:<?php echo $post['post_body'] . 'Your IP is: ' . $_SERVER['REMOTE_ADRR']; ?>
Why should I refuse your solutions? Your answer is not the answer to my question that's all.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.
$post_body = str_replace('##IP##', $_SERVER['REMOTE_ADDR'], $post_body);Then the IP will be in place of ##IP##
DONT! iframes are evil!nailyener wrote:"Use iframe".