PHP four tips for beginners

PHP

Posted on 2015-04-20 02:49:49


1. Get full source code html website from (address bar)

Get full link from address bar, you can use two functions are : $_SERVER[‘HTTP_HOST’] và $_SERVER[‘REQUEST_URI’]. Example :
URL
 $_SERVER[‘HTTP_HOST’] :  Return host address, this case is www.apple.com$_SERVER[‘REQUEST_URI’] Return /downloads/dashboard/email_messaging/todo.html
 Get full address, you can use(.) merge two functions:
1
2
3
4
<?php
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo $url;
?>
2 Get Width (width) và height (height) of image
We can do work with getimagesize(). This function help our get size of image, include swf file. Example:
01
02
03
04
05
06
07
08
09
10
<?php
list($width, $height, $type, $attr) = getimagesize("image_name.jpg");
echo "Image width " .$width;
echo "<BR>";
echo "Image height " .$height;
echo "<BR>";
echo "Image type " .$type;
echo "<BR>";
echo "Attribute " .$attr;
?>
When you run behind code, you will see result :
Image width 379 Image height 344 Image type 2 Image attribute width=”379″ height=”344″
Some type of Image Type: 1 = GIF     5 = PSD     9 = JPC     13 = SWC 2 = JPG     6 = BMP     10 = JP2     14 = IFF 3 = PNG     7 = TIFF(intel byte order)     11 = JPX     15 = WBMP 4 = SWF     8 = TIFF(motorola byte order)     12 = JB2     16 = XBM
You can choose suitable type for each screen
3 Forward page in PHP You can forward page with two options, use function  header or meta
1
2
3
header("location:http://tutscode.com");
// hoặc
echo "<meta http-equiv='refresh' content='0;url=http://tutscode.com'>";
However you can hide auto time forward page:
1
2
3
<HEAD>
<meta http-equiv='refresh' content='2;url='file_name or URL'>
</HEAD>
In which:
content = time (second)
file_name = File name which is been wanted to refresh or redirect
URL = Which is been you wanted redirect to there
4 compress page with PHP
To reduce bandwidth and increase speed load your website, we need zip that website. Have two options for do it. However you must exactly your hosting support for gzip and deflate
- By 1 : Use zlib.output_compression You can open file php.ini and add column code :
1
zlib.output_compression = On
If in php.ini file which has that code. You can edit suitable. You can edit level compress with options :
1
zlib.output_compression_level = d
With d is character from 1 (minimal compression) to 9 (maximum compression)
- By 2 : Use ob_start
If you can not edit php.ini file. However you can compress website, you could add behind code and name website you want to compress (You must push that code to the first column in all files)
1
ob_start('ob_gzhandler');