Skip to content

My PHP , Wordpress and Linux Lab

Archive

October, 2006Archive for

Virtual Subdomains and PHP

Oct 12
Posted by : Sabeen Malik in PHP, Web World

With the ever dynamic web world and to turn the web from a passive to an active entity , webmasters are fascinated with the idea of having subdomains for users like http://username.domain.com. Looks quite neat and tricky. So lets see how we can achieve this with apache and some PHP.

First i go in my server and change the DNS zone for the requested domain , lets say abc.com. On WHM you can accomplish this easily by going into WHM , then clicking Edit DNS Zone, this will show you the list of DNS zones available on the server , from the list choose abc.com , click Edit. Now you will see various DNS entries for abc.com , in an empty box  at the bottom ,  add a wildcard (*) CNAME entry something like this..

* 14400 IN CNAME abc.com.

If you are not using WHM , you will have to manually find the DNS zone file for abc.com on your server and edit it to take effect like above.

Now what we have done is , we have created a wild card subdomain , meaning that if the subdomain requested doesnt exist , this wild card subdomain will catch it , you can also call this a Catch-All subdomain.

Next i go and edit my httpd.conf file and add a virtual host something like this…

<VirtualHost 11.11.11.11>
ServerAlias *.abc.com
ServerAdmin admin@abc.com
DocumentRoot /home/abc/public_html/subdomains
ServerName *.abc.com
User abc
Group abc
</VirtualHost>

In above block what i have done is , i have pointed the requests for *.abc.com to a different folder than the main domain , this folder can be inside your main domain folder as well , in my case the folder is inside the main domain folder and is called “subdomains”. So any subdomain that is not actually found in the DNS entry will point to this entry in the httpd.conf file and that in turn will point to the folder /home/abc/public_html/subdomains .

Now after doing the above you can put anything inside subdomains folder i.e example.php and you can call this via your browser like this. http://anything.abc.com/example.php , works good , eh?

Now what i will do inside my example.php is ….

$exp = explode(”.”, $HTTP_HOST) ;
$username = $exp[0];

Now what we have done above is , exploded the HTTP_HOST variable and found the username. Once we have that , we can bring in the rest of the data based on it from the DB.The best thing about this approach is that it doesnt overload the DNS server with 100s of subdomain entries.

I find this a very easy and usefull approach , hope this helps someone else as well. Have fun!

Talking Google!

Oct 10
Posted by : Sabeen Malik in Web World

GoogleOk great Google got YouTube as well , now what next? I wish they would buy Microsoft one day! :D

But no really , what do i really think about that? I will tell you what i think , i think Google knows way too much about me and you than we think.

I see it like this …

1 : Google Search — Google knows about the interests i have , basically they can create a profile of each user based on the all the searches.

2 : GMail — Google knows what written in the emails i send and receive.

3 : Talk — Google knows what sort of friends i have and what i talk about them.

4 : Orkut — Google again knows whats the circle of friends i keep and what interests me in life.

5 : Vdo/YouTube — Basically they want to know more about me by having my personal vdos in there.

6 : SpreadSheet — Spreadsheets are used for managing finances etc , so they know a bit more about me.

7 : Alerts/Calender — Its another way of knowing a bit more about me.

8 : Google Desktop — What if it starts sending info back home at some point about what i have on my computer?

Now most of you might think that Microsoft is already doing this. But the question is , if the big G agreed to deal with Chinese govt regarding searches. Then why wouldnt Google sell it to any one else , for instance the US Govt or any one else for that matter ? Google a while back said something on the lines “we want to organize all of the users data” .. hmmmm .. So basically we know where they are headed, a complete control over the users data and maybe even go through it once a while?

I personally like Google alot , and i mean alot but i am just not sure what will they become in the future and how will they use the information the have. Google is a giant we all welcomed in our life , most of us probably just wanted to see Microsoft get its ass kicked real hard. The dream has come true , Microsoft is quite afraid of Google now but will this turn into a nightmare for a trusting user in the years to come?

Cheap and Easy Backup Solutions

Oct 10
Posted by : Sabeen Malik in Web World

I was not a real strong believer of backups , cause i thought i am good to hard drives why would they act against me. And it turned out i was TOTALLY WRONG. Hard drives couldnt care any less if they were in a bad mood and it dawned on me like 6 months ago.

One night i am browsing my sites on my server and i see weird write permission denied errors , even for session data. Now i thought it was a simple glitch , so i went and did a reboot from my control panel provided by The Planet. Next thing i know , the server fails to restart. I put in a ticket and the great support people at the planet tell me that the BIOS isnt detecting any hard drive and i am like .. no no no , that cannt be so! After alot of effort by them they declared it was dead, nothing can be done. They went out of the way for me to check with techs if they can find a way to scrap off any data from the dead HD. But no luck! So i end up loosing about 5 GB of data on that server , with little or no backup. A lesson to be learned was there for me!

So they got the server back up for me with a new HD as soon as humanly possible for them. and the first thing i did was to buy a streamload.com account now known as mediamax.com then i went into my WHM (cPanel/WHN rox , Plesk Sux) and setup a periodic backup via FTP to streamload.com and since then i have been at peace. Atleast all the DB and files will be safe, the server config can be re-done with some pain, thats not a big deal.

So i suggest any of you who have dynamic websites or servers, getting mediamax/streamload account isnt a bad idea at all , even a free account will serve your purpose.

If you have 2 servers , you can swap the backups , backup server 1 on server 2 and vice versa. This way you dont even need a third party solution for backup. The chances of both servers getting kicked in the balls at the same time are really slim , unless goddess of bad luck loves you :)

I learned my lesson the hard way , i should have known better after being in this field for like 6 years now. I hope someone who reads this understand the importance of having a backup solution.

Wow, the moon is so .. BORING!

Oct 10
Posted by : Sabeen Malik in Telescope Fun

The Boring MoonDont understand why would anyone even want to go there after spending millions of dollars? Probably going to Mars back then would have been a better idea , cause seems like NASA never made it even to the moon after that. May be it would have been good idea to rig the landing video of Mars instead of the Moon? :D

A lengthy script hanging other pages of your site?

Oct 10
Posted by : Sabeen Malik in PHP

Lets suppose you have made a data crawling application and you are using sessions in it. Now you want the user to keep going through other sections of the application while you have clicked on the “Run Crawler” link and open it in a new window. Now what will happen is the other pages will be locked as long as your long script runs.

The reason is quite simple , the session will lock session access from other pages untill the script ends and the final session data is written. This is where the session_write_close() comes in real handy , once you have used the session as you want on top of the lengthy script , call session_write_close() , this will finalize the session for that page and allow other pages from the same site and session to be free to use it again.

Hope it helps someone!

Multi Threading with PHP?

Oct 10
Posted by : Sabeen Malik in PHP

Ok the title is a bit mis-leading , but well there was a point where i had to run alot of PHP processes simultaneously and use the servers resources to the fullest. For instance if you are making a cralwer or something and find that its too slow processing a page and then moving on to the next. You can use this approach.

for($i = 1 ; $i < 10 ; $i++){
$param1 = “anything”;
$param2 = $i;

exec(”pkill -f ‘runone.php $param1 $param2′”);
exec(”/usr/local/bin/php /home/abc/public_html/runone.php $param1 $param2 > /dev/null &”);
}

What this will do is , first kill the process if it already exists in the background and then tell linux to execute PHP from command prompt with the script path as parameter , then $param1 and $param2 can be the parameters you pass to that script , because remember that PHP from shell CAN NOT take parameters from query string.

So $param1 and $param2 will be received in runone.php like this..
$param1 = $argv[1];
$param2 = $argv[2];

Hope this helps someone! :)

Gmail POP with PHP

Oct 10
Posted by : Sabeen Malik in PHP

Ok the problem i faced with Gmail POP with PHP is that i couldnt figure out how to work with the encrypted tsl protocol . So here is what i did , i installed stunnel on my linux server , created connections to Gmail POP and SMTP in them and connected my PHP code to stunnel on localhost instead of connecting directly to Gmail and it works like a charm  :)

Just make sure your not over connecting with Gmail or they will block you.

Bandwidth control with PHP

Oct 10
Posted by : Sabeen Malik in PHP

I was given a task to restrict number of downloads , divide the total bandwidth into 2 groups and control the speed of the downloads. So i came up with the idea of doing it with PHP cause mod_bandwidth or mod_throttle werent tailored for the job.

Here is the part where i attempt to control the speed of the download.
$filedownload = “files/abc.exe”;
$time = 10000;
$obytes = 150*1024; //150k download speed restriction
$fd = fopen ($filedownload, “rb”);
while (!feof ($fd)) {
list($usec, $sec) = explode(” “, microtime());
$time_start = $usec + $sec;
$bytes = ceil($obytes/100);
echo fread($fd, $bytes);
flush();

if($time < 10000) usleep(10000-$time);
$i++;

list($usec, $sec) = explode(” “, microtime());
$time_end = $usec + $sec;
$time  = ceil(($time_end – $time_start)*1000000)+10;
}
fclose ($fd);

Basically we know how many max bytes we need to send per sec , so we calculate the time in microseconds it took for the code iteration to take place and deduct that time from 1 sec and usleep it for that long , if it took more than 1 sec to complete the iteration ( for example the user is on dialup and cannt receive 150k per sec) than dont put in the delay.

Hope this helps someone.

Force output to browser in PHP

Oct 10
Posted by : Sabeen Malik in PHP

During the past i have had some serious issues with scripts that were supposed to run for quite some time and getting them to show output as the script proceeds

for instance lets take this code for example:

for($i=1 ; $i < 100 ; $i++){
echo i.”<br>”;
sleep(1);
}

Now the above code should print $i as it proceeds but most of the time it doesnt happen that way. So what i found after some research is .. put the code like this.

ob_end_flush();
for($i=1 ; $i < 100 ; $i++){
echo i.”<br>”;
@ob_fllush();
flush();
sleep(1);
}

Also some times the httpd.conf file includes instructions to turn on gzip compression on all or selected sites. In this case the script wont output untill it reaches the end , simply because the gzip compression takes place first on the script output and then the compressed data is sent to the bowser for decoding. I would suggest turning off compression, this will surely help :)

Major Crash

Oct 8
Posted by : Sabeen Malik in RC Airplanes

Damn! Flying straight ahead and going for a turn , all of sudden the sticks stopped responding. The next thing i know i hear the plane hitting in the tree and my heart sinks , anticipating a complete mess but luckily it was not that bad. I was very angry and more angry cause every one thought , it happened cause i dont know how to fly!. My understanding was that there was a temporary radio interference , which we had noticed earlier as well when the plane went for a dip while flying straight. Later on Mr Adeem said this was very probable and he is now changing the servo and the receiver. Hopefully this wont happen again!

Khurram was around to witness the whole scene. Gohar and Haroon were on their way to the field. and when they got their , the plane had been rescued by then and i was trying to explain what happened , but no one really thought it was possible , until later on Adeem and the Hitec guys confirmed my suspicions. Gonna get the plane back on friday!