How to filter bad words?

You can use this simple function to filter bad words.

Please see attachment for mysql table of frequently used 458 bad words. http://www.phpmind.com/codedemo/php-bad-word-filter/bad_word.txt.zip
You can add more word easily as you know more!!


  function bad_wordcensor($txt)
  {
    $q = mysql_query("SELECT bad_word, replacement FROM bad_words");
    while ($row_bad = mysql_fetch_array($q))
    {
      $txt = str_ireplace($row_bad['bad_word'], $row_bad['replacement'], $txt);
    }

  return $txt;
  }

echo bad_wordcensor($_POST["comments"]);
Share

How to close colorbox when clicking on a link?

Colorbox is very cool js plug-in which is used by many developers.

I was creating a facebook app and had situation where i wanted to close Colorbox/popup and open new page which was not suppose to open in Colorbox.

I was searching and found really simple code which can be used in several ways with any php technology stuff.

PHPMIND.COM This is good for normal site

PHPMIND.COM This works when we have Iframe like in facebook apps 

Share

How to access one server if they are connect with LoadBlancer?

If you are LAMP developer and your server is connected with LoadBlancer now LoadBlancer is big advantage in production to distribute traffic but sometimes it could be trouble for developer as you want to access one server out of two.

Now here is the analogy.
You have 2 servers and web1 and web2. Web1 is main and you are accessing through FTP and adding files then web2 is synced with web1 but while you are developing few files or code you want to access only web1 how to do that ?

Well this is simple, change host file and one config file!
I am using MAC so add these 2 lines in host file and you will be accessing only web1.

edit /etc/hosts
add the lines
00.56.983.249 www.phpmind.com
00.56.983.249 phpmind.com
at the bottom of the file
edit /etc/resolv.conf
add the line
order hosts, bind
to the top of the file

And you are done!

Share

How to check error log in Apache/LAMP?

First login to your server using ssh

A. ssh phpmind@phpmind.com
B. Enter proper password

Then switch back to the root and use this command.

Benefit of “-f” option is as you hit your php page in browser terminal will auto refresh errors and you can see most recent errors.

tail -f /var/log/apache2/phpmind.com-error.log

Your Path may be diffrent! I have different sub domains parked in my server. So this is my error log path in LAMP.

Hope it will work for you.

Share

How to get IP address through php code.

Every php coder want to store IP address of user for tracking and other different purpose.

Here is the function which i am using in my code to store ip address in db.

    function userIPAdd()
    {
       //This returns the True IP of the client calling the requested page
       // Checks to see if HTTP_X_FORWARDED_FOR
       // has a value then the client is operating via a proxy
       $userIP = $_SERVER['HTTP_X_FORWARDED_FOR'];
       if($userIP == "")
      { 
         $userIP = $_SERVER['REMOTE_ADDR'];  
      }
      // return the IP we've figured out:
      return $userIP;
    }

You can use this simple php function in different ways.

Share

Display PHP Errors When Display_errors Is Disabled

When Display_errors is off in php.ini it is hard to see where the bug is!

While you are working in server where display errors are off you can add this code on top of the page and it will show whats going on in your php code.

ini_set('display_errors', 1);
 ini_set('log_errors', 1);
 ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
 error_reporting(E_ALL);

I prefer to use this code through a function or include file and when you are done you can close or disable.

Share

How to Submit form through PHP CURL ?

Last week I was working with salesforce form to collect data from third party website. If you have to just submit form its easy salesforce does not restrict to use CURL in order to post data but my requirement was to post salesforce from data and store that data in my database too. This is easy and simple and has a lot of ways to do.
Now I would like to show you PHP CURL way to post form data. You can use PHP Jquery and Ajax to make it more fancy. But I want to keep it simple.
Step 1 –
I am using one sales force form as example.

 







Step 2  –  This is standard PHP CURL script (form-action-curl.php) to post from you can use anywhere without any modification in from you can add more fields if you need.

 

//Initialize the $query_string variable for later use
$query_string = "";

//If there are POST variables
if ($_POST) {

//Initialize the $kv array for later use
$kv = array();

//For each POST variable as $name_of_input_field => $value_of_input_field
foreach ($_POST as $key => $value) {

//Set array element for each POST variable (ie. first_name=Arsham)
$kv[] = stripslashes($key)."=".stripslashes($value);

}

//Create a query string with join function separted by &
$query_string = join("&", $kv);
}
//Check to see if cURL is installed ...
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}

//The original form action URL from Step 2 :)
$url = 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';

//Open cURL connection
$ch = curl_init();

//Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($kv));
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);

//Set some settings that make it all work :)
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

//Execute SalesForce web to lead PHP cURL
$result = curl_exec($ch);

//close cURL connection
curl_close($ch);
if($result=='ok')
{
//echo '<script>alert("Posted -- ")</script>';
}
// Here you can write mysql query to insert data in table.

$insert_tbl_index_page= "insert into tbl_form_data(first_name,last_name,street,city,zip,phone,email)values('$first_name','$last_name','$street','$city','$zip','$phone','$email')";

Share