reCAPTCHA PHP example

HTML

Paste this snippet before the closing </head> tag on your HTML template:

<script src='https://www.google.com/recaptcha/api.js'></script>

Paste this snippet at the end of the <form> where you want the reCAPTCHA widget to appear:

<div class="g-recaptcha" data-sitekey="{site key}"></div>

 

PHP Code

if($_SERVER[“REQUEST_METHOD”] === “POST”)
{
//form submitted

//check if other form details are correct

//verify captcha
$recaptcha_secret = “{secret key}”;
$response =file_get_contents(“https://www.google.com/recaptcha/api/siteverify?secret=”.$recaptcha_secret.“&response=”.$_POST[‘g-recaptcha-response’]);
$response = json_decode($response, true);
if($response[“success”] === true)
{
echo “Logged In Successfully”;
}
else
{
echo “You are a robot”;
}
}

,

PHP error_reporting

Example #1 error_reporting() examples

<?php

// Turn off all error reporting
error_reporting(0);

// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE);

// Report all PHP errors (see changelog)
error_reporting(E_ALL);

// Report all PHP errors
error_reporting(-1);

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

?>

, , ,

PHP page perfomance test by time

<?php

function processing_time($START=false)
{
$an = 4; // How much digit return after point

if(!$START) return time() + microtime();
$END = time() + microtime();
return round($END – $START, $an);
}

?>

<?php
require_once “./pageload.php”;
$START = processing_time();

#####some code here########

$RESULT = processing_time($START);
echo $RESULT;

?>

,

send a multipart email (both HTML and plain text)

Example 1

<?php

$notice_text = “This is a multi-part message in MIME format.”;
$plain_text = “This is a plain text email.\r\nIt is very cool.”;
$html_text = “<html><body>This is an <b style=’color:purple’>HTML</b> text email.\r\nIt is very cool.</body></html>”;

$semi_rand = md5(time());
$mime_boundary = “==MULTIPART_BOUNDARY_$semi_rand”;
$mime_boundary_header = chr(34) . $mime_boundary . chr(34);

$to = ‘abc@abc.com’;
$bcc = “You <you@you.com>, Them <them@them.com>”;
$from = “Me.com <me@me.com>”;
$subject = “My Email”;

$body = “$notice_text

–$mime_boundary
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

$plain_text

–$mime_boundary
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

$html_text

–$mime_boundary–“;

if (@mail($to, $subject, $body,
“From: ” . $from . “\n” .
“bcc: ” . $bcc . “\n” .
“MIME-Version: 1.0\n” .
“Content-Type: multipart/alternative;\n” .
” boundary=” . $mime_boundary_header))
echo “Email sent successfully.”;
else
echo “Email NOT sent successfully!”;
?>

http://www.tek-tips.com/faqs.cfm?fid=2681

Example 2

Headers

MIME-Version: 1.0

From: Foo <foo@bar.com>

Subject: Test mail

Content-Type: multipart/alternative;boundary=np4f377521487cd

Body

This is a MIME encoded message.

 

–np4f377521487cd

Content-type: text/plain;charset=utf-8

 

This is the text/plain version.

 

–np4f377521487cd

Content-type: text/html;charset=utf-8

 

This is the <b>text/html</b> version.

 

–np4f377521487cd–


<?php

 

$boundary = uniqid(‘np’);

 

 

$headers = “MIME-Version: 1.0\r\n”;

$headers .= “From: Foo <foo@bar.com>\r\n”;

$headers .= “Subject: Test mail\r\n”;

$headers .= “Content-Type: multipart/alternative;boundary=” . $boundary . “\r\n”;

 

 

$message = “This is a MIME encoded message.”;

 

$message .= “\r\n\r\n–” . $boundary . “\r\n”;

$message .= “Content-type: text/plain;charset=utf-8\r\n\r\n”;

$message .= “This is the text/plain version.”;

 

$message .= “\r\n\r\n–” . $boundary . “\r\n”;

$message .= “Content-type: text/html;charset=utf-8\r\n\r\n”;

$message .= “This is the <b>text/html</b> version.”;

 

$message .= “\r\n\r\n–” . $boundary . “–“;

 

//mail(‘bar@foo.com’, ‘Test mail’, $message, $headers);

 

?>

 

http://krijnhoetmer.nl/stuff/php/html-plain-text-mail/
, , ,

using php check width, height and type of an image

“;
echo “Image height ” .$height;
echo “
“;
echo “Image type ” .$type;
echo “
“;
echo “Attribute ” .$attr;

?>

Output result:

Image width 379
Image height 344
Image type 2
Image attribute width=”379″ height=”344″

Type of the image
1 = GIF
2 = JPG
3 = PNG
4 = SWF
5 = PSD
6 = BMP
7 = TIFF(intel byte order)
8 = TIFF(motorola byte order)
9 = JPC
10 = JP2
11 = JPX
12 = JB2
13 = SWC
14 = IFF
15 = WBMP
16 = XBM

, , , ,

php simple regular expression

Simple regex

Regex quick reference
[abc] A single character: a, b or c
[^abc] Any single character but a, b, or c
[a-z] Any single character in the range a-z
[a-zA-Z] Any single character in the range a-z or A-Z
^ Start of line
$ End of line
\A Start of string
\z End of string
. Any single character
\s Any whitespace character
\S Any non-whitespace character
\d Any digit
\D Any non-digit
\w Any word character (letter, number, underscore)
\W Any non-word character
\b Any word boundary character
(…) Capture everything enclosed
(a|b) a or b
a? Zero or one of a
a* Zero or more of a
a+ One or more of a
a{3} Exactly 3 of a
a{3,} 3 or more of a
a{3,6} Between 3 and 6 of a

options: i case insensitive m make dot match newlines x ignore whitespace in regex o perform #{…} substitutions only once

 

e.g.

$text = “string strong stung”;
if ( preg_match( “/s.*g/”, $text, $array ) )
print $array[0];
// prints string strong stung

If we only want the match to include until the first occurrence of g, we should add a ?, which means “optional”

$text = “string strong stung”;
if ( preg_match( “/s.*?g/”, $text, $array ) )
print $array[0];
// prints string

php argument

#!/usr/bin/php
?

,

php error message – Warning: Cannot modify header information

First solution:

change the configuration of php.ini as below:

output_buffering=4096(or higher).

Second solution:

From php source code, don’t modify any data(like cookies, Locaton) in header after output html code.

Thrid solution:

Use output buffering :
<?php ob_start(); ?>
… HTML codes …
<?php
… PHP codes …
header (“Location: ….”);
ob_end_flush();
?>

, , ,