str_replace and $_POST

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
noblow91
Forum Newbie
Posts: 6
Joined: Tue Dec 01, 2009 5:31 pm
Location: Washington
Contact:

str_replace and $_POST

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: str_replace and $_POST

Post by John Cartwright »

if($_POST['this'] = "text") --- assignment operator

vs.

if($_POST['this'] == "text") --- comparison operator
User avatar
noblow91
Forum Newbie
Posts: 6
Joined: Tue Dec 01, 2009 5:31 pm
Location: Washington
Contact:

Re: str_replace and $_POST

Post by noblow91 »

Unfortunately, "text" is remaining "text" still. Its getting a little confusing, especially since this is only my third day using php.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: str_replace and $_POST

Post by John Cartwright »

Try adding

Code: Select all

$ip = $_SERVER['REMOTE_ADDR'];
 
echo $ip .'<br />';
 
What is outputted?
User avatar
noblow91
Forum Newbie
Posts: 6
Joined: Tue Dec 01, 2009 5:31 pm
Location: Washington
Contact:

Re: str_replace and $_POST

Post 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.
User avatar
goatrider
Forum Newbie
Posts: 4
Joined: Tue Dec 01, 2009 6:20 pm

Re: str_replace and $_POST

Post 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";
}
?> 
User avatar
noblow91
Forum Newbie
Posts: 6
Joined: Tue Dec 01, 2009 5:31 pm
Location: Washington
Contact:

Re: str_replace and $_POST

Post 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.
User avatar
goatrider
Forum Newbie
Posts: 4
Joined: Tue Dec 01, 2009 6:20 pm

Re: str_replace and $_POST

Post 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.
User avatar
noblow91
Forum Newbie
Posts: 6
Joined: Tue Dec 01, 2009 5:31 pm
Location: Washington
Contact:

Re: str_replace and $_POST

Post 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.
User avatar
goatrider
Forum Newbie
Posts: 4
Joined: Tue Dec 01, 2009 6:20 pm

Re: str_replace and $_POST

Post 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";
User avatar
noblow91
Forum Newbie
Posts: 6
Joined: Tue Dec 01, 2009 5:31 pm
Location: Washington
Contact:

Re: str_replace and $_POST

Post 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.
Post Reply