• Home
  • About
  • Who Am I?
  •  

    PHP sessions across subdomains , the multiple servers issue!

    November 16th, 2006

    Ok so sometimes we need to carry the sessions across all of the subdomains. So this is one thing you can do in your .htaccess file.

    php_value session.cookie_domain “.domain.com”

    Incase of cookies you most probably know what to do but as a reminder here it is.

    setcookie(”TestCookie”, $value, time()+3600, “/~rasmus/”, “.example.com”, 1);

    Notice the .domain.com in both cases , this lets the cookies be accessable from all subdomains.

    Now thats all goody goody BUT when it comes to having a domain span across multiple servers , like lets say you host the main domain.com on server 1 and the subdomain abc.domain.com on server 2, the session id will be carried across just fine if you use the .htaccess method BUT whatever is inside the session will NOT be available across both servers. The reason is pretty simple , lets say you begin your session on server 1 , you store some data in the session and it is stored in the domain.com session save directory BUT when you goto abc.domain.com , you cannt access the data stored in the session save directory from server 1. So basically this doesnt help much across multiple servers, does it?

    Ok so what can we do about this?

    Now after some thinking a probable solution was to use the database and keep the session data in there. But i didnt like the extra load it was putting on the MySql server.

    So after alot of thinking I devised a little plan for this , now there is one thing we do have as common on both ends , we have the session id , now you can integrate a crypt / decrypt mechanism with the session id acting as the key, to solidify it further , add some more characters to the session id and use that as a key. Then send the session data in the query string.

    something like this:

    $key = “somethinghere”.session_id() . “somethingmore”;

    Use the above generated key on server 1 to generate the query string and decode it with the same key on server 2 and vice versa if need be.



    Virtual Subdomains and PHP

    October 12th, 2006

    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!

    October 10th, 2006

    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

    October 10th, 2006

    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.