FRIHOST • FORUMS • SEARCH • FAQ • TOS • BLOGS • COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


How to write a code which will keep bandwidth to a minimum?





Kashinilaya
I posted a few days ago in the Designing Tips forum a question on how to lower bandwidth for a site that includes lots of text. I only got one reply which didn't really answered my question. So I decided to post in the Scripting forum a diferent question (it seemed to me the best place for that).

I only want to write my page once. I won't have time to improve it, so I want to start it right. Bandwidth is an important issue no matter where we decide to host our page in and I can't figure out how many people will visit my site.

My site is for showing my written work (I'm a writer). So, it's going to have many small pieces of text (like 200k to 500k bytes in length, each)

I read somewhere else in Frihost that PHP lowers bandwidth. Is that true? (sounds odd to me, for PHP is supposed to run in the server computer, i.e. the webhost computer) If yes, how to accomplish that?

I have no experience at all writting internet pages, but I do have experience in computer programming. I don't have the time to learn all that is available. I think that PHP + CSS would be the best option. Can you help me with your opinion?
LukeakaDanish
Well to be honest its difficult giving you a "fulfilling" answer...

It might be easier if you had an example of what "exactly" you want.

When you say php+css, i hope you meant html+php+css, as php is pretty useless without html...otherwise, this is a good idea...though javascript is always good...

Using php wont really decrease filesizes (as far as i know), unless you want to write the content dynamically, in which case its posible it will outscore javascript...that depends on the situation.

If all your page is going to do is show some new text every time a user clicks a link, my suggestion would be iframes/frames (i konw a lot of people hate them, but heres the reason)

The text will stay the same size unless you somehow manage to "zip" it, however everything around the text can be made to have a minimal impact on bandwidth usage, by allowing the user to load it only once -> iframes.

If you do opt for this option (Very Happy), be aware that anything inside the iFrame wont be caught by searchengines, so some keywords could be worked into the footer or something...

I hope you found this helpful

Luke
polarBear
The short answer is no, it's not possible.

The long answer:

For the user to see the text it has to be downloaded to his PC, and that is when you consume bandwidth. What you could do is, instead of loading each text on a different html page, put ALL your texts on ONE really slow loading page, and hide-unhide them via javascript. That way you could reduce the bandwidth usage A LITTLE.

You could also use only 1 CSS file for all the HTML files, and keep your markup really minimal. That way. although the HTML garbage would repeat, at least it won't repeat all the CSS rulesets, and it could make you lose less bandwidth.

OR you could try and catch some AJAX-like javascript to load specifically the texts the user wants, on HIS demand, on just ONE HTML page.

OR you could zip all the text files and let the user download them and view them offline.

None of this solutions requires PHP, though you could use it to zip the files on demand, but it's not really necessary, and none of this solutions does anything drastic, except the ZIP files.
Enderwiggin
My personal thoughts on the matter are that PHP + HTML + CSS is the way to go, Its true that PHP without HTML is essentially useless... my page http://wowchaos.frih.net/ uses 2 main pages, index.php, and extras.php... It only uses 2 to work without several annoying errors...

The extras.php uses something called php include, the code is
Code:
<?php include('$news') ?>
, when extras.php?news=asdf.txt it loads asdf.txt in the center, while if extras.php?news=news.txt it puts the news.txt file in there, and so on...

The
Code:
<?php include('$news') ?>
code has to go whereever the news & such would be... And yea... thats about it...

If I am wrong about any of this, please let me know...
Kashinilaya
Thank you all for your suggestions. Not only I got three different (and certainly good) solutions, but also it helped me diminishing my ignorance in internet terms. I understood now (specially with Polarbear's comment) that bandwidth has nothing (or has little) to do with disk access at the webhost, but the transference of data between the webhost computer and the page visitor's computer (i.e. if the data is in the webhost disk or in the webhost memory doesn’t make a difference). I'm surprised I didn't realize that before. (please correct me if I'm wrong; I must understand it once for all).

The iframe/frame solution given by Luke is exactly what I wanted for my site. I'm affraid, though, I won't have the time (and the patience) to learn how to use frames/iframes. Too many people speak ill of it, probably because it's hard to learn or requires more experience to be used properly.

If I understood it well, Polarbear gave a solution which is similar to Luke's, but using javascript. Is that correct? Is javascript processed at the user's (= page visitor's) computer? If yes, it could really improve bandwidth, specially by zipping the texts and let them being extracted at the user's computer (not offline, though). Otherwise, it sounds like as good a solution as iframes, maybe with the advantage of being easier to learn and use.

Thank you Enderwiggin for the practical example. I don't know PHP and CSS fully yet (actually, not even HTML), but I believe I can handle them enough for building my page. I need to keep it simple, if I want to do it fast.

I'll think of all the solutions and come to a decision shortly. Anyway, I really appreciated your comments.

If you mind taking the time to answer two more questions related to bandwidth I'll be very thankful:

    If I keep the pictures I want to display in my site in a different server (say, a server outside Frihost), will it reduce bandwidth in my site server (say, at Frihost)?


    Is javascript processed at the user's computer? (or is there any other language which works that way?)


Thanks again.
LukeakaDanish
Kashinilaya wrote:


If you mind taking the time to answer two more questions related to bandwidth I'll be very thankful:

    If I keep the pictures I want to display in my site in a different server (say, a server outside Frihost), will it reduce bandwidth in my site server (say, at Frihost)?


    Is javascript processed at the user's computer? (or is there any other language which works that way?)


Thanks again.


1: This will dramatically improve your bandwidth usage, as images take up so much more space than text.

(Monthly) Bandwidth = "The total size of all data packets downloaded by users of your site from the server in question"...or something like that

2: Yes, javascript is processed at the users computer! This doesnt neccesarily make it good for reducing bandwidth but used in conjuntion with php, its a very good solution

A word on iFrames

The reason why people hate them is not coz their hard to use. Pretty much everything in HTML is SO easy, it could be learnt by a 7 year old...as long as they understand english.

The main reason they are disliced is that searchengines like google dont see the content you put inside them and therefor the phrases you use ONLY inside iFrames wont bring your page out pf the search engine.

That being said, if your linkmenu and some basic stuff is in the main document, iFrames arent too bad! I would recomend them for your page...it seams appropriate!

Following will produce an iframe

Code:

<iframe
   frameborder="1"
   marginheight="10"
   marginwidth="10"
   noresize
   scrolling=auto
   src="intro.html"
   name="frm1"
   style="height: 450px; width: 492px; border-color: navy;">


Once you have learnt html most of this should be self explanatory. If you want to have a transparent background (so the background of your main page show) add
Code:
allowtransparency="true"

to the iframe and add
Code:
style="background-color: transparent;"

to your framed pages.

Hope this was helpful. PLease feel free to ask about anything you dont understand

Regards

Luke
LukeakaDanish
BTW: You cant have the users browser unzip files...they would have to be downloaded (as in downloading a program) and then unzipped using windows and then viewed.

Javascript is more for writing a lot of stuff which is very similar out like

Line1
Line2
Line3
Line4
Line5
Line6
Line7
Line8
Line9
Line10

could be written using the following script:

Code:
i=1
do {
document.write('Line' + i);
i++
}
while(i<=10);


I just thought of the ultimate tip for using as little bandwidth as possible:

Learn HTML and CSS thoroughly!

There is not much more to it...with good knowledge you should be able to judge what produces the smallest filesizes yourself.

By the way: How popular do you expect your site to be? 10Gb bandwidth is A LOT if your content is mostly text! I personally think 1Gb is enough for 9 out of 10 people!

Luke
LukeakaDanish
Quote:
My site is for showing my written work (I'm a writer). So, it's going to have many small pieces of text (like 200k to 500k bytes in length, each)


what is 200k to 500k..is k kilobyte? in that case that isnt small pieces of text...thats friggin 50page essays!

if you mean 200 to 500 characters (i suspect you might) then that would translate to...maybe 1 to 3 kilobyte...


10,000,000,000 (10gb) is the total bandwidth

say each user loads your main page (10kb), images (10kb more) and pages of text files (30kb)...that adds up to 50kb --> 50,000

divide 10,000,000,000 by 50,000 and you'll get 200,000

Unless you expect more than TWO HUNDRED THOUSAND visitors PER MONTH...thats 6452 VISITORS PER DAY, 10 Gb bandwidth is enough...in fact, 10Gb bandwidth should be pleanty however baddly you make your page, and however little of the advice we have given you, you follow!

Of course i know nothing about you...you might be J.K Rowling, in which case, 10Gb is very little, but i recomend doing a little maths before worying about bandwidth...

Hope I continue to say useful things

Luke

(Sorry for the tripple posting...but the stuff i discuss in the diffent posts becomes isnt, as such, related. If you want to subtract 6Points from me (admin)...feel free to do so...i have plenty.)
kv
LukeakaDanish wrote:
BTW: You cant have the users browser unzip files...they would have to be downloaded (as in downloading a program) and then unzipped using windows and then viewed.


Not really. Most of the browsers support gzip format. That is, they are capable of receiving html/text data in gzip format, unzipping it and displaying it. This way you would be actually using only around 10% of the bandwidth you would have used otherwise (assuming text only contents are zipped by a factor of 10). Lot of forum softwares have gzip support (you can figure it out when the bottom of the page says "gzip enabled")
LukeakaDanish
so the files would be something like.html.gz2? then just include() or iframe them as usual?

This sounds nice...maybe this is something i should learn Very Happy
kv
LukeakaDanish wrote:
so the files would be something like.html.gz2? then just include() or iframe them as usual?

This sounds nice...maybe this is something i should learn Very Happy


No. If the extension is .gz2, browser will try to download it as a file. You just send the gzipped html content with the following headers:

Content-Type: text/html; charset=utf-8
Content-Encoding: gzip

Observe the second line. Then you can write the gzip content (html contents gzipped). Browser will unzip it. The problem with this method is that it doesn't work for static files and requires a script. But you can use a workaround. Set the headers in a script and write the contents of the file in the script instead.

If you enable "mod_gzip" in apache, you can configure apache to compress and serve pages with correct headers.
ChunkyBustout
Since you aren't into and don't have enough to program html/css and so forth, I reccommend you use a content mangement system that doesn't require a database (just one less thing to go wrong. I started my own page this way and later, coded it by hand using just html/css with a few small javascripts added. It's not the best thing but I like it and you can view it here http://www.chunkybustout.com. When you view my blog, you'll see a different thing altogether and it alone may suit your needs. Its a free php script for blogs but you can set it up to do a lot more. Its very easy to use and customize. It's call pppblog.

There are many free systems available and they usually come with a few templates to help customize your layout. You can always edit the templates to suit your needs. A place to start is http://opensourcecms.com. They provide links to most CMS packages around and even a demo page for each one so you can see it in action. When you get time to want to program, you can do your page just like how you have it setup in the CMS since you know it works.

I hope this helps because I wish someone could have pointed these things to me to save me time.
Atomo64
One of the best ways to reduce the bandwidth usage are these:
use gzip compression, either by using ob_gzhandler or zlib, or the mod_gzip apache module
Remove html comments, blank spaces and others... on HTML, CSS and JS.
Use AJAX
Kashinilaya
Before I place my replies, let me secure you understand how thankful I am for your comments. I realize I'm very, very ignorant on internet programming and that requires a tremendous patience from the teachers. You have all been very kind in helping me here and I sincerely recognize it.

Let me start replying Luke's comments.
I'll definitely consider iframes. The code you provided will be very handy. I'll study it and take the decision.
I thought that using javascript I could use a command to unpack a zip file, probably using the self-extract feature Zip has. Anyway, Kv posted an interesting solution to that.
It seems that I did screwed up in my calculations. I don't have access to internet at home. This is not my computer, so I don't have the files here. I was lazy to check the actual files' size and then looked up some files in this computer. There are many 25Kb files here, which content takes about half of a page (A4 size). My texts range from 2 to 5 A4 pages, each. One of them takes more than ten pages. But I can't do the math like that. You are probably right about the expected file sizes and probably right when said that 10G bandwidth will be enough for me. I don't think I'll get continuous access to my page for a given visitor. Once a user accesses it and reads my texts there will not have much else to see there. But, once my site is on, I plan to start a national campaign to attract people to my site, as a request for cultural support. Something like: "please, visit my page and invite everybody in your maillist to do the same." I have no idea if this sort of thing succeeds in Brazil (in some other countries I'm sure it would!), but if it works I may have a large amount of people trying to access my page at once. If I have bandwidth problems by then, my site will be unaccessible and, the worst, people might not try a second time. So far, I'm a nobody (yes, I'm not J.K. Rowling). I feel that it will be a one-shot chance, until I get Rowling's popularity. Anyway, I should probably get rid of this perfectionism (and worries) and put things in motion at once.

Now, Kv's information is really interesting. Could you please give more information on gzip? (like where can I find it, if it is a free software, how to use it, whatever you know about). Your reply to Luke's question sounded like Chinese to me, which means that I have a whole lot to learn. Regardless of what I decide to do, it's certainly going to be useful!

ChunkyBustout suggested CMS. I looked into that a few days ago and thought it didn't apply to what I wanted. Having taken a look in your site, though, I'll have to reconsider it (by the way, nice site and nice pictures). I'll probably need to give up some nice features and fancy look I planned for it, until I can handcode it, but having a working site fast may pay off. It was sure a useful hint!

Atomo64 clues are also welcome, specially the gzip additional information. Where can I find the modules/software you mentioned?
What is AJAX?

I feel like I should thank you all again and again. I hope one day I'll be able to help you somehow. Little chance it'll happen with scripting. I'm too old and too focused in other things to reach your level of knowledge.

Best thoughts!
Atomo64
Marston
Umm, nobody here's heard of PHP Caching?
Atomo64
Marston wrote:
Umm, nobody here's heard of PHP Caching?

what do you mean with PHP caching?
kv
I Guess he is referring to php accelerators.

Kashinilaya,

Can you pm me with your exact requirements? I may be able to help you.
Atomo64
the Zend Optimizer?
Rising
kv wrote:
LukeakaDanish wrote:
so the files would be something like.html.gz2? then just include() or iframe them as usual?

This sounds nice...maybe this is something i should learn Very Happy


No. If the extension is .gz2, browser will try to download it as a file. You just send the gzipped html content with the following headers:

Content-Type: text/html; charset=utf-8
Content-Encoding: gzip

Observe the second line. Then you can write the gzip content (html contents gzipped). Browser will unzip it. The problem with this method is that it doesn't work for static files and requires a script. But you can use a workaround. Set the headers in a script and write the contents of the file in the script instead.

If you enable "mod_gzip" in apache, you can configure apache to compress and serve pages with correct headers.
Yes.I think GZIP is the best solution to reduce the bandwith.
Stubru Freak
If you want to use AJAX, you should watch out, it can also cost you even more bandwidth.

Without AJAX, when someone goes to your website, the lay-out and text is loaded. If someone goes to a new page, lay-out + text is loaded again.

Without AJAX, when someone goes to your website, the lay-out and text is loaded, but there is also a script loaded which will retrieve new pages without having to download the lay-out again. So if someone goes to a new page only the text is loaded.

This is usefull if you have an extensive lay-out and/or you expect people to retrieve numerous pages before leaving your site.
Otherwise it will only cost you bandwidth because of the extra AJAX code loaded.
Atomo64
Stubru Freak wrote:
If you want to use AJAX, you should watch out, it can also cost you even more bandwidth.
[...]
This is usefull if you have an extensive lay-out and/or you expect people to retrieve numerous pages before leaving your site.
Otherwise it will only cost you bandwidth because of the extra AJAX code loaded.

the AJAX script doesn't need to be very large, and you can even cache the data from a page, so if you go from the main page to the contact page and then you want to come back, there's no need to ask the server again. With a less than 1kB script you can do it... or if it is larger, by removing comments, spaces (blanks) and by using gzip you can reduce the size of it.
Kashinilaya
Thank you all again.
As a matter of fact I'm getting overwhelmed with so many possibilities for solving my problem. I'm affraid I'll have to blindly pick up some of those solutions and pray they'll be enough for my needs. Unfortunately, I won't have time to study all of them.

Kv I really appreciate your kindness in offering additional help. I'm sending you a PM shortly with the specifications of what I want (is that what you meant with "requirements"? I have the pseudo-code of most of the site, but would need to translate it into English). Please wait a little longer. This has been a busy week for me. If I don't manage to PM you by the end of the week, I'll do it so in the beginning of next week.

Be well.
aningbo
The truth is.... you can minimize your bandwidth by actually using pictures to the minimum. so try to avoid those huge pictures which r really unnecessary.

coding the scripting doesn't take much space and not much bandwidth.
Mgccl
to minimize the bandwidth is to make all the unessery stuff gone, like if you have js, take out the useless space and other kind of things that are not useful to the website viewer....
also, only show meta tags to the people who are not the member of your site(that's how include search engine)
Related topics
Java
Windows Tips&tricks!
can a virus cause unrecoverable damage to HD?
website program
Help Apache problems
PHP, MySQL and Flash
My first Visual Basic program!
Whoopsadaisy!
Changements to the Points/frih$ system (Discuss)
History of the BASIC family of languages
Notepad++.5.0.beta
Write Code once if You use JQuery
PHP_ini Display Errors is turned on!
Frihost Android App
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.