Home >Backend Development >PHP Tutorial >Super practical PHP code snippets, _PHP tutorial
When you send an email, you may want to know whether the email has been read by the other party. Here’s a very interesting snippet of code that displays the actual date and time the record was read by the other party’s IP address.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<?
error_reporting (0);
Header( "Content-Type: image/jpeg" );
//Get IP
if (! empty ( $_SERVER [ 'HTTP_CLIENT_IP' ]))
{
$ip = $_SERVER [ 'HTTP_CLIENT_IP' ];
}
elseif (! empty ( $_SERVER [ 'HTTP_X_FORWARDED_FOR' ]))
{
$ip = $_SERVER [ 'HTTP_X_FORWARDED_FOR' ];
}
else
{
$ip = $_SERVER [ 'REMOTE_ADDR' ];
}
//Time<code class="php comments">//Time
$actual_time<code class="php variable">$actual_time = time();<code class="php plain">= time();
$actual_day<code class="php variable">$actual_day = <code class="php plain">= date<code class="php functions">date (<code class="php plain">( 'Y.m.d'<code class="php string">'Y.m.d' , <code class="php plain">, $actual_time<code class="php variable">$actual_time );<code class="php plain">);
$actual_day_chart<code class="php variable">$actual_day_chart = <code class="php plain">= date<code class="php functions">date (<code class="php plain">( 'd/m/y'<code class="php string">'d/m/y' , <code class="php plain">, $actual_time<code class="php variable">$actual_time );<code class="php plain">);
$actual_hour<code class="php variable">$actual_hour = <code class="php plain">= date<code class="php functions">date (<code class="php plain">( 'H:i:s'<code class="php string">'H:i:s' , <code class="php plain">, $actual_time<code class="php variable">$actual_time );<code class="php plain">);
//GET Browser<code class="php comments">//GET Browser
$browser<code class="php variable">$browser = <code class="php plain">= $_SERVER<code class="php variable">$_SERVER [<code class="php plain">[ 'HTTP_USER_AGENT'<code class="php string">'HTTP_USER_AGENT' ];<code class="php plain">];
<code class="php spaces">
//LOG<code class="php comments">//LOG
$myFile<code class="php variable">$myFile = <code class="php plain">= "log.txt"<code class="php string">"log.txt" ;<code class="php plain">;
$fh<code class="php variable">$fh = <code class="php plain">= fopen<code class="php functions">fopen (<code class="php plain">( $myFile<code class="php variable">$myFile , <code class="php plain">, 'a+'<code class="php string">'a+' );<code class="php plain">);
$stringData<code class="php variable">$stringData = <code class="php plain">= $actual_day<code class="php variable">$actual_day . <code class="php plain">. ' '<code class="php string">' ' . <code class="php plain">. $actual_hour<code class="php variable">$actual_hour . <code class="php plain">. ' '<code class="php string">' ' . <code class="php plain">. $ip<code class="php variable">$ip . <code class="php plain">. ' '<code class="php string">' ' . <code class="php plain">. $browser<code class="php variable">$browser . <code class="php plain">. ' '<code class="php string">' ' . <code class="php plain">. "rn"<code class="php string">"rn" ;<code class="php plain">;
fwrite(<code class="php plain">fwrite( $fh<code class="php variable">$fh , <code class="php plain">, $stringData<code class="php variable">$stringData );<code class="php plain">);
fclose(<code class="php plain">fclose( $fh<code class="php variable">$fh );<code class="php plain">);
//Generate Image (Es. dimesion is 1x1)<code class="php comments">//Generate Image (Es. dimesion is 1x1)
$newimage<code class="php variable">$newimage = ImageCreate(1,1);<code class="php plain">= ImageCreate(1,1);
$grigio<code class="php variable">$grigio = ImageColorAllocate(<code class="php plain">= ImageColorAllocate( $newimage<code class="php variable">$newimage ,255,255,255);<code class="php plain">,255,255,255);
ImageJPEG(<code class="php plain">ImageJPEG( $newimage<code class="php variable">$newimage );<code class="php plain">);
ImageDestroy(<code class="php plain">ImageDestroy( $newimage<code class="php variable">$newimage );<code class="php plain">);
<code class="php spaces">
?><code class="php plain">?>
|
Source code
A great code snippet to easily extract keywords from web pages.
1 2 3 4 5 6 7 8 9 10 |
$meta = get_meta_tags( 'http://www.emoticode.net/' );
$keywords = $meta [ 'keywords' ];
// Split keywords
$keywords = explode ( ',' , $keywords );
// Trim them
$keywords = array_map ( 'trim' , $keywords );
// Remove empty values
$keywords = array_filter ( $keywords );
print_r( $keywords );
|
Source code
Using DOM, you can easily grab links from any page. The code example is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$html = file_get_contents ( 'http://www.example.com' );
$dom = new DOMDocument();
@ $dom ->loadHTML( $html );
// grab all the on the page
$xpath = new DOMXPath( $dom );
$hrefs = $xpath ->evaluate( "/html/body//a" );
for ( $i = 0; $i < $hrefs ->length; $i ++) {
$href = $hrefs ->item( $i );
$url = $href ->getAttribute( 'href' );
echo $url . '<br />' ;
}
|
Source code
In WordPress, if you want to automatically convert the URL and jump to the hyperlinked page, you can use the built-in function make_clickable() to perform this operation. If you want to operate the program outside of WordPress, then you can refer to the wp-includes/formatting.php source code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
function _make_url_clickable_cb( $matches ) {
$ret = '' ;
$url = $matches [2];
if ( empty ( $url ) )
return $matches [0];
// removed trailing [.,;:] from URL
if ( in_array( substr ( $url , -1), array ( '.' , ',' , ';' , ':' )) === true ) {
$ret = substr ( $url , -1);
$url = substr ( $url , 0, strlen ( $url )-1);
}
return $matches [1] . "<a href="$url" rel="nofollow">$url</a>" . $ret ;
}
<code class="php spaces">
function<code class="php keyword">function _make_web_ftp_clickable_cb( _make_web_ftp_clickable_cb($matches $matches<code class="php plain">) { ) {$ret
<code class="php plain">= $ret<code class="php string">'' ; = ''<code class="php variable">$dest = ;$matches
<code class="php plain">[2]; $dest<code class="php spaces"> $dest = <code class="php plain">= $matches<code class="php string">'http://' . [2];$dest
<code class="php plain">; $dest<code class="php spaces"> = if 'http://'<code class="php plain">( empty . ( $dest<code class="php variable">$dest ) ) ;
<code class="php keyword">return
<code class="php variable">$matches if<code class="php plain">[0]; ( // removed trailing [,;:] from URL empty<code class="php spaces"> if (<code class="php plain">( in_array($dest<code class="php functions">substr ( ) )$dest
<code class="php plain">, -1), return<code class="php keyword">array $matches<code class="php plain">( '.' [0];,
<code class="php string">',' // removed trailing [,;:] from URL<code class="php plain">,
<code class="php string">';' if<code class="php plain">, ':' ( in_array()) === true ) { substr<code class="php spaces"> $ret (= $dest<code class="php functions">substr ( , -1), $dest array<code class="php plain">, -1); ($dest '.'<code class="php plain">= substr , ( ','<code class="php variable">$dest , 0, , strlen ';'<code class="php plain">( $dest , )-1); ':'<code class="php spaces"> } )) === true ) {
<code class="php keyword">return $ret<code class="php variable">$matches [1] . = "<a href="$dest" rel="nofollow">$dest</a>" substr<code class="php plain">. $ret (; $dest<code class="php plain">} , -1);function
<code class="php plain">_make_email_clickable_cb( $dest<code class="php variable">$matches ) { = substr<code class="php variable">$email = ($matches $dest<code class="php plain">[2] . '@' , 0, . strlen<code class="php variable">$matches [3]; ( $dest<code class="php keyword">return $matches )-1);[1] .
<code class="php string">"<a href="mailto:$email">$email</a>" ; }
|
源码
数据URL可以直接嵌入到HTML/CSS/JS中,以节省大量的 HTTP请求。 下面的这段代码可利用$file轻松创建数据URL。
1
2
3
4
5
|
function
|