Page 1 of 1
str_replace and $_POST
Posted: Tue Dec 01, 2009 5:36 pm
by noblow91
I am trying to write in a small feature to my site that replaces certain text based on IP address. So far I have (as an example):
Code: Select all
<?php $ip=$_SERVER['REMOTE_ADDR'];
if($_POST['this'] = "text")
if($ip != '127.0.0.1')
$_POST['this'] = str_replace("text", 'othertext', $_POST['this']);
?>
I don't know why str_replace is not replacing the text inside $_POST['this']. Any help is greatly appreciated.
Re: str_replace and $_POST
Posted: Tue Dec 01, 2009 6:06 pm
by John Cartwright
if($_POST['this'] = "text") --- assignment operator
vs.
if($_POST['this'] == "text") --- comparison operator
Re: str_replace and $_POST
Posted: Tue Dec 01, 2009 6:16 pm
by noblow91
Unfortunately, "text" is remaining "text" still. Its getting a little confusing, especially since this is only my third day using php.
Re: str_replace and $_POST
Posted: Tue Dec 01, 2009 6:18 pm
by John Cartwright
Try adding
Code: Select all
$ip = $_SERVER['REMOTE_ADDR'];
echo $ip .'<br />';
What is outputted?
Re: str_replace and $_POST
Posted: Tue Dec 01, 2009 6:24 pm
by noblow91
I have something similar on my previous page:
Code: Select all
$ip = $_SERVER['REMOTE_ADDR'];
print($ip);
The output is 192.168.1.7, which is my IP when I'm not using my switch. I'm wanting to have it set up for 192.168.2.2 (my IP through the switch). I have it in place of 127.0.0.1 as of now.
Re: str_replace and $_POST
Posted: Tue Dec 01, 2009 6:28 pm
by goatrider
Is there any reason you
need to use str_replace? Why not just try doing a simple conditional like this...
Code: Select all
<?php
$ip = $_SERVER['REMOTE_ADDR'];
if($ip == '127.0.0.1'){
$text = "text";
}
else{
$text = "othertext";
}
?>
Re: str_replace and $_POST
Posted: Tue Dec 01, 2009 6:32 pm
by noblow91
I don't have a user database on my site yet, so I was wanting to use str_replace to pretty much tie everybody's IPs to a name so nobody else would be able to use somebody else's name without using that person's internet.
Re: str_replace and $_POST
Posted: Tue Dec 01, 2009 6:44 pm
by goatrider
If you don't have that many users, you can try using a switch on $ip as follows:
Code: Select all
<?php
$ip = $_SERVER['REMOTE_ADDR'];
switch ($ip) {
case "127.0.0.1":
$text = "Bob";
break;
case "10.1.1.1":
$text = "Joe";
break;
case "10.2.2.2":
$text = "Sally";
break;
default:
$text = "Unknown user";
}
echo "$text";
?>
The only problem with this is you'll have to add a new case for each user, which can get monotonous if you've got more than say, 10.
Re: str_replace and $_POST
Posted: Tue Dec 01, 2009 6:56 pm
by noblow91
What I'm wanting to do is make it so that if user 1 tries to post something under user 2's name without it matching user 2's IP then it replaces that name with something else or appends something to the end of it so other people know that it wasn't really user 2 posting it.
Re: str_replace and $_POST
Posted: Tue Dec 01, 2009 7:07 pm
by goatrider
In that case, just run the switch before the post, get the user's REAL name (by comparing it to the IP address) and then append this to the end of the user's posted message:
Code: Select all
echo "This message was written and posted by $text";
Re: str_replace and $_POST
Posted: Tue Dec 01, 2009 9:59 pm
by noblow91
I was checking the emails I get upon receiving comments on my site and it turns out that the code that I am using does work. I went to review it once again and finally realized that it replaced the name after it had already been saved to the database, but before it was sent via email.
I have learned , today, that I need to make sure the order is correct otherwise it wont work.