Showing posts with label webhosting. Show all posts
Showing posts with label webhosting. Show all posts

How to improve page load speed of your php website?

1. Use include at the place of include_once for including files

2.Remove Blank Spaces from your php code.You can use this function:

function removeBlankLines($string)
{
return preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $string);
}


3. Use , (comma) at the place of . (period) and single quote(') at the place of double quote(") wherever possible in echo statements.
For ex.
Before:
echo "my name is". $this->name."and your is".$this->your_name;
After:
echo 'my name is',$this->name,'and your is',$this->your_name;

4. Provide width and height of image tags. For eg width="10" height="10"

5. Use CDN for external Javascript files.

6. If you are using jquery or any other popular js technologies, then use google library service. 

7.Compress your css with css compressor.

8. Use minified version of javascript libraries, if not available then create one for yourself using sites like javascript compressor.

9.If possible write css files on page itself i.e inline, if file is too large then write css for visible content first on webpage and import css file in footer.

10. Import Javascript files in footer and not in header.

11. Load Javascript and other content Asynchronously or with the help of Ajax.

12. For image which are not above the fold, you can use lazyload javascipt to delay their loading.Read more about Lazy Load Plugin.

13. Compress your images using Yahoo Smush it. 

14. Use Gzip for compressing webpage.
Use this code before doctype to enable gzip compression on linux servers
 if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start();

15. Remove errors and warning from your code.

16. Use only required variables, if possible use values instead of variable.
For ex.
Before:
$j=10;
for($i=1;$i<$j;$i++) {   } // do something
After
for($i=1;$i<10 do="" font="" i="" nbsp="" something="">

17. Use microtime function of php to measure time taken by php to execute any specific function. For eg. if I want to measure time taken by first_function, I can measure it with the following code:
 
$time_start = microtime(true);
// Call your function
first_function();
$time_end = microtime(true);
$time = $time_end - $time_start;

echo "Total time to execute first function is $time seconds\n";

18. Use HTTP Caching : For PHP Website you can use header function or mod_expires

19. Avoid using SQL queries in loops(While or For), instead of that use array inside the loop and use implode function to append that array in SQL query.

20. Optimize CSS by using common declarations: 
Before
h1 { padding-top: 10px; }
p { padding-top: 10px; }
After
h1, p { padding-top: 10px; }


21. Use complex query instead of multiple queries to reduce number of requests.
Before:
Select * from products where pid=1;
Select * from text where pid=1; 
After:
Select * from products,text where products.pid = text.pid

22. You can use mysql procedure's at the place of php function. It will enhance your website security and performance.

How to Host PHP Websites at your Home Computer?

Hosting PHP Website at home has its own security risks but it is not difficult as most of you think. To host a Website you need IP address(Dynamic or Static) which you already have because you are reading this blog and Server Software like Apache or IIS and domain name.If you are using PHP and MySQL at your home computer then your computer has server installed which is either Apache or IIS. If some of you don't then should first install it from wamp.

1. Make sure that your computer has Server  Software Installed :

Download Wamp(Wamp is for windows , Lamp is for Linux) according to your Operating System(Linux , Windows or MAC) from this website. Install it and move your php files into root folder of wamp which is usually www , if your server software is different than it would be htdocs.

Now,as you moved all your files to the root folder of Apache Server ,check whether your server is working or not, by typing localhost/your_folder_name/file_name.php into the address bar of your browser. If you get the expected output then everything is working fine and you can move to step 2.

2.Hosting server needs Static Ip address , if you have that then move to step 3 , if you don't have static ip address or don't even know the meaning of ip then read this step :

Whenever one computer connects with another computer on any network it needs a unique address like we need home address to get our letters, this address is called IP (Internet Protocol) Address which is like 198.168.8.8. Whenever we connect to the internet , our Internet Service Provider allots IP address to our computer from a range of IP Addresses ,this type of allotment is called dynamic if it changes whenever we reconnect our internet and the similar address is called static when it doesn't change irrespective of our connecting or disconnecting.

Static cost us more price than dynamic and usually common user don't need static ip at all.

If you don't have static ip , I assume you don't then you should try searching some good dynamic dns web service on google , which would provide you service for free to use your existing dynamic address to host website.

3. If you are using broadband as I do then you need to change factory settings of your modem , so that it can forward any request on port 80 to your localhost wamp server.

There are many ways to do so, each vary with different modems but generally you should type 192.168.1.1 in your address bar which opens up your modem control panel. If it asks for username and password and you don't know them, just type admin as username and password both(It usually works). After that click on Add NAT which is in Advanced Setting Tab of your modem home page. In NAT, write 80 for both UDP and TCP  in there respective boxes. 80 is a Port number which is used for Web Services like http requests. Select both TCP and UDP as your packet types and in IP address, type your computer's ip address which you can find using ipconfig in command prompt.
 
4. Add Exception to all of your firewalls so that they allow any request from port 80 on your computer.You can find windows firewall in Control Panel.

5. Congratulations ! You have done it. I know there is still large scope of improvement for this article but it is a vast topic and I tried my best to make it as compact as possible.Moreover , I will keep improving it according to your suggestion and comments. Bye for Now.

Navigate from one page to another in php

Create new page and give it any name , I am giving it name as "another_page_name.php" .

Create a link for it as shown in your existing page.

Open your existing page and click on it .



its done




Related Video :