Saturday, April 16, 2011

How to display a selected part of HTML file using file_get_contents()

The PHP's file_get_contents() function can read a entire HTML page into a string.And we can use this function to get a page from internet and to display only the specified area we want,for example : 
if a file http://any_page.html has following design elements 

<div class='main'>
some content
<div class='extra'>
some unneeded content


and if we want to display only <div class='main'>some content</div> part in our webpage then we need to find out position of string <div class='main'> as the start point and position of string <div class='extra'> as a end point.To do that we need 3 more functions of PHP that are strpos()  strlen() and substr().

substr() : Returns the position of the first occurrence of matched string.
(note: there is a difference between strpos() and strrpos() ) 
strpos() : Returns the portion of string with "strat point" and "length" as parameters.
strlen() : Returns the length of the given string.

now with the knowledge of this functions you can use some function like this

function get_selected_area($strat_string,$end_string,$page_address) 
$whole_html = file_get_contents($page_address);
$strat_point = strpos($whole_html,$strat_string) ;
$end_point = strpos($whole_html,$end_string);
$length_of_needed_string = ($end_point - $strat_point) - 1; //because we don't want "<" in the end
$needed_part_of_html = substr($whole_html,$strat_point,$length_of_needed_string); 
return  $needed_part_of_html;
Now if you put this function in a separate php file and use it via include() or require() the following function can be used any where in any page like
echo get_selected_area("<div class='main'>","<div class='extra'>","http://any_page.html");
 NOTE: The string chosen for strat and end must satisfy the HTML standards,for example :
 you can't do something like
echo get_selected_area("<div class='main'>","</div>","http://any_page.html");
as a HTML page may have several </div> even before the <div class='main'> and this will surely give you some hard time. 

You can also use this alternative function if you want it's short in code.

function get_selected_area($strat_string,$end_string,$page_address) 

$whole_html = file_get_contents($page_address);
$strat_point = strpos($whole_html,$strat_string) ;
$end_point = strpos($whole_html,$end_string);                                        for($i=$strat_point;$i < $end_point;$i++)                                          $needed_part_of_html = $whole_html[$i];

Saturday, January 15, 2011

How to run a CS(Counter Strike) server from home

Wanna play Counter Strike with your clan?and no arrangements for the LAN party then this tutorial might helpful for you,run your private CS server from your home and play with your mates despite anywhere they are.It's pretty easy to do that,you just need.........

1. half-life server (HLDS) properly installed in your computer.
2. A properly forward port.(so you are visible to outside world)
3. A free dynamic IP forwarding service (if you don't have a static IP)

And thanks to all copy cats out there,the internet is flooding with the tutorials for 1st step,but still if you can't find one here it is
(NOTE: i am a proud member of this site,so no i am not a copy cat)

2. port forwarding
If you are using a router/modem then you are behind the walls and others can't see you,so that's why he have to port forward the modem/router,so others can connect to your application using that port.
Check what kind of connection type you are using "bridge mode" or "PPPOE"
bridge-mode : you dial every-time from your PC to connect to internet.
PPPOE mode : you are always connected to internet,unless your modem is not switched off.
  If you are using bridge mode follow this steps otherwise skip to ☻
While your modem switched on and LAN properly connected type into your internet browser (no matter it's firefox,ie,chrome or opera) your modem's login window will appear type "admin" in both username and password that's by default but if you already changed it and forget then reset your modem to factory setting by pressing and holding small button,somewhere behind or bottom of your modem.

Once you are in,try to find something similar to "internet connection configuration" mine is inside configuration->internet

The first one is bridge mode connection and second one (marked also) is PPPOE mode connection which we need,click on add button......

something similar will open now as you can see,in mine VCI is 35 because i am using BSNL as my ISP and   35 work on that,if 35 not works for you try 38.Leave service category = UBR without PCR,and click on next.....
choose "PPP over ethernet" form the option leave everything else unchanged and click on next...
configure this same as per image and click on next.....
In this field you can use anything for the Service Name but your username and password should be correct as your ISP provided and select always on.
Now your modem is set to PPPOE mode and every time you switched it on,it will connect to internet directly but you have to configure your LAN settings to use it.
Go to your LAN connection properties then TCP/IP properties and select "use following IP" and enter an IP address for your computer in the format 192.168.1.X ,In "default gateway" enter your modem's IP and click ok.
Restart your modem,wait untill DSL LED gets constant and try to open any website just to check everything went OK and you are able to access internet.

Open your modem configuration again by entering in browser,login again,now try to find something like  "Advanced Security"
This thing is hidden in some modems like mine and i have to enter "" manually to open "advanced menu". If you are also not able to find it take some help from
and once you find it you will see "port forwarding" some where in it.
In my modem the "half life server" is already given in games but if not in yours just use "user defined"and enter a name for it like CSserver,"WAN interface" will be ppoe_0_35 or ppoe_0_38 whatever you are using,In
"Forward to Internal Host IP Address:" enter IP of your computer which you wanna make server in my case it's
in Protocol choose TCP/UDP and for all 4 ports enter 27015 you can use other HL supported ports too but as this one is standard we are using it.Click apply and your port 27015 is now forwarded.

3.IP forwarding (if you don't have a static-IP)
 There are many sites who provide free IP forwarding but my personal fav. is it's free but you must use your host atleast once in 60 days to keep it running .Just go to  and once you are registered click on "add host" option and choose a name which suite you for example mine is "" once you are done with creating host go and download No-IP DUC from this tool updates your current IP to the No-IP server.You are almost done open No-IP DUC ,login with your username and password and select the host name you created.
(Note: Whenever you run the server,make sure you are running No-IP DUC too otherwise your IP will not updated to your host) 
All settings done here,lets test your server run hlds.exe (configure all server settings correctly)
along with the No-IP DUC,make sure internet is working fine,now you can call your friends and tell them to join the server by typing
connect [your host name]:[port]  example ""
NOTE: static IP users use this
 connect [your ip address]:[port]  example ""
in console or by adding it to "favorites" in find server option. you can join your own server in same pc using hl.exe and play with your friends. 
I hope this post will help you to creating your own server,also check my another post it's all about administrating a CS server

Wednesday, January 12, 2011

Tricks for custom form design in VB.NET

In this tutorial we are going to learn some simple tricks for changing a simple form to cool design in VB.NET (i am using 2005) off-course beginners level.


so which one is looking cool to you?I hope that's the right one.
lets see how to achieve this,first choose an image(better to select *.png or *.gif with transparent background) which you want to use as your form template,you can do this by selecting BackgorundImage in form properties or you can use me.BackgroundImage and remember to add it as a resource file not a local file.once your image is imported you have to set it's layout by  
BackgroundImageLayout=None or  me.BackgroundImageLayout=ImageLayout.None 
and change the form size to cover the image completely,something like
we are allmost done just have to hide the original form which holds our image,this is a two step process
1.) Hide the form border.
2.) Make transparent the form background.

first step is easy just go to form properties and set 
FormBorderStyle=None or me.FormBorderStyle=Windows.Forms.FormBorderStyle.None 
however when you make your form border-less you can't move it,So we have to add some extra code here to avoid that problem.Copy this code anywhere in your form coding body.
#Region " ClientAreaMove Handling "
    Const WM_NCHITTEST As Integer = &H84
    Const HTCLIENT As Integer = &H1
    Const HTCAPTION As Integer = &H2
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Select Case m.Msg
            Case WM_NCHITTEST
                If m.Result = HTCLIENT Then m.Result = HTCAPTION
                'If m.Result.ToInt32 = HTCLIENT Then m.Result = IntPtr.op_Explicit(HTCAPTION) 'Try this in VS.NET 2002/2003 if the latter line of code doesn't do it... thx to Suhas for the tip.
            Case Else
                'Make sure you pass unhandled messages back to the default message handler.
        End Select
    End Sub
#End Region
now your form can move even without a border (don't ask me how this code works cause i don't completely  understand either,and all credit goes to the original coder)

Second step is pretty easy you just have to set form backcolor to transparent however you can't set 
BackColor=Transperant   or Me.BackColor = Color.Transparent 
because this is not supported by VB.NET.
so again need to do a little trick here,go to form properties and set BackColor to anycolor which is rarely used and ensure you are not  going to use it any where else.(In this case i am using hot pink,because that would be the most ugly color for me :P ) so again form properties
BackColor=HotPink or Me.BackColor = Color.HotPink
now we have to set the TransparencyKey exaclty same to color we choose (in this case HotPink)
TransparencyKey=HotPink or Me.TransparencyKey = Color.HotPink

that's it! your form is now boderless and transparent and after some coding this will look like......
isn't it cool? now go make one for yourself  :)