PHP

Prevent SQL injection

In: PHP

30 Aug 2010

In order to prevent SQL injection, we should clean our input first, and then use parameters we got. In order to clean input that came from our user, just use function “clean” from following snippet: 1 2 3 4 function clean( $input ) { if (get_magic_quotes_gpc()) return mysql_real_escape_string( stripslashes( $input ) ); else return mysql_real_escape_string( [...]

Sometimes there is a need to get posts outside of WordPress. In order to get latest couple of posts and present them as news outside of WP, I have written a function that might be useful. This function has two parameters. First one is database connection, and second is optional array of parameters used to [...]

PHP count words

In: PHP

25 Aug 2010

In order to use PHP to count words in some text, you can use str_word_count – a PHP word count function. This is probably the simplest way to count words in your text. PHP count words example: 1 2 3 4 5 //this line will output "3" echo str_word_count("PHP count words");   //this line will [...]

Recently, on website wprecipes.com, I have stumbled upon an interesting script displaying how to integrate your Google Analytics code into your WordPress theme by modifying only functions.php. To do so, just copy and paste following snippet into your functions.php in your current WordPress theme. 1 2 3 4 5 add_action(’wp_header’, ‘ga’);   function ga() { [...]

Use WP 3.0 menu

In: PHP|WordPress

23 Aug 2010

WordPress 3.0 has added us a nice functionality for using custom menus. Allowing us to mix categories, pages and URLs, WP gives us option to make menus with ease. In order to implement WP 3.0 menu into you theme, just use following snippet: 1 2 3 4 5 wp_nav_menu( array( ‘menu’ => ‘Menu_name’, ‘container_class’ => [...]

You might want to use WordPress to display random post. In case you want to do so, just use following snippet: 1 2 3 4 5 $rand_post = get_posts(’numberposts=1&orderby=rand’);   $post = current($rand_post); the_title(); the_content();

PHP log request

In: PHP

18 Aug 2010

Did you ever had a need for logging all parameters sent to your script? If your answer is yes – this is how you can do it in only one line. PHP log REQUEST: error_log( print_r($_REQUEST, true), 3, $_SERVER[’DOCUMENT_ROOT’] . ‘/info_index_log.txt’); This script will grab all data sent to your script (POST or GET) and [...]

PHP caching

In: PHP

17 Aug 2010

Whenever you are building dynamic (PHP) website it is a good option to consider caching. Why should you consider PHP caching? You won’t be running a whole PHP script on your server. You won’t send queries to database. You don’t have to have connection to a database. You will have less load on server. You [...]

Simple PHP captcha

In: PHP

12 Aug 2010

In this snippet we show you how to create simple PHP captcha protection for your website forms. In root of your domain create PHP file called captcha.php, and paste following snippet into it. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 [...]

PHP get current URL

In: PHP

30 Jul 2010

Very often you need to know exact URL of your current page. To get URL of current page, you should just implement following function and apply it, as shown in example below. 1 2 3 4 5 6 function get_URL() { $s = empty($_SERVER["HTTPS"]) ? ” : ($_SERVER["HTTPS"] == "on") ? "s" : ""; $protocol [...]